Click here to Skip to main content
15,883,817 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my web application I have ( FAQ) page, which contains retrieved Questions and Answers from the database using "datalist" , I want The Answers display when click on Show-More Button.

C#
<asp:DataList ID="DataList1" runat="server" Width="595px" OnSelectedIndexChanged="DataList1_SelectedIndexChanged1"  >
                                      <ItemTemplate>

                                          Q:
                                          <asp:Label ID="QuestionLabel" runat="server" CssClass="labelcolor" Text='<%# Eval("Question") %>' />
                                          <br/>
                                          <asp:Button ID="Button3" runat="server" Text="Show_More" onclick=" Button3_Click" />
                                          A:
                                          <asp:Label ID="AnswerLabel" runat="server" Text='<%# Eval("Answer") %>'  Visible="false"/>
                                          <br />

                                      </ItemTemplate>
                                  </asp:DataList>                                   </asp:DataList>





code behind:

C#
protected void Button3_Click(object sender, EventArgs e)
{

    AnswerLabel.visble = true;


}


What I have tried:

I tried to add button that display the Answer but I got this error

Error CS0103 The name 'AnswerLabel' does not exist in the current context
Posted
Updated 11-Apr-16 6:38am

1 solution

Of course it doesn't exist - your list will contain multiple controls called AnswerLabel, and the compiler has no way of knowing which AnswerLabel control you want to change.

You need to use FindControl to find the specific AnswerLabel control within the list:
C#
protected void Button3_Click(object sender, EventArgs e)
{
    var button = (Control)sender;
    var answerLabel = button.NamingContainer.FindControl("AnswerLabel");
    if (answerLabel != null)
    {
        answerLabel.Visible = true;
        button.Visible = false;
    }
}
 
Share this answer
 
Comments
Member 12451924 11-Apr-16 12:56pm    
I got this error when I click on button :

Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Richard Deeming 11-Apr-16 13:01pm    
Then there's something wrong with the way you're binding your data.
Member 12451924 11-Apr-16 13:10pm    
I changed it to
<asp:Button ID="Button3" runat="server" Text="Button" onclick="Button3_Click" CausesValidation="True" UseSubmitBehavior="False" />
and it is working , Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900