Solved

Dynamica checkboxes and how to determine checked

Posted on 2007-04-04
6
850 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now