2012-11-28 82 views
2

我是.NET的newby,我的複選框有問題。即使他們被選中,他們也會返回錯誤。這裏是我的ASP代碼.NET複選框。總是返回false

<asp:GridView ID="gvGeneros1" runat="server" class="divTable" 
     AutoGenerateColumns="False" DataKeyNames="idgenero" CssClass="table"> 
     <Columns> 
      <asp:BoundField DataField="nome" HeaderText="Gênero" SortExpression="nome" > 
      <ControlStyle Width="200px" /> 
      <ItemStyle Width="200px" /> 
      </asp:BoundField> 
      <asp:TemplateField AccessibleHeaderText="Check"> 
       <ItemTemplate> 
        <asp:CheckBox ID="checkGenero" runat="server" /> 
       </ItemTemplate> 
      </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

,這裏是我的C#代碼

DataSet dsDivided; 

protected void Page_Load(object sender, EventArgs e) 
{ 
    Music musicbll = new Music(); 
    DataSet dsGeneros = musicbll.getGenders(); 

    int size = dsGeneros.Tables[0].Rows.Count; 
    dsDivided = null; 

     // Divide in two DataTable 
     dsDivided = Tools.SplitDataTableInTwo((DataTable)dsGeneros.Tables[0], size/2); 

    gvGeneros1.DataSource = dsDivided.Tables["FirstSet"]; 
    gvGeneros1.DataBind(); 

    for (int i = 0; i < gvGeneros1.Rows.Count; i++) 
    { 
     ((CheckBox)gvGeneros1.Rows[i].Cells[1].Controls[1]).Checked=false; 
    } 

} 


protected void btGravarPrefs_Click(object sender, EventArgs e) 
{ 
    DataTable dt = new DataTable("generos"); 
    dt.Columns.Add("idgenero", typeof(int)); 
    dt.Columns.Add("active", typeof(bool)); 

    for (int i = 0; i < gvGeneros1.Rows.Count; i++) 
    { 
     int idCliente = (int)dsDivided.Tables[0].Rows[i][0]; 
     bool check = ((CheckBox)gvGeneros1.Rows[i].Cells[1].Controls[1]).Checked; //always false 
     dt.Rows.Add(new object[] { idCliente, check}); 
    } 
} 

}

我不知道該怎麼嘗試更多,我尋遍,它似乎是正確的。由於

+0

當你的頁面加載設置了'Checked'值。您選擇了CheckBox後,是否驗證過這個事件沒有發生? –

回答

12

我想你應該換你Data bindingif (!Page.IsPostBack)

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     //Bind your datasource here 
    } 
} 
+0

它像一個魅力。反應非常快,非常感謝。 – Zumbicap