简体   繁体   English

将变量传递给用户控件

[英]Passing a variable into User Control

I want to pass one of the textbox's(on the master page) value into the user control(.ascx) page. 我想将文本框的值(在母版页上)之一传递到用户控件(.ascx)页中。 Here is my code shows how to open user control.. 这是我的代码,显示了如何打开用户控件。

Control usrCnt= LoadControl("userControl.ascx");
usrCnt.ID = "usrCnt";
ASPxPanel1.Visible = true;
ASPxPanel1.Controls.Clear();
ASPxPanel1.Controls.Add(userCnt);

How can post the textbox's value to the user control? 如何将文本框的值发布到用户控件? I can't do like this.. 我不能这样

Control usrCnt= LoadControl("userControl.ascx?param=" + textbox.Text);

Create a method for your usercontrol like SetText and then 为您的用户控件创建一个方法,例如SetText,然后

 usrCnt.SetText("textValue");

if it is your webusercontrol code behind 如果这是您背后的webusercontrol代码

   public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void SetText(string theText)
        {
            this.Label1.Text = theText;
        }
    }

and if you've been added the control to the page in page call it as 并且如果您已将控件添加到页面中的页面,则将其称为

 this.WebUserControl11.SetText(TextBox1.Text);

put these on the upper part of your usercontrol 将它们放在用户控件的上部

private string _TextBoxValue = string.Empty;
public string TextBoxValue {
    get { return _TextBoxValue; }
    set { _TextBoxValue = value; }
}

then on your masterpage 然后在您的母版页上

usrCnt.TextBoxValue = TextBox1.Text;

For the quickest and dirty way is on your MasterPage 对于最快和最肮脏的方法是在您的MasterPage上

ViewState["TextBoxValue"] = TextBox1.Text(); ViewState [“ TextBoxValue”] = TextBox1.Text();

and on UserControl, access ViewState["TextBoxValue"] to get the value. 在UserControl上,访问ViewState["TextBoxValue"]以获取该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM