我给你想象两种简单实现用例吧。(下面是随手写的代码,没有测试,自己理解意思就行) 第一种,假设每一行有一个按钮(ID是btn),点击这一行就触发这一行所在的btn的click服务器端的事件。Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim bt As Button = DirectCast(e.Row.FindControl("btn"), Button) e.Row.Attributes("onclick") = "document.all." + bt.ClientID + ".click();" End If End Sub这是比较容易想到的思路。第二种,自己动手改造GridView,类似于。public class MyGridView:GridView , IPostBackEventHandler { public MyGridView() { this.RowDataBound += new GridViewRowEventHandler(MyGridView_RowDataBound); } void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { PostBackOptions ops = new PostBackOptions(this,"row:"+ e.Row.DataItemIndex.ToString()); e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackEventReference(ops); this.Page.ClientScript.RegisterForEventValidation(ops); } } public GridViewSelectEventHandler RowSelected; void IPostBackEventHandler.RaisePostBackEvent(string eventArgument) { if (eventArgument.StartsWith("row:")) { if (RowSelected != null) { int ItemIndex = int.Parse(eventArgument.Substring(4)); GridViewSelectEventArgs args=new GridViewSelectEventArgs(ItemIndex); RowSelected(this, args); } } } }如果是一个正规的团队开发,我比较喜欢第二种。
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" AutoPostBack="true" OnCheckedChanged="chk_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField> // .aspx.cs
void chk_CheckedChanged(object sender, EventArgs e)
{
// 触发此事件的 CheckBox
CheckBox chk = sender as CheckBox;
// 得到 CheckBox 所在行
GridViewRow row = chk.NamingContainer as GridViewRow;
// 得到 GridView,当然这里可以直接引用控件ID
GridView grd = row.NamingContainer as GridView;
// 得到主键
int productId = (int)grd.DataKeys[row.RowIndex].Value;
// more codes
// ...
//System.Diagnostics.Debug.Assert(false, productId.ToString());
Response.Write(row.RowIndex);
Response.Write(row.Cells[0].Text);
}
谢谢上面的各位!
看来还是自己下来慢慢摸索了
当checkbox选种,直接读取该行的checkbox的VALUE就是该行的索引值了。
第一种,假设每一行有一个按钮(ID是btn),点击这一行就触发这一行所在的btn的click服务器端的事件。Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim bt As Button = DirectCast(e.Row.FindControl("btn"), Button)
e.Row.Attributes("onclick") = "document.all." + bt.ClientID + ".click();"
End If
End Sub这是比较容易想到的思路。第二种,自己动手改造GridView,类似于。public class MyGridView:GridView , IPostBackEventHandler
{
public MyGridView()
{
this.RowDataBound += new GridViewRowEventHandler(MyGridView_RowDataBound);
} void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PostBackOptions ops = new PostBackOptions(this,"row:"+ e.Row.DataItemIndex.ToString());
e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackEventReference(ops);
this.Page.ClientScript.RegisterForEventValidation(ops);
}
} public GridViewSelectEventHandler RowSelected; void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
if (eventArgument.StartsWith("row:"))
{
if (RowSelected != null)
{
int ItemIndex = int.Parse(eventArgument.Substring(4));
GridViewSelectEventArgs args=new GridViewSelectEventArgs(ItemIndex);
RowSelected(this, args);
}
}
}
}如果是一个正规的团队开发,我比较喜欢第二种。