SamuraiJack
SamuraiJack

Reputation: 5539

Unable to check CheckBox state inside GridView

The following is my code...

protected void Button3_Click(object sender, EventArgs e) { fillgrid();

        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+"'";    
            }                
        }
    }

The problem is that if (status.Checked==true) always returns false no matter I check the checkboxes in the girdview or not.

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


        }
    }

ASPX:

<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>

abc() method just fills the List. I hope context is more clear now.

Upvotes: 0

Views: 1126

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460058

Why should they have Checked==true? You are creating them in fillGrid since you DataBind it to it's DataSource. So they might be Checked if you comment out fillgrid().

From the aspx markup and the Text of Buttton3 i see that you want to use it for inserts(use better ID's). Then i don't know why you want to reload the datasource before you have inserted anything. Just move fillgrid to the end (to reload the Grid after you have inserted).

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
}

Upvotes: 0

Related Questions