How to access user control values ​​from a page?

Hi, I created a custom control called test.ascs with one text field. Now I have added this control on the default.aspx page. How can I access this text field value from my default.aspx page?

Is there any chance?

+5
source share
4 answers

I usually set the text property text directly in the test.ascx file like this:

public string Text
{
    get { return txtBox1.Text; }
    set { txtBox1.Text = value; }
}

Then you can get and set this text box from the default.aspx code, for example:

usrControl.Text = "something";
var text = usrControl.Text;
+6
source

TextBox, .

TextBox myTextBox = userControl.FindControl("YourTextBox") as TextBox;
string text = myTextBox.text;
+3

If this is the purpose of the control, then create a public property in your user control that exposes this value, then you can access this from your page:

string textBoxValue = myUserControl.GetTheValue;
+3
source

How to access text field value from USERCONTROL on page using this usercontrol

step 1: in user control make an event handler

public event EventHandler evt;
    protected void Page_Load(object sender, EventArgs e)
    {
        txtTest.Text = "text123";
        evt(this, e);
    }

2: on the page call the event handler

protected void Page_Load(object sender, EventArgs e)
    {
        userCntrl.evt += new EventHandler(userCntrl_evt);
    }

void userCntrl_evt(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)userCntrl.FindControl("txtTest");
        string s = txt.Text;
    }
+2
source

All Articles