【发布时间】:2015-10-07 17:25:00
【问题描述】:
看起来这很简单,但对于我的生活,我无法弄清楚它是如何工作的。
我有一个gridview。
我有一个标准按钮。
如何使用按钮点击显示gridview?
有什么建议吗?
【问题讨论】:
-
“执行”gridview的定义是什么?
-
抱歉,我认为选词不好。我想用按钮点击来显示gridview。
看起来这很简单,但对于我的生活,我无法弄清楚它是如何工作的。
我有一个gridview。
我有一个标准按钮。
如何使用按钮点击显示gridview?
有什么建议吗?
【问题讨论】:
这是来自 MSDN 页面的一个简短示例:(https://msdn.microsoft.com/en-us/library/bb907626.aspx)
您可以在GridView 中添加asp:TemplateField,并通过Button 中的CommandArgument 属性设置当前行索引。
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="AddToCart"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
Text="Add to Cart" />
</ItemTemplate>
</asp:TemplateField>
在您的代码中,在RowCommand 事件中:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "AddToCart") {
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = GridView1.Rows[index];
// Add code here to add the item to the shopping cart.
}
}
希望对您有所帮助。
【讨论】: