GridView 中RowCommand事件用法
//如果我照提示说的把enableEventValidation的值设为False,就获取不到e.CommandArgument的值.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Del")
{
Response.Write("删除");
GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
string index = row.Cells[3].Text;//当前行第一个单元格的值
object myCommandArgument = e.CommandArgument;
Response.Write(myCommandArgument);
Response.Write(index);
}
if (e.CommandName == "Upd")
{
Response.Write("修改");
string dw = e.CommandArgument.ToString();
for (int i = 0; i < GridView1.Rows.Count; i++)
{
string id = Convert.ToString(GridView1.Rows.Cells[0].Text);
if (id == dw)
{
Response.Write(id);
}
}
}
}
<asp:GridView ID="GridView1" DataKeyNames="ItemID" runat="server" Width="100%" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand"
>
<Columns>
<asp:BoundField HeaderText="宠物ID" DataField="ItemID" ReadOnly="True"/>
<asp:BoundField HeaderText="宠物名称" DataField="ShangpinName" />
<asp:BoundField HeaderText="售价" DataField="Price" DataFormatString="{0:c}" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="数量" DataField="Quantity" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:ButtonField HeaderText="选择" Text="选择" CommandName="select" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:ButtonField>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CommandArgument='<%# Eval("ItemID")+"*"+Eval("ShangpinName") +"*"+Eval("Price")+"*"+Eval("Quantity")+"*"+Eval("ListPrice")%>'
CommandName="Del">选择</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%# Eval("ItemID") %>' CommandName="Upd">选择</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<HeaderTemplate>
全选<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="True" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>