Click here to Skip to main content
15,885,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys

I have two asp table. I create in the first table some checkboxes according to database programmatically and the second table also I add checkboxes according to checkboxes that selected from the first table, when I click button all of checkboxes disappear with its data.

C#
void Page_Load(object sender, ArgEvent e)
{
          DataView DV = Db.Qury("Select Id, Name from tblInputs");
          for(int i = 0; i < DV.Count; i++)
          {
               Table1.Rows.Add(new TableRow());
               TableCell cell = new TableCell();
               CheckBox chkbox = new ChackBox();
               chkbox.ID = DV[i][0].ToString();
               chkbox.Text = DV[i][1].ToString();
               cell.Controls.Add(chkbox);
               Table1.Rows[Table1.Rows.Count - 1].Cell.Add(cell);
           }
               

}
void Button_Click(object sender, ArgEvent e)
{
       for(int i = 0; i < Table1.Rows.Count; i++)
          {
               foreach(Control item in Table1.Rows[i].Controls)
               {
                     Checkbox chk = (Checkbox)item[0];
                     if(chk.Checked)
                     {
                            DataView DV = Db.Qury("Select Id, Name from Tranfer where InputId = " + chk.ID);
                          for(int i = 0; i < DV.Count; i++)
                         {
                                Table2.Rows.Add(new TableRow());
                                TableCell cell = new TableCell();
                                RadioButton Rdbtn = new RadioButton();
                                Rdbtn.ID = DV[i][0].ToString();
                                Rdbtn.Text = DV[i][1].ToString();
                                cell.Controls.Add(Rdbtn);
                               Table2.Rows[Table2.Rows.Count - 1].Cell.Add(cell);
                         }


                     }
               }
               
           }
}



The question: How can I save selected checkboxes and appear them after postback?


Sorry for English
Posted
Comments
Samira Radwan 4-Sep-15 11:42am    
store your table in a session. When update the session write it back to your table.
Ammar Al-hamdabni 4-Sep-15 12:01pm    
I tried that in button click but doesn't work because on page loading clear them before click
Samira Radwan 4-Sep-15 12:21pm    
sure it will! add your session on page load between if (!IsPostBack). Then assign the session to DataTable on button click. Once updated assign it back to the session.
Ammar Al-hamdabni 4-Sep-15 13:46pm    
Can you alter the code as you said
Abdul Samad KP 4-Sep-15 19:23pm    
You are right , since the controls are generated at runtime , you have to place your code outside the if(!IsPoastBack) block, but in that case your code should work properly.
So I am deleting my answer.
I guess the code you have posted is not the original one, because there are many errors in that.

1 solution

As Abdul Samad KP answered in page_Load
Then add your table to asession
C#
void Page_Load(object sender, ArgEvent e)
{
     if (!IsPostBack)
 
     {
          DataView DV = Db.Qury("Select Id, Name from tblInputs");
          for(int i = 0; i < DV.Count; i++)
          {
               Table1.Rows.Add(new TableRow());
               TableCell cell = new TableCell();
               CheckBox chkbox = new ChackBox();
               chkbox.ID = DV[i][0].ToString();
               chkbox.Text = DV[i][1].ToString();
               cell.Controls.Add(chkbox);
               Table1.Rows[Table1.Rows.Count - 1].Cell.Add(cell);
           }
Session["mytb"]=Table1;
        }       
 
}

protected void btn_Submit_Click(object sender, EventArgs e)
{
//Here assign DataTable to the Session, modify/update the DataTable
DataTable mytb = (DataTable)Session["mytb"];  
//after updating/modifying datatable assign it back to your table. Something like:
Table1=mytb;
}


I didn't test the code but the logic should be good
Hope it helps!

Regard,
Samira
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900