[英]How to use linkbutton in repeater using C# with ASP.NET 4.5
In asp.net I have a table in database containing questions,date of submission and answers.On page load only questions appear.now i want to use any link which on clicking show answers and date specified in table data, either in textbox or label and clicking again on that link answer disappear again like expand and shrink on click. 在asp.net中,我在数据库中有一个包含问题,提交日期和答案的表。在页面加载时只显示问题。现在我想使用任何链接,点击显示表格数据中指定的答案和日期,在文本框或标签中并再次单击该链接答案再次消失,如单击时展开和缩小。 So what coding should i use for this in C#? 那么我应该在C#中使用什么编码呢?
I believe you could handle the ItemCommand event of the Repeater. 我相信你可以处理Repeater的ItemCommand事件。
Place a LinkButton control for the link that you want the user to click in the Repeater's item template. 为您希望用户在Repeater的项目模板中单击的链接放置一个LinkButton控件。 Set the CommandName property of this to something meaningful, like "ShowAnswers". 将此CommandName属性设置为有意义的属性,如“ShowAnswers”。 Also, add a Label or TextBox control into the Repeater's item template, but set their Visible property to false within the aspx markup. 此外,将Label或TextBox控件添加到Repeater的项模板中,但在aspx标记中将其Visible属性设置为false。
In the code-behind, within the ItemCommand event handler check if the value of e.CommandName
equals your command ("ShowAnswers"). 在代码隐藏中,在ItemCommand事件处理程序中检查e.CommandName
的值e.CommandName
等于您的命令(“ShowAnswers”)。 If so, then find the Label or TextBox controls for the answers and date within that Repeater item (accessed via e.Item
). 如果是,则在该Repeater项目中找到答案和日期的Label或TextBox控件(通过e.Item
访问)。 When you find them, set their Visible property to true. 找到它们后,将其Visible属性设置为true。
Note: you could take a different approach using AJAX to provide a more seamless experience for the user, but this way is probably simpler to implement initially. 注意:您可以采用不同的方法使用AJAX为用户提供更加无缝的体验,但这种方式最初可能更简单。
I think the implementation would look something like this. 我认为实现看起来像这样。 Disclaimer: I haven't tested this code. 免责声明:我没有测试过这段代码。
Code-Behind: 代码隐藏:
void Repeater_ItemCommand(Object Sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "ShowAnswers")
{
Control control;
control = e.Item.FindControl("Answers");
if (control != null)
control.Visible = true;
control = e.Item.FindControl("Date");
if (control != null)
control.Visible = true;
}
}
ASPX Markup: ASPX加价:
<asp:Repeater id="Repeater" runat="server" OnItemCommand="Repeater_ItemCommand">
<ItemTemplate>
<asp:LinkButton id="ShowAnswers" runat="server" CommandName="ShowAnswers" />
<asp:Label id="Answers" runat="server" Text='<%# Eval("Answers") %>' Visible="false" />
<asp:Label id="Date" runat="server" Text='<%# Eval("Date") %>' Visible="false" />
</ItemTemplate>
</asp:Repeater>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.