【问题标题】:Using a Button click to execute ASP.net C# gridview使用按钮单击执行 ASP.net C# gridview
【发布时间】:2015-10-07 17:25:00
【问题描述】:

看起来这很简单,但对于我的生活,我无法弄清楚它是如何工作的。

我有一个gridview

我有一个标准按钮。

如何使用按钮点击显示gridview?

有什么建议吗?

【问题讨论】:

  • “执行”gridview的定义是什么?
  • 抱歉,我认为选词不好。我想用按钮点击来显示gridview。

标签: c# asp.net gridview


【解决方案1】:

这是来自 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.
    }
}

希望对您有所帮助。

【讨论】:

  • 都是好人。我想出了如何做我想做的事。我想多想了。我只是将 GridView.Visible 设置为“false”,然后使用按钮单击事件使其再次可见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多