Reputation: 1106
I am storing name and last name in two labels in main page. I also have those values in a class (class doesnt do much but i am using them for future expansion). I have a user control that will send an email with name and last name as body.
My question is that how can I transfer label or class variable values into user control's body variable?
Upvotes: 12
Views: 60035
Reputation: 11844
Create a property on your user control with the datatype of the data you want to pass to it, and populate it in your page on creation of the control.
public class myUserControl : Control
{
...
public int myIntProperty {get; set;}
...
}
Later this in the code behind you can assign the value like
myUserControl cntrl = new myUserControl();
cntrl.myIntProperty = 5;
Instead of this you can pass the value through Markup also like
<uc1:myUserControl ID="uc1" runat="server" myIntProperty="5" />
Upvotes: 34
Reputation: 93000
You need to define public properties on the control and then when you use control on the page you can pass values to those parameters.
Something like:
<cc:mycustomControl runat="server"
MyProperty1=<%# label1 %>
MyProperty2=<%# label2 %>
/>
Upvotes: 3
Reputation: 482
you can do something like this in your user control
string x=((yourparentcontrol)this.parent).label1.text;
and use the string x
.
Upvotes: 1
Reputation: 176886
Step 1: You can expost the values as property and than you can make use of that easily.
Step 2: To access your page from the user control you can make use of Parent
property or may be some custome login to access the parent page and than write code to consume the property value.
Upvotes: 1
Reputation: 63956
You need to create properties on your control to hold these values; then from the page code, simply assign the values to the properties in the control.
On your control, you can have something like
public string FirstName
{
get {
if (ViewState["FirstName"] == null)
return string.Empty;
return ViewState["FirstName"].ToString();
}
set {
ViewState["FirstName"] = value;
}
}
Upvotes: 3