Passing a value from page to usercontrol
-
-
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
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.
-
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.
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.
-
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.
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 anid
ofmyUserControl1
) - 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.
-
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 anid
ofmyUserControl1
) - 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.
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
-
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
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 hasid
=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 -
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 hasid
=myLabel
:Private _countryVal as String
Public Property CountryVal as String
Get
return _countryVal
End Get
Set
_countryVal = value
myLabel.Text = _countryVal
End Set
End PropertyMike, 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.:)