Solved

Dynamica checkboxes and how to determine checked

Posted on 2007-04-04
6
848 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:nbbit
Comment Utility
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
Comment Utility
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
Comment Utility
balochdude - close enough!  you get 5 big 'uns.
take care!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

9 Experts available now in Live!

Get 1:1 Help Now