如何在使用 C# 和 ASP.NET 4.5 的中继器中使用链接按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14861690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use linkbutton in repeater using C# with ASP.NET 4.5
提问by user2069465
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. So what coding should i use for this in C#?
在 asp.net 中,我在数据库中有一个表格,其中包含问题、提交日期和答案。在页面加载时只出现问题。现在我想使用任何链接,点击显示表格数据中指定的答案和日期,在文本框或标签中并再次点击该链接答案再次消失,就像点击时扩大和缩小一样。那么我应该在 C# 中使用什么编码呢?
采纳答案by Dr. Wily's Apprentice
I believe you could handle the ItemCommandevent of the Repeater.
我相信你可以处理中继器的ItemCommand事件。
Place a LinkButton control for the link that you want the user to click in the Repeater's item template. Set the CommandName property of this to something meaningful, like "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.
为您希望用户在中继器的项目模板中单击的链接放置一个 LinkButton 控件。将它的 CommandName 属性设置为有意义的值,例如“ShowAnswers”。此外,将 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"). If so, then find the Label or TextBox controls for the answers and date within that Repeater item (accessed via e.Item
). When you find them, set their Visible property to true.
在代码隐藏中,在 ItemCommand 事件处理程序中检查 的值是否e.CommandName
等于您的命令(“ShowAnswers”)。如果是这样,则在该 Repeater 项目(通过 访问e.Item
)中找到答案和日期的 Label 或 TextBox 控件。找到它们后,将它们的 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>