Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Dynamica checkboxes and how to determine checked

Posted on 2007-04-04
6
Medium Priority
?
881 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 1500 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

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!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

971 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