1

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

6
  • have you taken checkboxfield type column or an itemtemplate column?
    – Sid M
    Sep 25, 2013 at 9:27
  • 1
    can you share aspx and code behind! Sep 25, 2013 at 9:34
  • I used template colmn
    – chathura
    Sep 25, 2013 at 9:44
  • what's in the Page_Load? Sep 25, 2013 at 10:19
  • how you check the checkboxes the first time? Sep 25, 2013 at 10:29

2 Answers 2

5

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
}
7
  • are you getting any error or have you debugged your code,is it getting inside the if block?
    – Sid M
    Sep 25, 2013 at 10:11
  • im not getting any error. if(chk.Checked) is true (even though I unchecked it). But when I add a new subject it works fine.
    – chathura
    Sep 25, 2013 at 10:16
  • enclose your code starting from for (int i = 0; i < courses.Count; i++) in if(!Page.IsPostBack){ //that code }
    – Sid M
    Sep 25, 2013 at 11:33
  • then the checkboxes are not checked.
    – chathura
    Sep 25, 2013 at 11:49
  • what i'm saying is include your code for checking the checkbox inside if(!Page.IsPostBack){ //that code } ,your checkboxes will be checked for the first time after that if any postback is done by your update button it won't check all your checkboxes.
    – Sid M
    Sep 25, 2013 at 11:57
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.