Skip to content

Button sends false for dynamic checkboxes after 2nd postback

Usman Raza

Asked Jul 7, 2014, 5:53 PM EDT1 answer
Share
LinkedInX

I have one dynamically generated html table in my web application.One of its column list out asp checkboxes controls. I want to select multiple rows(by checkboxex)to publish,unpublish or delete my articles. It successfully publishes or unpublishes for the first postback of asp button. But when i click for the second time it sends false for all the checkboxex even some of them are selected.So I expect it must work for the second postback if it works for the first time because it had successfully passed all the page life cycle for the first time then why not for the second time..I am really confused.Please help me
 Here is my code:

C#
// For dynamically generated controls i used oninit state

         protected override void OnInit(EventArgs e)
         {
             base.OnInit(e);

             if (!this.DesignMode)
             {
                 ds = GetArticles();

                 FillArticleTable();
             }
         }
 // Code for page Load

         protected void Page_Load(object sender, EventArgs e)
         {
             if (!IsPostBack)
             {
                 if (Request.QueryString["id"] != null)
                 {
                     string eventdata = Request.QueryString["event"];
                     int id = Convert.ToInt32(Request.QueryString["id"]);
                     if (eventdata != "edt")
                     {
                         Actions(id, eventdata);
                     }
                     else
                     {
                         Response.Redirect("Articles.aspx?id=" + id + "");
                     }

                 }

                 else
                 {
                     ds = GetArticles();
                     Cache["Articles"] = ds;

                 }

             }

         }

  // Code for Button Click event

         protected void btnApplyAction_Click(object sender, EventArgs e)
         {
             if (this.ddlActions.SelectedValue != "none")
             {
                 IterateOverHtmlTable(this.ddlActions.SelectedValue);
                 this.myTable.Rows.Clear();
                 ds = GetArticles();
                 FillArticleTable();
             }
         }
 // Code For retrieving Articles from database

         private DataSet GetArticles()
         {
             DataSet ds = new DataSet();
             SqlConnection con = new SqlConnection(@"Data Source = xenz-pc\sqlexpress; Initial Catalog =MedicalSystem2; User Id =sa; Password =sa;");
             SqlCommand com = new SqlCommand("Select A.ID,A.ArticleTitle,A.Author,C.Category, A.Published,A.CreatedON,A.ModifiedON  from Articles as A Inner Join Category As C On A.Category_ID = C.ID ", con);
             con.Open();
             SqlDataAdapter adp = new SqlDataAdapter();
             adp.SelectCommand = com;
             adp.Fill(ds);
             con.Close();
             return ds;
         }

 //Code For Dynamically generating html table

         private void FillArticleTable()
         {

             int rows = 0;
             string imgsource;

             for (int i = 0; i              {
                 HtmlTableRow row = new HtmlTableRow();

                 rows = rows + 1;

                 Object[] fields = ds.Tables[0].Rows[i].ItemArray;

                 if (Convert.ToBoolean(fields[4]) == false)
                 {
                     imgsource = "minus-circle.gif";

                 }
                 else
                 {
                     imgsource = "tick-circle.gif";
                 }

                 for (int j = 0; j                  {
                     HtmlTableCell cell = new HtmlTableCell();

                     if (j == 0)
                     {
                         HtmlTableCell srno = new HtmlTableCell();
                         srno.InnerText = rows.ToString();
                         cell.InnerText = fields[j].ToString();
                         row.Cells.Add(srno);
                         row.Cells.Add(cell);
                     }
                     else if (j == 1)
                     {
                         cell.InnerText = fields[j].ToString();
                         row.Cells.Add(cell);
                     }
                     else if (j == 2)
                     {
                         cell.InnerText = fields[j].ToString();
                         row.Cells.Add(cell);
                     }
                     else if (j == 3)
                     {
                         cell.InnerText = fields[j].ToString();
                         row.Cells.Add(cell);
                     }
                     else if (j == 4)
                     {
                         cell.InnerHtml = @"";

                         row.Cells.Add(cell);
                         row.Cells.Add(checkboxes);
                         row.Cells.Add(actions);
                     }


                 }
                 this.myTable.Rows.Add(row);

             }

         }

 //Code For Finding out checkboxes which are selected by user

         private void IterateOverHtmlTable(string eventdata)
         {

             HtmlTable table = (HtmlTable)Page.FindControl("myTable");



             int count = 0;


             foreach (HtmlTableRow row in table.Rows)
             {
                 count = count + 1;

                 if (count > 1)
                 {
                     foreach (CheckBox item in row.Cells[8].Controls)
                     {
                         if (item.Checked)
                         {



                             Actions(Convert.ToInt32(row.Cells[1].InnerText), eventdata);
                         }
                     }

                 }
             }

         }

 // Code For Publish,Unpublish and Delete Actions

         private void Actions(int id, string eventdata)
         {
             if (Connection != null)
             {
                 if (Connection.State != ConnectionState.Open)
                 {
                     Connection.Open();
                 }
             }
             else
             {
                 Connection = new SqlConnection(@"Data Source = xenz-pc\sqlexpress; Initial Catalog =MedicalSystem2; User Id =sa; Password =sa;");
                 if (Connection.State != ConnectionState.Open)
                 {
                     Connection.Open();
                 }
             }

             SqlCommand com = new SqlCommand();
             int rows;

             if (eventdata == "pu")
             {
                 com.CommandText = "Update Articles Set Published = 'true' Where ID = " + id;
                 com.CommandType = CommandType.Text;
                 com.Connection = Connection;
                 rows = com.ExecuteNonQuery();
                 Connection.Close();
                 if (rows > 0)
                 {



                     this.Publish.InnerText = "Published";
                 }
             }
             else if (eventdata == "upu")
             {
                 com.CommandText = "Update Articles Set Published = 'false' Where ID = " + id;
                 com.CommandType = CommandType.Text;
                 com.Connection = Connection;
                 rows = com.ExecuteNonQuery();
                 Connection.Close();
                 if (rows > 0)
                 {


                     this.Unpublish.InnerText = "Unpublished";
                 }
             }

             else
             {
                 com.CommandText = "Delete From Articles Where ID =" + id;
                 com.CommandType = CommandType.Text;
                 com.Connection = Connection;
                 rows = com.ExecuteNonQuery();
                 Connection.Close();
                 if (rows > 0)
                 {


                     this.Publish.InnerText = "Deleted";
                 }
             }

         }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Guyz I finally found a solution for second time postback issue.
    I just added following one line just after the Instantiation of checkbox in the else if(j == 6) block of FillArticleTable() function and it worked. 

    chkactionbox.ID = "chk_" + rows.ToString(); 
    0