2

我是 .NET 的新手,我的复选框有问题。即使它们被选中,它们都返回 false。这是我的asp代码

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

}

我不知道该尝试更多,我到处搜索,似乎是正确的。谢谢

4

1 回答 1

13

我认为你应该Data bindingif (!Page.IsPostBack)

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    { 
       //Bind your datasource here
    }
}
于 2012-11-28T14:33:16.820 回答