0

I have a four column table with a variable amount of rows, which needs to support selection of single or multiple rows. I have decided to use CheckBoxes in one of the columns to implement this functionality but I cannot find a way of reading the state of the controls after they have been generated.

First off, I started using JQuery to slip the CheckBoxes straight into the table on the client side, assuming I could just use "FindControl()" to look it up when the form was posted. FindControl failed miserably even after adapting it to search all controls on the page recursively.

After some googling it appeared that ASP.Net simply doesn't allow or function as I had hoped and tried so instead I changed the code to add all the CheckBoxes to a Table in the code file and there was no difference.

As I am much more familiar with using the C# HttpListener, I attempted to read back the posted form data and process it myself, only to find that ASP uses some form of either encryption or obfuscation on the data rendering it a more time consuming task that its worth to even look at that method.

There is almost no restriction to how I achieve this but I simply don't know enough about ASP.Net to work this out alone, especially after google has failed me!

If you need to see some code feel free to ask, but please be specific.

This is the code I have TRIED to use to find a control, its a right mess at the moment:

// I assign the name "CHK_SEL_" + i.ToString() to the ID, but that is on a page with a master page. Examining the ID with chrome shows that the ClientID is actually MainContent_CHK_SEL_i, so I search for that too.
Control c = MyFind(this, "CHK_SEL_2");
ControlCollection cc = this.Controls; // Everything except this line, results in null. and after examining this using visual studio, I cannot manually find the controls I am after either.
Control ccc = this.FindControl("CHK_SEL_2");
Control cccc = SearchControl(this, "CHK_SEL_2");
Control ccccc = MyFind(this, "MainContent_CHK_SEL_2");
Control cccccc = this.FindControl("MainContent_CHK_SEL_2");
Control ccccccc = SearchControl(this, "MainContent_CHK_SEL_2");

private Control MyFind(Control search, string ID)
{
    int i = 0;
    Control c = search.FindControl(ID);
    if (c != null) return c;
    while (search.Controls.Count > i && (c = MyFind(search.Controls[i++], ID)) == null) ;
    return c;
}

public static Control SearchControl(Control controlToSearch, string controlID)
{
    if (controlToSearch.ID == controlID) return controlToSearch;
    for (int i = 0; i < controlToSearch.Controls.Count; i++)
    {
        Control c = SearchControl(controlToSearch.Controls[i], controlID);
        if (c != null) return c;
    }
    return null;
}
2
  • Can you please provide the code segment, it seems as FindControl works in such cases,but i'll have to see what have you implemented before making any suggestion. Commented Jun 4, 2013 at 10:06
  • I have updated my post with some code, if there is anything else you would like/need to see, just ask. Commented Jun 4, 2013 at 10:27

1 Answer 1

1

The simplest way you can find the status of your checkboxes (checked or unchecked) is as follow:

        Table tbl = (Table)form1.FindControl("Table1");

        int rows = tbl.Rows.Count;
        for (int i=0; i<rows;i++)
        {
            TableRow tr = tbl.Rows[i];
            TableCell tc = tr.Cells[0];

            CheckBox cb = (CheckBox)tc.Controls[0];
            bool status = cb.Checked;
        }
Sign up to request clarification or add additional context in comments.

2 Comments

While I appreciate an answer, this did not solve the problem at all. I have decided to instead use Ajax and Sessions as a workaround. Thanks for the input though!
Don't you think working with session for this task will make the page unnecessarily loaded...

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.