我有一个Gridview并在rowDatabound上单击该行创建
显示模式弹出窗口。我想要的是当单击该行时,应该传递该行的ID。
protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, "12");
}
}
我如何在服务器控件上获取此“ 12”。我已经把12作为静态演示。但它会改变。
最佳答案
要知道单击了哪一行,必须使用Row
的GridViewRowEventArgs
属性。
protected void grdOrderProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
e.Row.Attributes["onClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnPop, e.Row.RowIndex.ToString());
}
}
RowIndex将作为参数传递给
btnPop
。为了接收它,btnPop
应该实现IPostBackEventHandler
。像这样:
public class MyControl : Button, IPostBackEventHandler
{
// Use the constructor to defined default label text.
public MyControl()
{
}
// Implement the RaisePostBackEvent method from the
// IPostBackEventHandler interface.
public void RaisePostBackEvent(string eventArgument)
{
//You receive the row number here.
}
}
请参阅ClientScriptManager.GetPostBackClientHyperlink