Results 1 to 8 of 8

Thread: Need help with WebUserControl

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    Need help with WebUserControl

    Hello.

    I'm having some trouble with webusercontrols. I've created one with a checkbox and a textbox. Now, I want to create three of these when my page loads. I'm using this code for that (I'm also using a placeholder):
    Code:
    WebUserAlternative uc1;
    
    for (int i = 0; i < 3; i++)
    	{
                      
                uc1 = (WebUserAlternative)Page.LoadControl("~/WebUserAlternative.ascx");
                uc1.ID = "nr" + i;
                PlaceHolder1.Controls.Add(uc1);
                 
            }
    Now to my problem.
    How do I check the text that has been written in each of those textboxes (the textbox inside the webusercontrol) and if the checkboxes are checked?
    I know how to do this when I "pre-create" (not on the fly) one webusercontrol, but I can't figure out how to get the text from all the textboxes etc that are created on page_load.

    Damn, that was messy Anyway.. I hope you guys understand my problem.

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Need help with WebUserControl

    Have you created properties in your usercontrol to access these values already?

    Properties in usercontrol...
    Code:
        public string EmailAddress
        {
            get { return this.txtEmail.Text.Trim(); }
            set { this.txtEmail.Text = value; }
        }
    To access dynamic controls at runtime
    Code:
                foreach (Control c in this.placeholder.Controls)
                {
                    if (c.GetType().ToString() == "ASP.mycontrol_ascx")
                    {
                        myString = ((mycontrol)c).EmailAddress;
                     }
                }
    Hope that helps.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    Re: Need help with WebUserControl

    Worked great! Thanks!

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    Re: Need help with WebUserControl

    Btw... I tried to put this code in a dropDownList_SelectedIndexChanged method. But that doesn't work. It only seems to work when I put it in page_load.
    Does anyone know how to fix that?
    It's not that important... I'm just curious as to why it doesn't work.

  5. #5
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: Need help with WebUserControl

    Because selecting an index of DropDownList doesn't fire the event. Place a Breakpoint inside dropDownList_SelectedIndexChanged method, run the application and change its index.

    Change the AutoPostBack property of dropDownList to True, it will give you the desired result.
    Show Appreciation. Rate Posts.

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    33

    Re: Need help with WebUserControl

    Thanks for answering, but I've already tried that. Doesn't work (at least for me it doesn't). Oh well.. I'll play around with it some more and see what I can come up with.

  7. #7
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Need help with WebUserControl

    Quote Originally Posted by korven
    Btw... I tried to put this code in a dropDownList_SelectedIndexChanged method. But that doesn't work. It only seems to work when I put it in page_load.
    Does anyone know how to fix that?
    It's not that important... I'm just curious as to why it doesn't work.
    What do you mean exactly when you say it doesn't work?

    Are you re-creating your controls on each page load?

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Need help with WebUserControl

    Does the SelectedIndexChanged event actually get hit?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width