2010-01-07 58 views
0

我创建了一个gridview的动态,现在我要当selectedIndex改变时触发事件。的SelectedIndexChanged上动态创建的GridView

GridView NewDg = new GridView(); 
NewDg.ID = "SubGridView" + e.Row.RowIndex.ToString(); 
NewDg.DataKeyNames = new string[]{"logentry_id"}; 
NewDg.SelectedIndexChanged += new EventHandler(NewDg_SelectedIndexChanged); 
NewDg.RowDataBound += new GridViewRowEventHandler(NewDg_RowDataBound); 

RowDataBound的作品,但它不会产生正确的回发URL我猜。 在的RowDataBound我有以下代码:

GridView sendingGridView = (GridView)sender; 
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex); 

这将产生以下代码:

javascript:__doPostBack('SubGridView4','Select$0') 

只有这不会导致回发到这个功能:

 void NewDg_SelectedIndexChanged(object sender, EventArgs e) 
    { 

     GridView sendingGridView = (GridView)sender; 
     ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString())); 
    } 

有谁知道我做错了什么?

+0

我想您所suscribing事件,在第二回传不再存在subgridview;这将是该事件不加 – 2010-01-07 15:58:13

+0

这听起来很合乎逻辑的原因,但也应该有一种方法来选择此subgridview不应该吗? – Michael 2010-01-07 16:01:06

+0

呃...当我一直在使用动态控件时,问题在于它们不再存在,所以我的解决方法是检查Request对象以查看实际来自客户端的内容,并在重新创建子网格视图时考虑这些值 – 2010-01-07 16:03:46

回答

0

我找到了答案,我的问题上Code Project

我现在在我的GridView控件使用GridView

  <asp:TemplateField> 
      <ItemTemplate> 
       <asp:GridView ID="SubGridView" 

因为在GridView中延伸的,在GridView会当我点击加号显示出来(见链接)

在页面加载我执行以下操作:

 protected void Page_Load(object sender, EventArgs e) 
    { 
     GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated); 
     GridView1.DataSource = dc.GetLogEntriesWithUsername(); 
     GridView1.DataBind(); 

我在这个gridview上已经有了一个DataBound和一个Selected Index Changed事件。

该行创建的事件我执行以下操作:

 void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      GridView SubGridView = e.Row.FindControl("SubGridView") as GridView; 
      List<GetLogEntriesWithUsernameByParentIdResult> subLogEntries = dc.GetLogEntriesWithUsernameByParentId(((GetLogEntriesWithUsernameResult)e.Row.DataItem).logentry_id).ToList(); 
      if (subLogEntries.Count > 0) 
      { 
       SubGridView.DataSource = subLogEntries; 
       SubGridView.DataBind(); 
       (e.Row as ExtGridViewRow).ShowExpand = SubGridView.Rows.Count > 0; 
      } 
     } 
    } 

在subgridview我也有一个数据绑定和SelectedIndex的Changed事件。这现在起作用了!

我用这个数据绑定事件:

 protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) 
    {  // only apply changes if its DataRow 
     GridView sendingGridView = (GridView)sender; 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      // when mouse is over the row, save original color to new attribute, and change it to highlight yellow color 
      e.Row.Attributes.Add("onmouseover", 
      "this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='#C0C0C0';this.style.cursor='pointer';"); 

      // when mouse leaves the row, change the bg color to its original value 
      e.Row.Attributes.Add("onmouseout", 
      "this.style.backgroundColor=this.originalstyle;this.style.cursor='cursor'"); 
      //e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex)); 

      e.Row.Cells[1].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex); 
      e.Row.Cells[2].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex); 
      e.Row.Cells[3].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex); 
      e.Row.Cells[4].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex); 
      e.Row.Cells[5].Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(sendingGridView, "Select$" + e.Row.RowIndex); 

和所选择的指数变化情况:

 protected void GridView_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     GridView sendingGridView = (GridView)sender; 
     ViewDetails(Convert.ToInt32(sendingGridView.SelectedDataKey["logentry_id"].ToString()));   
    } 

的ViewDetails功能显示在不同的DIV选择logentry的细节。 现在我忙于最后一步,那就是不断显示数据,因为它是在我点击一行之前。

感谢您的帮助,但是这是解决我的问题。

0

首先,你现在重新创建每个页面加载电网?这是以这种方式创建网格的要求。其次,尝试点击RowCommand,并以这种方式查找命令名称;也许这将成功地启动;你通过命令参数的引用的命令,如:

void rowcmd(..) { 
    if (e.CommandName != null && e.CommandName.StartsWith("Select")) { 
     //Dothis 
    } 
} 
相关问题