2010-01-07 84 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 
    } 
} 
相關問題