Erdinç Özdemir
Erdinç Özdemir

Reputation: 1381

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. 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);

Upvotes: 2

Views: 9432

Answers (2)

Alvin
Alvin

Reputation: 995

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

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

and on UserControl, access ViewState["TextBoxValue"] to get the value.

Upvotes: 1

DeveloperX
DeveloperX

Reputation: 4683

Create a method for your usercontrol like SetText and then

 usrCnt.SetText("textValue");

if it is your webusercontrol code behind

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

Upvotes: 3

Related Questions