2010-01-07 4 views
0

동적으로 gridview를 만들었으므로 이제 selectedindex가 변경되었을 때 이벤트를 실행하고 싶습니다.selectedindexchanged on dynamic created 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을 생성하지 않습니다.

 void NewDg_SelectedIndexChanged(object sender, EventArgs e) 
    { 

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

: 만이이 기능에 포스트 백으로 이어질하지 않습니다

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

:

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

이 다음과 같은 코드를 생성 : 나는 다음과 같은 코드를 가지고있는 RowDataBound에서 아무도 내가 뭘 잘못하고 있는지 알아?

+0

당신이 이벤트를 발생시키고있는 subgridview가 두 번째 포스트 백에 더 이상 존재하지 않는다고 생각합니다. 그 사건을 일으키지 않는 이유가 될 것입니다 –

+0

그건 꽤 논리적으로 들리 겠지만,이 subgridview를 선택하는 방법도 있어야하지 않아야합니까? – Michael

+0

음 ...동적 컨트롤로 놀았을 때 문제가 더 이상 존재하지 않아서 제 해결 방법은 클라이언트가 실제로 들어오는 것을보기 위해 요청 객체를 검사하고, subgridview를 다시 만들 때이 값을 고려하여 –

답변

0

내가 Code Project에 내 질문에 대한 답을 발견

내가 지금 내있는 gridview에있는 gridview를 사용

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

GridView의 Extender 때문에 더하기 기호 (링크 참조)를 클릭하면 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 이벤트가 있습니다. i는 다음을 수행하는 열 생성 이벤트

: 또한 데이터 바인딩 및 selectedIndex의 Changed 이벤트가있는 subgridview에서

 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; 
      } 
     } 
    } 

. 이 지금 작동합니다!

 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 기능은 다른 사업부에서 선택한 logentry의 세부 정보를 표시

나는이 바인딩 이벤트를 사용합니다. 이제 마지막 단계에서 바쁘다. 행을 클릭하기 전에 데이터를 계속 보여주고있다.

도움 주셔서 감사합니다. 그러나 이것이 내 문제의 해결책입니다.

0

먼저 모든 페이지로드시 눈금을 다시 만드시겠습니까? 이 방법으로 그리드를 작성하기위한 요구 사항입니다. 둘째, RowCommand를 두드려보고 그 방법으로 명령 이름을 찾으십시오. 어쩌면 그것은 성공적으로 발사 될 것이다; 당신은에서와 같이 명령 인수를 통해 명령에 대한 참조를 가져 :

void rowcmd(..) { 
    if (e.CommandName != null && e.CommandName.StartsWith("Select")) { 
     //Dothis 
    } 
} 
관련 문제