Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass values from page to user control

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?

like image 470
Bhrugesh Patel Avatar asked Nov 14 '11 04:11

Bhrugesh Patel


2 Answers

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" />
like image 174
Sai Kalyan Kumar Akshinthala Avatar answered Sep 21 '22 10:09

Sai Kalyan Kumar Akshinthala


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 %>
/>
like image 21
Petar Ivanov Avatar answered Sep 25 '22 10:09

Petar Ivanov