简体   繁体   中英

Using a Button click to execute ASP.net C# gridview

It seems like this would be simple, but for the life of me, I can't figure out how it would work.

I have a gridview .

I have a standard button.

How do I use the button click to display the gridview?

Any suggestions?

This is a brief example from the MSDN page: ( https://msdn.microsoft.com/en-us/library/bb907626.aspx )

You might to add an asp:TemplateField inside your GridView and through the CommandArgument property in the Button, set the current row index.

<asp:TemplateField>
  <ItemTemplate>
    <asp:Button ID="AddButton" runat="server" 
      CommandName="AddToCart" 
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
      Text="Add to Cart" />
  </ItemTemplate> 
</asp:TemplateField>

And in your code, in the RowCommand event:

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.
    }
}

Hope this help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM