Programmatically accessing a control within a Repeater

Joined
Nov 29, 2007
Messages
1
Reaction score
0
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:
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 I'm writing in vain. Again, any help would be greatly appreciated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top