gpt4 book ai didi

c# - 如何通过单击 ASP.NET gridview 中一行中的按钮获取行数据

转载 作者:IT王子 更新时间:2023-10-29 04:29:10 25 4
gpt4 key购买 nike

我在 ASP.NET web 应用程序中有一个 GridView,我在其中的每一行中添加了两个按钮:

 <ItemTemplate>
<asp:Button ID="btnEdit" Text="Edit" runat="server" />
<asp:Button ID="btnDelete" Text="Delete" runat="server"/>
</ItemTemplate>

现在如何通过单击一行中的编辑按钮从 gridview 中获取行数据?

最佳答案

你也可以像这样使用按钮点击事件:

<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="MyButtonClick" />
</ItemTemplate>
</asp:TemplateField>
protected void MyButtonClick(object sender, System.EventArgs e)
{
//Get the button that raised the event
Button btn = (Button)sender;

//Get the row that contains this button
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
}

你可以这样获取数据:

 void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{

// If multiple ButtonField column fields are used, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Select")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);

// Get the last name of the selected author from the appropriate
// cell in the GridView control.
GridViewRow selectedRow = CustomersGridView.Rows[index];
}
}

gridview 中的 Button 应该有这样的命令并处理 rowcommand 事件:

<asp:gridview id="CustomersGridView" 
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="false"
onrowcommand="CustomersGridView_RowCommand"
runat="server">

<columns>
<asp:buttonfield buttontype="Button"
commandname="Select"
headertext="Select Customer"
text="Select"/>
</columns>
</asp:gridview>

Check full example on MSDN

关于c# - 如何通过单击 ASP.NET gridview 中一行中的按钮获取行数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14254880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com