4
Answers

Retrieve information from a dynamically created control (checkbox)

Daniel Scalfaro

Daniel Scalfaro

17y
4.1k
1

I'm looking for assistance with overcoming an issue I have been having with an asp.net web application for the past 6 weeks.  Any assistance would be tremendously beneficial. 

 

The web application I’m working on will ultimately reset Sybase accounts across 30+ database instances. The current layout of the application is as follows:

 

  • User prompted for Sybase account
  • Goes through list of databases and check if user as access to the instance
  • If user has access; application builds a panel of dynamically created checkbox.

Up to this point I have working.  Below is what I need help on.

 

  • Once the methods above loops through the db’s and builds the checkbox; I need create the ability for a user to select one or many of the dynamically created checkbox and pass the value, id, name or any identify element of the control to a reset password method.

 

Below is the code I’m working with that does not work:

 

foreach (object o in envPnl2.Controls)

        {

            CheckBox chk = o as CheckBox;

            if (chk == null)

                continue;

           

            if (chk.Checked)

            {

                resetPassword(chk.Text);

            }

        }

 

Below is the code I’m using to create the checkbox and add them to a table (for alignment):

 

  void createDynChks(int c, string db)

    {

        TableRow tr = new TableRow();

        // Create column 1 

        TableCell td1 = new TableCell();

        // Create a checkbox control dynamically 

        CheckBox _checkbox = new CheckBox();

        _checkbox.ID = db;

        _checkbox.Text = db;

 

 

        if ((c == 2) || (c == 4))

        {

            _checkbox.Enabled = false;

        }

 

        // Add control to the table cell 

        td1.Controls.Add(_checkbox);

        // Add cell to the row 

        tr.Cells.Add(td1);

        tr.HorizontalAlign = HorizontalAlign.Left;

        // Add row to the table. 

        if (cntclmn == 1)

        {

            tblDyn1.Rows.Add(tr);

        }

        if (cntclmn == 2)

        {

            tblDyn2.Rows.Add(tr);

        }

        if (cntclmn == 3)

        {

            tblDyn3.Rows.Add(tr);

        }

        if (cntclmn == 4)

        {

            tblDyn4.Rows.Add(tr);

        }

        if (cntclmn == 5)

        {

            tblDyn5.Rows.Add(tr);

        }

    }

 

Please help if anyone can offer a direction.

-Dan

Answers (4)