2017-04-21 65 views
0

我有一個相當簡單的應用程序,它返回一個失敗的報告列表,在左邊有一個ID,名稱和時間綁定字段和複選框模板字段。C#GridView動態複選框消失

我有一個'Reschedule'按鈕,當它按下時,應該選取複選框被勾選並處理它們的行。

GridView加載成功,我可以選擇/取消選擇CheckBox,但是當我按下'Reschedule'按鈕並返回代碼時,複選框不再存在。

我知道這是關係到動態控制/回傳和複選框需要重新創建,我已經試了無數的建議,以前類似的問題,但沒有奏效

GridView控件 - 自動生成列假(試真)

按鈕 - 的OnClientClick =「」(試圖返回false)

最初創建的字段並將其綁定到一個數據表(數據表具有3列映射到3個綁定字段)是這樣的: -

TemplateField tfield = new TemplateField(); 
failedSchedulesGridView.Columns.Add(tfield); 

BoundField bfield1 = new BoundField(); 
bfield1.HeaderText = "SI_ID"; 
bfield1.DataField = "si_id"; 
failedSchedulesGridView.Columns.Add(bfield1); 

BoundField bfield2 = new BoundField(); 
bfield2.HeaderText = "SI_NAME"; 
bfield2.DataField = "si_name"; 
failedSchedulesGridView.Columns.Add(bfield2); 

BoundField bfield3 = new BoundField(); 
bfield3.HeaderText = "SI_UPDATE_TS"; 
bfield3.DataField = "si_update_ts"; 
failedSchedulesGridView.Columns.Add(bfield3); 

failedSchedulesGridView.DataSource = dt; 
failedSchedulesGridView.DataBind(); 

的Page_Load 可以看出我已經試過這裏重新創建GridView的列,但它沒有工作,被註釋掉

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!this.IsPostBack) 
    { 
     //TemplateField tfield = new TemplateField(); 
     //failedSchedulesGridView.Columns.Add(tfield); 

     //BoundField bfield1 = new BoundField(); 
     //bfield1.HeaderText = "SI_ID"; 
     //bfield1.DataField = "si_id"; 
     //failedSchedulesGridView.Columns.Add(bfield1); 

     //BoundField bfield2 = new BoundField(); 
     //bfield2.HeaderText = "SI_NAME"; 
     //bfield2.DataField = "si_name"; 
     //failedSchedulesGridView.Columns.Add(bfield2); 

     //BoundField bfield3 = new BoundField(); 
     //bfield3.HeaderText = "SI_UPDATE_TS"; 
     //bfield3.DataField = "si_update_ts"; 
     //failedSchedulesGridView.Columns.Add(bfield3); 

    } 
    failedSchedulesGridView.DataSource = dt; 
    failedSchedulesGridView.DataBind(); 
} 

OnRowDataBound

protected void OnRowDataBound(Object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType != DataControlRowType.Header) 
    { 
     cbx++; 
     CheckBox cb = new CheckBox(); 
     cb.ID = "cb" + cbx; 
     e.Row.Cells[0].Controls.Add(cb); 
    } 
} 

當我嘗試代碼失敗在按下「重新計劃」按鈕之後訪問複選框,因爲未找到checbox: -

protected void ReschedulePB2_Click(object sender, EventArgs e) 
{ 
    int i = 0; 
    foreach (GridViewRow row in failedSchedulesGridView.Rows) 
    { 
     i++; 
     string cbName = "cb" + i; 
     CheckBox cb = (CheckBox)row.Cells[0].FindControl(cbName); 

     if (cb.Checked) 
+0

你爲什麼要創建c#中的列可以在aspx頁面中做對嗎? – Krishna

+0

我不熟練使用ASP,我最近已經從COBOL交叉培訓到C#,而且我的ASP技能僅限於Visual Studio爲我生成的內容。 –

回答

0

試試這個

寫在你的aspx頁面

<asp:GridView ID="failedSchedulesGridView" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" Width="850px" onrowcommand="GridView1_RowCommand"> 
     <Columns> 
      <asp:TemplateField > 
       <HeaderTemplate> 
        <asp:CheckBox ID="cbHeader" runat="server" /> 
       </HeaderTemplate> 
       <ItemTemplate> 
        <asp:CheckBox ID="cbItem" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="si_id" HeaderText="SI_ID" SortExpression="si_id" /> 
      <asp:BoundField DataField="si_name" HeaderText="SI_NAME" SortExpression="si_name" /> 
      <asp:BoundField DataField="si_update_ts" HeaderText="SI_UPDATE_TS" SortExpression="si_update_ts" /> 
     </Columns> 
</asp:GridView> 

你的頁面加載應該是這樣的

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!this.IsPostBack) 
    { 
     failedSchedulesGridView.DataSource = dt; 
     failedSchedulesGridView.DataBind(); 
    } 
} 

你重新安排點擊將

protected void ReschedulePB2_Click(object sender, EventArgs e) 
{ 

    foreach (GridViewRow row in failedSchedulesGridView.Rows) 
    { 
     CheckBox cb = (CheckBox)row.Cells[0].FindControl("cbItem"); 

     if (cb.Checked) 
+0

非常好,非常感謝 –