2

I am kind of new to C# and Asp.Net, so this question might sound repetitive but I am not able to find a solution for this particular problem.

I have two buttons on my HTML page and a single class in .cs file. On one of the button clicks, I create a table programmatically (dynamically). The table contains some checkboxes which are also created dynamically. Table creation is one of the last tasks that I perform. Before that, I read several files and extract data from them to create the table. After the table is drawn, the user can select one or more checkboxes.

Now, how on second button click, can I know that which of the checkboxes were checked before the page reload? Currently I have made all these checkboxes member variables of the the only class that I have in the .cs file.

I tried adding checkbox event handler through C# code. But the handler is not getting called when the checkbox is checked. I don't want to set the 'autopostback' property of the checkbox to true since if thats set true, the page reloads after checking one of the checkboxes. User should be able to select multiple checkboxes.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
user1465266
  • 621
  • 1
  • 6
  • 12
  • What is code you wrote for the checkbox event handler? Maybe I had similar problem to yours some time ago..Post your code. – Alberto Solano Jun 19 '12 at 21:33
  • 1
    Please see this answer: http://stackoverflow.com/questions/7495486/button-click-event-not-firing-within-use-control-in-asp-net/7495550#7495550 – Valamas Jun 19 '12 at 21:34

1 Answers1

0

Add your checkboxes dynamically and set a unique name for each checkbox. Checkboxes are only posted back to the server if the checkbox is checked, so you can test to see if it is checked by checking Request.Form to see if the name exists. For example, lets say you named your check boxes chk_[0-9] (i.e. chk_0, chk_1 etc till 9), you could check if they ticked by doing:

for(int i=0; i < 10; i++)
{
    string chk_name = "chk_" + i.ToString();
    if (Request.Form[chk_name] != null)
    {
        //checkbox is checked
    }
    else
    {
        //checkbox is not checked
    }
}
BlackSpy
  • 5,563
  • 5
  • 29
  • 38