C# 无法选中GridView中的复选框状态

C# 无法选中GridView中的复选框状态,c#,asp.net,sql-server-2008,C#,Asp.net,Sql Server 2008,以下是我的代码 受保护的无效按钮3\u单击(对象发送者,事件参数e) { fillgrid() ASPX: abc()方法只是填充了列表。我希望上下文现在更清楚了。为什么要检查值==true?您正在fillGrid中创建它们,因为您将数据绑定到它的数据源。因此,如果您注释掉fillGrid(),它们可能会被检查 从aspx标记和Buttton3的Text中,我看到您希望将其用于插入(使用更好的ID)。然后我不知道为什么要在插入任何内容之前重新加载数据源。只需将fillgrid移到末尾(插入

以下是我的代码

受保护的无效按钮3\u单击(对象发送者,事件参数e) { fillgrid()

ASPX:




abc()方法只是填充了列表。我希望上下文现在更清楚了。

为什么要检查
值==true
?您正在
fillGrid
中创建它们,因为您将
数据绑定到它的
数据源
。因此,如果您注释掉
fillGrid()
,它们可能会被检查

从aspx标记和
Buttton3
Text
中,我看到您希望将其用于插入(使用更好的ID)。然后我不知道为什么要在插入任何内容之前重新加载数据源。只需将
fillgrid
移到末尾(插入后重新加载网格)

protectedvoid按钮3\u单击(对象发送者,事件参数e)
{ 

//fillgrid();为什么要选中它们==true
?我假设您是在
fillgrid
中创建它们的,因为您将它
数据绑定到那里。因此,当您注释掉
fillgrid()
时,它们可能会被选中(取决于上下文)。我正在从fillgrid()填充这个网格视图。然后检查行。我是否需要在不使用fillgrid()的情况下单击按钮填充网格?我没有绑定到Pageload。我发布了更多代码,请检查我的编辑。@TimSchmelter我想我知道我做错了什么。我稍后会回来。
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            Label lblteachername = (Label)GridView1.Rows[i].FindControl("lblgridteachername");
            CheckBox status = (CheckBox)GridView1.Rows[i].FindControl("chkgridstatus");
            if (status.Checked == true)
            {    
                string q = "insert into teacher (status) values('"+dayList[i].Date+"') where schid='"+dayList[i].SchId+"'";    
            }                
        }
    }
public void fillgrid()
    {
        string q = "select * from teacher where teachername='" + drpteachername.SelectedItem.ToString() + "'  and ('2013-03-01' between date and todate) and '2013-03-31' between date and todate";
        dt = dbo.Getdt(q);
        //NOTE- if you bind gridview with dt, gridview will automatically generate no. of rows equal to the no. of rows returned to dt through the sql query from database.

        abc();
        DataTable dt1 = new DataTable();
        for (int i = 0; i < dayList.Count; i++)
        {
            DataRow dr = dt1.NewRow();
            dt1.Rows.Add(dr);
        }

        GridView1.DataSource = dt1;
        GridView1.DataBind();
        string teachername = drpteachername.SelectedItem.ToString();
        string month = drpmonth.SelectedItem.ToString();
        string strclass = drpclass1.SelectedItem.ToString();
        string section = drpsection1.SelectedItem.ToString();

        //string time=drpstarttime1.SelectedItem.ToString();
        for (int i = 0; i < dayList.Count; i++)
        {

            Label lbldate = (Label)GridView1.Rows[i].FindControl("lblgriddate");
            string fullstring = Convert.ToString(dayList[i].Date);
            lbldate.Text = fullstring.Substring(0, 9);

            //string q1 = "select starttime,endtime from teacher where teachername='"+drpteachername+"', and '"+fullstring+"' between date and todate";
            //dt = dbo.Getdt(q1);
            //int count=dt.Rows.Count;

            Label lblteachername = (Label)GridView1.Rows[i].FindControl("lblgridteachername");
            lblteachername.Text = teachername;

            Label lblclass = (Label)GridView1.Rows[i].FindControl("lblgridclass");
            lblclass.Text = dayList[i].Class;

            Label lblsection = (Label)GridView1.Rows[i].FindControl("lblgridsection");
            lblsection.Text = dayList[i].Section;

            Label lbltime = (Label)GridView1.Rows[i].FindControl("lblgridtime");
            lbltime.Text = dayList[i].StartTime.Substring(0, 5) + "-" + dayList[i].EndTime.Substring(0, 5);


        }
    }
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                        <Columns>
                            <asp:TemplateField HeaderText="TeacherName">
                                <ItemTemplate>
                                    <asp:Label ID="lblgridteachername" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Class">
                                <ItemTemplate>
                                    <asp:Label ID="lblgridclass" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Section">
                                <ItemTemplate>
                                    <asp:Label ID="lblgridsection" runat="server" Text="Label"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Date">
                                <ItemTemplate>
                                    <asp:Label ID="lblgriddate" runat="server"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Time">
                                <ItemTemplate>
                                    <asp:Label ID="lblgridtime" runat="server" Text="Label"></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Present">
                                <ItemTemplate>
                                    <asp:CheckBox ID="chkgridstatus" runat="server" />
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
    <br />
                    <asp:Button ID="Button3" runat="server" Text="Insert" onclick="Button3_Click" />
                </td>
protected void Button3_Click(object sender, EventArgs e) 
{ 
    //  fillgrid();   <-------------------- from here

    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        Label lblteachername = (Label)GridView1.Rows[i].FindControl("lblgridteachername");
        CheckBox status = (CheckBox)GridView1.Rows[i].FindControl("chkgridstatus");
        if (status.Checked == true)
        {    
            string q = "insert into teacher (status) values('"+dayList[i].Date+"') where schid='"+dayList[i].SchId+"'";    
        }                
    }

    fillgrid();    // <-------------------- to  here
}