Gridview 中,自定义了几个按钮,比如:<asp:linkbutton id="btn1" CommandArgument='<%# Eval("ID")%>'></asp:linkbutton>
<asp:button id="btn2" CommandArgument='<%# Eval("ID")%>'></asp:button>
<asp:imagebutton id="btn3" CommandArgument='<%# Eval("ID")%>'></asp:imagebutton>他们的CommandArgument属性绑定的都是数据库的ID字段。然后我点击之后,在command事件里面,通过e.CommandArgument得到字段值。问题:给每个按钮都加上这属性挺麻烦的,有没有更好一些的办法,不需要给每个按钮都加属性,并且可以在command事件中得到点击的改行的ID值?谢谢。!
<asp:button id="btn2" CommandArgument='<%# Eval("ID")%>'></asp:button>
<asp:imagebutton id="btn3" CommandArgument='<%# Eval("ID")%>'></asp:imagebutton>他们的CommandArgument属性绑定的都是数据库的ID字段。然后我点击之后,在command事件里面,通过e.CommandArgument得到字段值。问题:给每个按钮都加上这属性挺麻烦的,有没有更好一些的办法,不需要给每个按钮都加属性,并且可以在command事件中得到点击的改行的ID值?谢谢。!
可以通过e.CommandSource来获得id
限定在RowCommand事件中,做了一下:(其实直接在LinkButton中也可以)
例如linkbutton,commandName是se,可这样获得id
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToString() == "se")
{
LinkButton myL = (LinkButton)e.CommandSource;
int Index = ((GridViewRow)(myL.NamingContainer)).RowIndex;//获得行号
string id = GridView1.DataKeys[Index].Value.ToString();//获得id
}
}
获取的是控件的ID,获取不到你绑定列的=>能获取的是控件的ID,获取不到你绑定列的