Solved

Dynamica checkboxes and how to determine checked

Posted on 2007-04-04
6
856 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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