将值从页面传递到用户控件

时间:2022-10-19 22:19:22

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?

我的问题是如何将标签或类变量值转换为用户控件的body变量?

5 个解决方案

#1


28  

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

而不是这个你可以通过Markup也传递价值

<uc1:myUserControl ID="uc1" runat="server" myIntProperty="5" />

#2


2  

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;
      }
}

#3


1  

Step 1: You can expost the values as property and than you can make use of that easily.

第1步:您可以将值作为属性公开,然后您可以轻松地使用它。

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.

步骤2:要从用户控件访问您的页面,您可以使用Parent属性,或者可以通过某些客户登录来访问父页面,而不是编写代码来使用属性值。

#4


1  

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 %>
/>

#5


0  

you can do something like this in your user control

你可以在你的用户控件中做这样的事情

string x=((yourparentcontrol)this.parent).label1.text;

and use the string x.

并使用字符串x。

#1


28  

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

而不是这个你可以通过Markup也传递价值

<uc1:myUserControl ID="uc1" runat="server" myIntProperty="5" />

#2


2  

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;
      }
}

#3


1  

Step 1: You can expost the values as property and than you can make use of that easily.

第1步:您可以将值作为属性公开,然后您可以轻松地使用它。

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.

步骤2:要从用户控件访问您的页面,您可以使用Parent属性,或者可以通过某些客户登录来访问父页面,而不是编写代码来使用属性值。

#4


1  

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 %>
/>

#5


0  

you can do something like this in your user control

你可以在你的用户控件中做这样的事情

string x=((yourparentcontrol)this.parent).label1.text;

and use the string x.

并使用字符串x。