2009-10-15 32 views
10

我有一個帶有模板字段的gridview。在該模板字段中是複選框。我在gridview之外有一個提交按鈕來分配被檢查的記錄。在回發沒有複選框註冊爲被檢查。這裏是我的代碼:GridView中的TemplateField中的複選框在回發中丟失檢查

<Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:CheckBox ID="cb" Checked="false" runat="server" /> 
         <asp:Label ID="lblCFID" runat="server" Visible="false" Text='<%# Eval("ID") %>' /> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="Name" HeaderText="Name" /> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" DataField="DOB" HeaderText="Date of Birth" /> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Gender" DataField="Gender" /> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Status" DataField="Status" /> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Plan Name" DataField="PlanName" /> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Type" DataField="ControlType" /> 
       <asp:BoundField HeaderStyle-HorizontalAlign="Center" HeaderText="Date of Service" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" DataField="DateofService" /> 
      </Columns> 

protected void AssignRecords(object sender, EventArgs e) 
{ 
    int Rows = gvASH.Rows.Count; 
    for (int i = 0; i < Rows; i++) 
    { 
     //CheckBoxField cb = ((CheckBoxField)gvASH.Rows[i].Cells[1]).; 
     CheckBox cb = (CheckBox)gvASH.Rows[i].Cells[0].FindControl("cb"); 
     Label lblID = (Label)gvASH.Rows[i].Cells[0].FindControl("lblCFID"); 
     if (cb.Checked == true) 
     { 

      string ID = lblID.Text; 
      //Assign Code 
     } 
    } 
} 

我有一個斷點設置在字符串ID = lblID.Text;但它從來沒有發現任何被檢查。

+0

我使用C#,但在VB.Net的asp.net網站這個例子中,基本上是做什麼我做:HTTP:// WWW .asp.net /學習/數據訪問/教程-52-vb.aspx 所以我知道這是可能的,我只是不知道爲什麼它不適合我。 – Jhorra 2009-10-15 22:32:07

+1

頁面生命週期是什麼時候分配被調用的方法? – 2009-10-15 23:12:58

+0

查看這裏的解決方案,你需要堅持選擇複選框http://highoncoding.com/Articles/697_Persisting_CheckBox_State_While_Paging_in_GridView_Control.aspx – user2323258 2013-04-26 09:50:52

回答

14

我想你缺的是,當你點擊按鈕,你的頁面是回傳,你重新綁定到GridView控件,你需要在這種情況下綁定像

if (!Page.IsPostBack) 
    { 
     GridView1.DataSourceID = "yourDatasourceID"; 
     GridView1.DataBind(); 
    } 
+0

你是對的,我只是在搜索方法上設置了一個斷點,並且它被再次調用。我將不得不追蹤它從哪裏被調用。 – Jhorra 2009-10-16 14:37:05

1

在回傳,內容的GridView是從page_init和page_load之間的postback Viewstate數據重新創建的。也許試着在page_load中檢查你的Gridview來看看有什麼。

0

設置複選框

的自動回屬性
AutoPostBack="true" 
相關問題