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

2023-09-07 15:47:33 作者:终是丢了她

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?

解决方案 在C 中,我自己做了一个用户控件,如果需要传值,在调用用户控件的那个页面该怎么传参数呢

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