How can I get the text field value for a user control

I have a user control that has a text field on it, now this usercontrol is on another user control that I use on the aspx page, how can I get the text field value for the first user control.

+3
source share
4 answers

Write a property in your usercontrol to open its contents, e.g.

public string TextBoxValue
{
    get { return txtControl1.Text; }
}

This way you can get the value of the text field without exposing the entire text box control to a public object.

+9
source

Jon Limjap - .

, ( ), Reflection " " ChildControls UserControl:

TextBox txt = UserControl1.FindControl("myTextBox") as TextBox;

if (txt != null)
{
  string val = txt.Text;
}
+2

.aspx .

<%@ Register TagPrefix="Test" TagName="TestControl" Src="Test.ascx" %>

, . TagPrefix , TagName - , Src - . ,

<Test:TestControl id="TestControl" runat="Server"/>

.aspx: .aspx- , javascript aspx-. test.ascx , .aspx, .ascx.

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

.aspx

TestControl.FirstName
You can set the FirstName of the control from aspx page using
TestControl.FirstName = "Suzzanne"

: ref

+1

, . , :

    string get_value(string control_name)
    {
        var key = Request.Form.AllKeys.First(x => x.ends_with(control_name));
        return Request.Form[key];
    }
0

Source: https://habr.com/ru/post/1706229/


All Articles