1
Answer

Button sends false for dynamic checkboxes after 2nd postback

Ask a question
Usman Raza

Usman Raza

10y
776
1

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:

// 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 < ds.Tables[0].Rows.Count; 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 < fields.Length; 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 = @"<img src=" + imgsource + " 'width=16' 'height=16' alt='published/unpublished'";
                        row.Cells.Add(cell);
                    }
                    else if (j == 5)
                    {

                        cell.InnerText = String.Format("{0:d}", fields[j]);
                        row.Cells.Add(cell);
                    }

                    else if (j == 6)
                    {
                        cell.InnerText = String.Format("{0:d}", fields[j]);
                        HtmlTableCell checkboxes = new HtmlTableCell();

                        checkboxes.Align = "Right";
                        HtmlTableCell actions = new HtmlTableCell();
                        CheckBox chkactionbox = new CheckBox();
                        chkactionbox.EnableViewState = true;
                        checkboxes.Controls.Add(chkactionbox);

                        actions.InnerHtml = @"<a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=pu'><img src='tick-circle.gif'  'width=16' 'height=16' alt='published' /></a><a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=upu'><img src='minus-circle.gif'  width='16' height='16' alt='not published' /></a><a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=edt'><img src='pencil.gif'  width='16' height='16' alt='edit' /></a><a href='ManageArticles.aspx?id=" + fields[0].ToString() + "&event=del'><img src='bin.gif'  width='16' height='16' alt='delete' /></a>";

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

        }


Answers (1)