Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Passing a value from page to usercontrol

Passing a value from page to usercontrol

Scheduled Pinned Locked Moved ASP.NET
questiontutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jetset32
    wrote on last edited by
    #1

    hi, I have a web page with a user control on it, the page has a drop down list on it and i would like to pass the value of what is chosen on the drop down list into the usercontrol. I havnt a clue how to do this?? any ideas?? Thanks in advance! Jetset

    M 1 Reply Last reply
    0
    • J jetset32

      hi, I have a web page with a user control on it, the page has a drop down list on it and i would like to pass the value of what is chosen on the drop down list into the usercontrol. I havnt a clue how to do this?? any ideas?? Thanks in advance! Jetset

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi there. I would recommend exposing a property from within your UserControl that you can set from the context of the page. You could then set this property equal to the value from the dropDownList pretty easily.

      J 1 Reply Last reply
      0
      • M Mike Ellison

        Hi there. I would recommend exposing a property from within your UserControl that you can set from the context of the page. You could then set this property equal to the value from the dropDownList pretty easily.

        J Offline
        J Offline
        jetset32
        wrote on last edited by
        #3

        hi, I have never exposed a propety before, could you give an example of how to expose properties in a user control? Do you mean by set from the context of the page to use cookies? If this is correct then I think i know how to do that. but could you just clarify. Thanks for your help again. Jetset.

        M 1 Reply Last reply
        0
        • J jetset32

          hi, I have never exposed a propety before, could you give an example of how to expose properties in a user control? Do you mean by set from the context of the page to use cookies? If this is correct then I think i know how to do that. but could you just clarify. Thanks for your help again. Jetset.

          M Offline
          M Offline
          Mike Ellison
          wrote on last edited by
          #4

          Hi there. I don't necessarily mean to use cookies - if you are passing the value through the queryString, or as a form post, then you'll pull the value from the Request.QueryString or Request.Form collection. However you get the value, you would then have the .aspx page set the property for the .ascx control. Here's an example of the code-behind for a simple .ascx in C# that exposes a property MyProperty:

          class MyUserControl : UserControl
          {
          private string _myProperty;
          public string MyProperty
          {
          get {return _myProperty;}
          set {_myProperty = value;}
          }
          }

          Of course, you would have additional code that affects controls within the UserControl based on the property value... then, let's say in your .aspx page you are passing the value for this property in the URL - something like

          http://localhost/myApp/myPage.aspx?myProperty=1

          Then, say, in the Page_Load event, you could set the property value in your .ascx (let's say the .ascx is on your page with an id of myUserControl1) - it might look like this:

          void Page_Load(object o, EventArgs e)
          {
          // get a reference to the .ascx control with id "myUserControl1"
          Control c = Page.FindControl("myUserControl1");

          // cast it as a MyUserControl object
          MyUserControl u = c as MyUserControl;

          // set its property to the URL parameter "myProperty"
          if (u != null)
          {
          u.MyProperty = Request.QueryString["myProperty"];
          }
          }

          I hope this helps give you some ideas.

          J 1 Reply Last reply
          0
          • M Mike Ellison

            Hi there. I don't necessarily mean to use cookies - if you are passing the value through the queryString, or as a form post, then you'll pull the value from the Request.QueryString or Request.Form collection. However you get the value, you would then have the .aspx page set the property for the .ascx control. Here's an example of the code-behind for a simple .ascx in C# that exposes a property MyProperty:

            class MyUserControl : UserControl
            {
            private string _myProperty;
            public string MyProperty
            {
            get {return _myProperty;}
            set {_myProperty = value;}
            }
            }

            Of course, you would have additional code that affects controls within the UserControl based on the property value... then, let's say in your .aspx page you are passing the value for this property in the URL - something like

            http://localhost/myApp/myPage.aspx?myProperty=1

            Then, say, in the Page_Load event, you could set the property value in your .ascx (let's say the .ascx is on your page with an id of myUserControl1) - it might look like this:

            void Page_Load(object o, EventArgs e)
            {
            // get a reference to the .ascx control with id "myUserControl1"
            Control c = Page.FindControl("myUserControl1");

            // cast it as a MyUserControl object
            MyUserControl u = c as MyUserControl;

            // set its property to the URL parameter "myProperty"
            if (u != null)
            {
            u.MyProperty = Request.QueryString["myProperty"];
            }
            }

            I hope this helps give you some ideas.

            J Offline
            J Offline
            jetset32
            wrote on last edited by
            #5

            Hi, Sorry about this, lets try and get this straight. I have a dropdownlist in a web page which also has a usercontrol. I would like to pass the dropdownlist selecteditem.value to the usercontrol. At the moment I would just like it to display in a label i have in the usercontrol. This is the code I have used so far (by the way im using Visual Basic) I have declared my user control in the web form as below; Public UKAddress1 As UkAddress Public CountryVal As String Then the code used to get the value from the dropdownlist and display it in the Count string as below; CountryVal = Country1.SelectedItem.Value UKAddress1.count = CountryVal In the usercontrol the code below is used to declare the count property as below; Public count As String When I run the code no number is displayed. I've tested the label code by using UKaddress1.count = "hello" And this works, my question would be, why isnt it showing the showselected item.value number? Thanks for your time and help so far by the way....Much appreciated. Jetset

            M 1 Reply Last reply
            0
            • J jetset32

              Hi, Sorry about this, lets try and get this straight. I have a dropdownlist in a web page which also has a usercontrol. I would like to pass the dropdownlist selecteditem.value to the usercontrol. At the moment I would just like it to display in a label i have in the usercontrol. This is the code I have used so far (by the way im using Visual Basic) I have declared my user control in the web form as below; Public UKAddress1 As UkAddress Public CountryVal As String Then the code used to get the value from the dropdownlist and display it in the Count string as below; CountryVal = Country1.SelectedItem.Value UKAddress1.count = CountryVal In the usercontrol the code below is used to declare the count property as below; Public count As String When I run the code no number is displayed. I've tested the label code by using UKaddress1.count = "hello" And this works, my question would be, why isnt it showing the showselected item.value number? Thanks for your time and help so far by the way....Much appreciated. Jetset

              M Offline
              M Offline
              Mike Ellison
              wrote on last edited by
              #6

              In your user control, wouldn't you need to set the label text upon setting the count property you've defined? I'm thinking of something like this in the UserControl (assuming the label within the UserControl has id=myLabel:

              Private _countryVal as String
              Public Property CountryVal as String
              Get
              return _countryVal
              End Get
              Set
              _countryVal = value
              myLabel.Text = _countryVal
              End Set
              End Property

              A 1 Reply Last reply
              0
              • M Mike Ellison

                In your user control, wouldn't you need to set the label text upon setting the count property you've defined? I'm thinking of something like this in the UserControl (assuming the label within the UserControl has id=myLabel:

                Private _countryVal as String
                Public Property CountryVal as String
                Get
                return _countryVal
                End Get
                Set
                _countryVal = value
                myLabel.Text = _countryVal
                End Set
                End Property

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                Mike, Thanks for your help, I figured it out! My code was ok, I havnt needed to use the Public property, Get Set method. I dont know wether its good practice the way I have achieved it...but it works!.. My code was ok but when I was assigning the countryval to the count value I wasnt declaring the value countryval in my code?? I hope this makes sense.. I thought if country val was declared as Public in my code...all sub routines would be able to see that variable? I dont know..anyway it works now.. and it is easy...like you said.. Thanks again for your help Stuart.:)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups