在asp.net中,我在数据库中有一个包含问题,提交日期和答案的表。在页面加载时只出现问题。现在我想使用任何链接点击显示表格数据中指定的答案和日期,或者文本框或标签再次单击该链接答案会再次消失,例如单击时展开和缩小。 那么我应该在C#中使用什么编码?
答案 0 :(得分:8)
我相信你可以处理Repeater的ItemCommand事件。
为您希望用户在Repeater项目模板中单击的链接放置一个LinkButton控件。将此CommandName属性设置为有意义的属性,如“ShowAnswers”。另外,将Label或TextBox控件添加到Repeater的项模板中,但在aspx标记中将其Visible属性设置为false。
在代码隐藏中,在ItemCommand事件处理程序中检查e.CommandName
的值是否等于您的命令(“ShowAnswers”)。如果是,则在该Repeater项目中找到答案和日期的Label或TextBox控件(通过e.Item
访问)。找到它们后,将其Visible属性设置为true。
注意:您可以采用不同的方法使用AJAX为用户提供更加无缝的体验,但这种方式最初可能更简单。
我认为实现看起来像这样。免责声明:我没有测试过此代码。
代码隐藏:
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标记:
<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>