简体   繁体   English

动态gridview-如何在rowdatabound中引用

[英]Dynamic gridview - how to reference in rowdatabound

I am dynamically creating several gridviews depending on data in a DB. 我正在根据数据库中的数据动态创建几个gridview。 I generate the gridview as follows in a foreach statement: 我在foreach语句中按以下方式生成gridview:

GridView gdv = new GridView();
gdv.ID = "gdv" + i.ToString();
gdv.SelectedIndexChanged += new EventHandler(gdv_SelectedIndexChanged);
gdv.RowDataBound += gdv_RowDataBound; 

RowDataBound looks like the following: RowDataBound如下所示:

protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {             
      e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(((GridView)sender), "Select$" + e.Row.RowIndex);
   }
}

This all works as expected, however the ((GridView)sender) returns the following value: 所有这些都按预期工作,但是[[GridView)sender)返回以下值:

'gdv00'

What i need returned for this link to work is the following: 为了使此链接正常工作,我需要返回以下内容:

'ctl00$ctl00$MainContent$Main$gdv0'

Pretty sure this has something to do with the master pages? 可以确定这与母版页有关吗? but not sure how to reference this properly in the rowdatabound event? 但不确定如何在rowdatabound事件中正确引用此内容?

The ID of ctl00$ctl00$MainContent$Main$gdv is the UniqueID property of the grid control (client ID is separated by _). ctl00 $ ctl00 $ MainContent $ Main $ gdv的ID是网格控件的UniqueID属性(客户端ID用_分隔)。 This is the ID rendered to the browser. 这是呈现给浏览器的ID。 I think the problem here is that the control isn't added to the control tree immediately, which has been a problem for some in the past. 我认为这里的问题是该控件没有立即添加到控件树中,这在过去曾是一个问题。 Try doing it this way: 尝试以这种方式进行操作:

GridView gdv = new GridView();
gdv.ID = "gdv" + i.ToString();
panel.Controls.Add(gdv);

//set grid props

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM