chathura
chathura

Reputation: 3452

How to check the status of a checkbox column in gridview in asp.net

I am trying to implement update subjects function. When I want to add a new subject then it's working. But when I unchecked a checkbox (If I want to remove a existing subject from a program), then it doesn't work.

I debug the program and it showed that the unchecked checkbox is also checked.

enter image description here

eg : If I unchecked IT102 and click the update button, all 3 subjects will be saved in the database.

This is the aspx code

<asp:GridView ID="gridview_modules" runat="server" AutoGenerateColumns="False"          
    GridLines="None">
                        <HeaderStyle Width="30%" />
                        <RowStyle Width="30%" />
                        <FooterStyle Width="30%" />

                        <Columns>
                            <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox runat="server" ID="checkbox_select" />
                            </ItemTemplate>
                            </asp:TemplateField>
                            <asp:BoundField DataField="courseNo"  HeaderStyle-Width="20%" 
                                ItemStyle-Width="10%" FooterStyle-Width="10%" >
                            <FooterStyle Width="10%" />
                            <HeaderStyle Width="20%" />
                            <ItemStyle Width="10%" />
                            </asp:BoundField>
                            <asp:BoundField DataField="title"/>
                        </Columns>
                    </asp:GridView>

This is the code in update button (Inside foreach loop)

  System.Web.UI.WebControls.CheckBox chk =        (System.Web.UI.WebControls.CheckBox)rowItem.Cells[0].FindControl("checkbox_select");
        if (chk.Checked)
        {
            all++; //no of checked subjects when the button is clicked
            if (con.saveCourseForProgram(SiteVariables.ProgramName, rowItem.Cells[1].Text.ToString(), year, sem, SiteVariables.Specialization))
            {
                success++;//try to insert in the db
            }
            else
            {
                //subject that didn't save in the db goes to courseList
                courseList.Add(rowItem.Cells[1].Text.ToString());

            }
        }

Code segment inside page_load

if (!Page.IsPostBack)
    {


        SiteVariables.ProgramName = null;
        SiteVariables.Year = null;
        SiteVariables.Semester = null;
        SiteVariables.Specialization = null;


        if (radioAll.Checked)
        {
            SqlDataSource DataSource2 = new SqlDataSource();
            DataSource2.ID = "SqlDataSource2";
            this.Page.Controls.Add(DataSource2);
            DataSource2.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SEP_Project_NewConnectionString2"].ConnectionString;
            DataSource2.SelectCommand = "SELECT courseNo,title from Course";
            gridview_modules.DataSource = DataSource2;
            gridview_modules.DataBind();
        }
    }

This is how I check checkboxes first time. This code is also inside the page_load.course is a list which has the subjects of a particular program.

 for (int i = 0; i < courses.Count; i++)
        {

            String courseNo = courses[i].Trim();
            //System.Diagnostics.Debug.Print("Course No :"+courseNo+"\n");

            for (int j = 0; j < gridview_modules.Rows.Count; j++)
            {
                //System.Diagnostics.Debug.Print("Row Value = " + gridview_modules.Rows[j].Cells[1].ToString() + "List value = " + courseNo + "\n");
                if (gridview_modules.Rows[j].Cells[1].Text == courseNo)
                {
                    var chk = (System.Web.UI.WebControls.CheckBox)(gridview_modules.Rows[j].Cells[0].FindControl("checkbox_select"));
                    chk.Checked = true;
                }
            }
        }

How to fix this?

Thanks

Upvotes: 1

Views: 16087

Answers (2)

Patricio Moraga
Patricio Moraga

Reputation: 49

In html aspx

<asp:TemplateField ItemStyle-HorizontalAlign="Center">
    <HeaderTemplate><center>TODOS<br /> <asp:CheckBox ID="chk_Todos" runat="server" AutoPostBack="True" OnCheckedChanged="chk_Todos_CheckedChanged"  /></center></HeaderTemplate>
    <ItemTemplate><asp:CheckBox ID="chk_Seleccionar" runat="server"/></ItemTemplate>
</asp:TemplateField>

For aspx vb.net

Protected Sub chk_Todos_CheckedChanged(sender As Object, e As EventArgs)
    Dim Check_All As CheckBox = grid_view.HeaderRow.FindControl("chk_Todos")
    For Each row As GridViewRow In Me.grid_view.Rows
        Dim Check As CheckBox = row.FindControl("chk_Seleccionar")
        If Check_All.Checked = True And Check.Enabled = True Then
            Check.Checked = True
        Else
            Check.Checked = False
        End If
    Next
End Sub

Upvotes: 0

Sid M
Sid M

Reputation: 4364

If you have taken checkboxfield type column then i'ld suggest you to take an itemtemplate column in your grid

<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>

and get the value of checkbox from code behind as follows

foreach(GridViewRow  gvrow in myGrid.Rows)
{
CheckBox chk = (CheckBox)gvrow.FindControl("chkSelect");
if (chk.Checked)
{
//your code here when checkbox is checked
}
else
{
//else part
}

Upvotes: 5

Related Questions