Accessing Control within a Repeater or Stopping a DropDownList from causing Validation

Last post 12-01-2007 1:26 AM by Hegemon Wiggin. 4 replies.

Sort Posts:

  • Accessing Control within a Repeater or Stopping a DropDownList from causing Validation

    11-29-2007, 7:37 AM

    Ok, this should be a rather simple question, I hope. I've tried reading the MSDN tutorials (and many Google searches), but don't have enough understanding of the interaction of the code behind and the main page to actually write the code myself. First off, here's the code:

    Code:
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <br />
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
    <HeaderTemplate>
    <table width="800" class="test">
    </HeaderTemplate>
    <ItemTemplate>
    <tr>
    <td colspan="4">
    <asp:Label runat="server" ID="Label1" Text='<%# Eval("question") %>' />
    </td>
    <td>
    Written by: <asp:Label runat="server" ID="Label13" Text='<%# Eval("writer") %>' />
    <br />
    On unit: <asp:Label runat="server" ID="Label14" Text='<%# Eval("unit") %>' /> regarding
    <asp:Label runat="server" ID="Label15" Text='<%# Eval("section") %>' />
    </td>
    </tr>
    <tr>
    <td width="150">
    A) <asp:Label runat="server" ID="Label7" Text='<%# Eval("AnswerA") %>' />
    <br />
    </td>
    <td width="150">
    B) <asp:Label runat="server" ID="Label8" Text='<%# Eval("AnswerB") %>' />
    <br />
    </td>
    <td width="150">
    C) <asp:Label runat="server" ID="Label9" Text='<%# Eval("AnswerC") %>' />
    <br />
    </td>
    <td width="150">
    D) <asp:Label runat="server" ID="Label10" Text='<%# Eval("AnswerD") %>' />
    <br />
    </td>
    <td width="150">
    E) <asp:Label runat="server" ID="Label11" Text='<%# Eval("AnswerE") %>' />
    <br />
    </td>
    <td width="50">
    Your Answer:
    <asp:DropdownList ID="DropDownList1" runat="server" >
    <asp:ListItem Value="" Text =" " Selected="True"></asp:ListItem>
    <asp:ListItem Value="A" Text="A) " Selected="False"></asp:ListItem>
    <asp:ListItem Value="B" Text="B) " Selected="False"></asp:ListItem>
    <asp:ListItem Value="C" Text="C) " Selected="False"></asp:ListItem>
    <asp:ListItem Value="D" Text="D) " Selected="False"></asp:ListItem>
    <asp:ListItem Value="E" Text="E) " Selected="False"></asp:ListItem>
    </asp:DropdownList>
    <asp:CompareValidator ID="CompareValidator1" runat="server"
    ValueToCompare='<%# Eval("CorrectAnswer") %>' ControlToValidate="DropDownList1"
    ErrorMessage="Answer Incorrect." ValidationGroup="answerssubmitted" Visible="False"></asp:CompareValidator>
    </td>
    </tr>
    </ItemTemplate>
    <FooterTemplate>
    <td colspan="6">
    <asp:Button ID="Button1" runat="server" Text="Submit" UseSubmitBehavior="False" ValidationGroup="answerssubmitted" CommandName="MakeValidationVisible" OnClientClick="Button_Click" />
    </td></table>
    </FooterTemplate>
    </asp:Repeater>

    This is a simple multiple-choice question database. I decided that the simplest way to check whether the answer was correct would be a CompareValidator. However, when an answer is selected by the user, the DropDownList triggers validation, rather than waiting for the submit button.

    I didn't know how to fix that (hopefully, you can tell I'm a beginner at this), so decided to hide the Validator until the submit button is pressed, and to use the submit button to set the "Visible" property of the Validator to "True".

    If someone can either explain how to make the the DropDownList not trigger validation (setting its "CausesValidation" property to false didn't do anything), or how to use my button to make the validator appear, that would be great.

    I'm at a complete loss on how to do this, and would greatly appreciate any help. I expect the code for the latter is something like:
    Quote:

    Sub Button_Click(ByVal Sender As Object, ByVal e As EventArgs)
    e.Item.FindControl("CompareValidator1").Visible=True
    End Sub

    Those are the kind of things that I'm reading around, but I really don't know what any of it means, so my attempts to code it myself are in vain. Again, any help would be greatly appreciated.

     

  • Re: Accessing Control within a Repeater or Stopping a DropDownList from causing Validation

    11-29-2007, 3:27 PM
    • Loading...
    • rmaiya
    • Joined on 06-25-2007, 11:08 PM
    • Olympia, WA
    • Posts 1,237

    Putting a  Compare validator inside the repeater for the correct answer creates a loop hole in your system. Anybody can view source and check what is the correct answer and they can select that answer. You need look a different way of doing this. If you still want to use compare validator then, I would suggest set the CausesValidation property of your Submit button to False and on the server side you can validate it.

    Raghu
    (MCSD.NET, MCAD.NET, MCDBA)
    [Don't forget to click on Mark as answer on the post that helped you ]
  • Re: Accessing Control within a Repeater or Stopping a DropDownList from causing Validation

    11-29-2007, 5:33 PM

    Ok, thanks for letting me know about that part, however, knowing that the users wouldn't think to do that and that they'd be using the system for self-improvement, I'm not overly concerned about it.

    If I set the submit behaviour to "False", how do I write the server-side code? While I'm fairly comfortable with VWD to create controls, I'm utterly lost with the back-end, especially when I'm accessing a control inside a repeater.

    I presume what you're saying entails writing code that is triggered by the submit button and then leads to Validation. If I ignore the loophole, I think I still have as much of a problem as before:
        1. Choosing an item of the drop-down list causes validation. I don't know how to stop this.
        2. My workaround to 1. was setting the CompareValidator 'Visible' property to "False", and then switching it to "True" when submit was pressed... at least, that's how it works in theory, but I have no idea how to write the code that would accomplish this.

    Any suggestions on those two specific problems? What you mentioned is good to know, but it doesn't solve my specific concern.

     Thanks!
     

  • Re: Accessing Control within a Repeater or Stopping a DropDownList from causing Validation

    11-30-2007, 1:35 PM
    Answer
    • Loading...
    • rmaiya
    • Joined on 06-25-2007, 11:08 PM
    • Olympia, WA
    • Posts 1,237

     Best way to fix your problem , remove your validation control from the repeater and replace it with a hidden text box and set the text property of that text box to correct answer. And put a label next to the dropdown set its color to red and make it visible false.

    And on the button click event do it like this 

    ButtonClick_Event ()

      foreach (RepeaterItem item in Repeater1.Items)
          {

            DropdownList ddl = (DropdownList)(item.FindControl("dropdownList1"));

            TextBox correctAns = (TextBox )(item.FindControl("hiddentextBox"));

             Label lblError = (Label)(item.FindControl("ErrorLable"));

           if ( ddl.SelectedValue != correctAns.Text)

    {

        lblError.Text = "Not A correct Ans" ;

        lblError.Visible = True;

    }
          
          }

    Raghu
    (MCSD.NET, MCAD.NET, MCDBA)
    [Don't forget to click on Mark as answer on the post that helped you ]
  • Re: Accessing Control within a Repeater or Stopping a DropDownList from causing Validation

    12-01-2007, 1:26 AM

    Thank you so much! For some reason, I couldn't use that on a code-behind page, and ended up just inserting it within <Script> tags on the page itself, but regardless, it worked, so I'm happy.  I had to fiddle with names for a bit, but I got it working. Thanks again for all your help!

Page 1 of 1 (5 items)
Microsoft Communities
Page view counter