是否可以从GridViewRow获取CommandArgument
在for-each循环中,按钮单击事件I need CommandArgument
示例代码:
foreach (GridViewRow row in MyGridView.Rows)
{
...
string commandArgument = ?;
...
}发布于 2018-03-25 22:53:42
根据您的评论,我理解您的任务。您可以通过创建用于获取表Id的隐藏字段来解决此问题,并且您可以在选中复选框时访问此id。然后根据id进行更新。这里我正在更新网格视图的名称列。
示例:WebForm1.aspx
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="name">
<ItemTemplate>
<asp:Label ID="lblUpdate" runat="server" Text= '<%#Eval("name") %>'></asp:Label>
<asp:CheckBox ID="chkbox" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>WebForm1.aspx.cs
protected void btnSave_Click(object sender, EventArgs e) {
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType==DataControlRowType.DataRow)
{
CheckBox chkbox = (CheckBox)row.Cells[0].FindControl("chkbox");
HiddenField hdID = (HiddenField)row.Cells[0].FindControl("id");
Label lbl = (Label)row.Cells[0].FindControl("lblUpdate");
if (chkbox!=null)
{
if(chkbox.Checked)
{
string Id = hdID.Value;
//now update name... here by the help of this RowId===> Id
}
}
}
}注意这里的单元格是指第一个TemplateField数据。我之所以使用它,是因为我将所有的name字段和checkbox字段都放在了first templatefield中。
我想这会对你有帮助的。:)
https://stackoverflow.com/questions/49474643
复制相似问题