Solved

Dynamica checkboxes and how to determine checked

Posted on 2007-04-04
6
859 Views
Last Modified: 2008-02-20
Creating an ASP.Net 1.1 page in VS.net 2003


Here's what I am doing.  I get a view of my dataset (Names, Addresses, etc).
Then I  loop through each record in order to create a check box for that user and their email addy.  This will give the end users a list of names that they can choose from in order to send their email to, check to send this person an email, uncheck not to send them the email.  
Looks a little like this

To: Joe Smith [x]
To: Sanjay Patel [x]
To: Kris Karasek [x]
To: Anna Smith [x]


for(int c=_NumberOfRecipients-1; c>-1; --c)
{
      //each row
      dr = dv.Table.Rows[c];
      //create a check box
      CheckBox chkTo = new CheckBox();
      chkTo.TextAlign= TextAlign.Left;
      chkTo.ID = dr["EmailAddress"].ToString();
      chkTo.ToolTip = dr["EmailAddress"].ToString();
      _names = "To: " + dr["FirstName"].ToString() + " " + dr["LastName"].ToString() + " ";
      chkTo.Text = _names;
      chkTo.Checked = true;
      //add the chk box to the contact panel
      iContact.Controls.Add(chkTo);
      iContact.Controls.Add(new HtmlGenericControl("BR"));
      //empty before next go round
      _names = string.Empty;
}


then on the SUBMIT button, when the user goes to send the email, i try to loop through the check boxes (created above) in the panel, to see which check boxes are checked and which are not in order to build a TO: string of email addresses, it fails to find any check boxes.

foreach (System.Web.UI.Control c in this.iContact.Controls)
{
    if ((c.GetType() == typeof(CheckBox)))
    {
        // is it a checkbox?            
        CheckBox chk = (CheckBox)c;
        if (chk.Checked == true)
        {
            //build a TO string for emailing members
          _EmailAddressesTo += chk.ID;
        }
    }
}//end for

Any help on how to get the second loop to actually access each dynamically created checkbox would be appreciated:-)

Thanks!
0
Comment
Question by:nbbit
  • 4
  • 2
6 Comments
 
LVL 2

Expert Comment

by:balochdude
ID: 18850709
Any dynamically created controls have to be created again on postback. Thats why you cant see any checkboxes because you are not creating them again.
0
 

Author Comment

by:nbbit
ID: 18850741
balochdude: *ugh* yep, yer right

then i guess my question becomes, what is the best approach to save each checkbox and its state between posts?
0
 
LVL 2

Accepted Solution

by:
balochdude earned 500 total points
ID: 18850903
add info to an array and either keep this info in session or viewstate.

    protected void PlaceHolder1_PreRender(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            mycontrolsarray = (mycontrolsarrayayList)Session["mycontrols"];
            if (mycontrolsarray != null)
            {
                if (mycontrolsarray[intCounter] is CheckBox)
                {
                    CheckBox chk = (CheckBox)mycontrolsarray[intCounter];
                    if (Request.Form[chk.UniqueID] != null)
                    {
                        chk.Checked = true;
                    }
                    else
                    {
                        chk.Checked = false;
                    }
                    ((PlaceHolder)this.GridView1.Rows[intCounter].Cells[1].FindControl("ph1")).Controls.Add(chk);
                }
                intCounter++;
            }
        }

    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {

        Session.Add("mycontrols", mycontrolsarray);

    }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nbbit
ID: 18850991
thanks for the suggestion, i'll start working to integrate it and get back to you and let you know if it works or not.  

may take me a bit, easter vacation is upon us, will try to get back to you as soon as i return
0
 

Author Comment

by:nbbit
ID: 18857414
actually, looking more closely at this, I cannot follow your example because I am in .Net 1.1 NOT 2.0+

Is there a 1.1 solution that will not only preserve the checkboxes and their names, but their state (checked or not) between posts??

TIA!
0
 

Author Comment

by:nbbit
ID: 18883800
balochdude - close enough!  you get 5 big 'uns.
take care!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question