如何通过单击 ASP.NET gridview 中一行中的按钮来获取行数据
Posted
技术标签:
【中文标题】如何通过单击 ASP.NET gridview 中一行中的按钮来获取行数据【英文标题】:How to get row data by clicking a button in a row in an ASP.NET gridview 【发布时间】:2012-12-24 15:51:36 【问题描述】:我在一个 ASP.NET Web 应用程序中有一个 GridView
,我在其中的每一行中添加了两个按钮:
<ItemTemplate>
<asp:Button ID="btnEdit" Text="Edit" runat="server" />
<asp:Button ID="btnDelete" Text="Delete" runat="server"/>
</ItemTemplate>
现在如何通过单击一行中的编辑按钮从 gridview 获取行数据?
【问题讨论】:
【参考方案1】:你也可以像这样使用按钮点击事件:
<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
【讨论】:
thnx 再次快速回复。我试过了...它显示 ithis 错误“无效的回发或回调参数。事件验证在配置中使用将commandName
放在.aspx 页面中
<asp:Button ID="btnDelete" Text="Delete" runat="server" CssClass="CoolButtons" CommandName="DeleteData"/>
为网格订阅rowCommand
事件,你可以这样尝试,
protected void grdBillingdata_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "DeleteData")
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
HiddenField hdnDataId = (HiddenField)row.FindControl("hdnDataId");
【讨论】:
这还不错,只是你忘了注明hdnDataId
的来源。【参考方案3】:
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="MyButtonClick" />
</ItemTemplate>
你的方法
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;
【讨论】:
这似乎与 Pranay Rana 的回答完全相同。【参考方案4】:您是否有任何特定原因希望您在项目模板中使用按钮。您也可以通过以下方式进行操作,从而为您提供网格行编辑事件的全部功能。您还可以获得轻松接线的奖励取消和删除功能。
标记
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
ImageUrl="~/images/Edit.png" Style="height: 16px" ToolTip="Edit"
CausesValidation="False" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update"
Text="Update" Visible="true" ImageUrl="~/images/saveHS.png"
/>
<asp:LinkButton ID="btnCancel" runat="server" CommandName="Cancel"
ImageUrl="~/images/Edit_UndoHS.png" />
<asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete"
ImageUrl="~/images/delete.png" />
</EditItemTemplate>
<ControlStyle BackColor="Transparent" BorderStyle="None" />
<FooterStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
背后的代码
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
TextBox txtledName = (TextBox) GridView1.Rows[e.NewEditIndex].FindControl("txtAccountName");
//then do something with the retrieved textbox's text.
【讨论】:
【参考方案5】:<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="LnKB" Text='edit' OnClick="LnKB_Click" >
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
protected void LnKB_Click(object sender, System.EventArgs e)
LinkButton lb = sender as LinkButton;
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
int x = clickedRow.RowIndex;
int id = Convert.ToInt32(yourgridviewname.Rows[x].Cells[0].Text);
lbl.Text = yourgridviewname.Rows[x].Cells[2].Text;
【讨论】:
【参考方案6】: <asp:Button ID="btnEdit" Text="Edit" runat="server" OnClick="btnEdit_Click" CssClass="CoolButtons"/>
protected void btnEdit_Click(object sender, EventArgs e)
Button btnEdit = (Button)sender;
GridViewRow Grow = (GridViewRow)btnEdit.NamingContainer;
TextBox txtledName = (TextBox)Grow.FindControl("txtAccountName");
HyperLink HplnkDr = (HyperLink)Grow.FindControl("HplnkDr");
TextBox txtnarration = (TextBox)Grow.FindControl("txtnarration");
//Get the gridview Row Details
与删除按钮相同
【讨论】:
【参考方案7】: protected void btnS10_click(object sender, EventArgs e)
foreach (GridViewRow row in Grd.Rows)
CheckBox chk_Single = (CheckBox)row.FindControl("ChkSendOne");
if (row.RowType == DataControlRowType.DataRow)
string id = (row.Cells[0].FindControl("lblSNo") as Label).Text;
if (Convert.ToInt32(id) <= 10)
chk_Single.Checked = true;
if (chk_Single.Checked == true)
lblSelectedRecord.InnerText = (Convert.ToInt32(lblSelectedRecord.InnerText) + 1).ToString();
【讨论】:
以上是关于如何通过单击 ASP.NET gridview 中一行中的按钮来获取行数据的主要内容,如果未能解决你的问题,请参考以下文章
如何通过单击一个按钮保存 asp.net 文本框值并将该数据用于另一个按钮单击?
如何在 Asp.net c# 中为 GridView 行启用双击和单击
如何在 c# ASP.NET 中使用 GridView_RowCommand 事件从 GridView 获取图像