ASP.NET中的数据按钮内的按钮 [英] Button inside datalist , ASP.NET

查看:68
本文介绍了ASP.NET中的数据按钮内的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网络应用程序中,我有(FAQ)页面,其中包含使用datalist从数据库中检索到的问题和答案,单击Show-More Button时我想要答案显示。



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.

<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:

protected void Button3_Click(object sender, EventArgs e)
{

    AnswerLabel.visble = true;


}





我的尝试:



我试图添加显示答案的按钮但我收到此错误



错误CS0103当前上下文中不存在名称'AnswerLabel'



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

推荐答案

当然它不存在 - 您的列表将包含多个名为的控件AnswerLabel ,并且编译器无法知道 <$ em $ c> AnswerLabel 您想要更改的控件。



您需要使用 FindControl 来查找特定的 AnswerLabel 控件list:

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:
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;
    }
}


这篇关于ASP.NET中的数据按钮内的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆