How can I use the two-way binding of the TextBox to the code-behind property in ASP.NET?

I cannot get any new entries in the text field: txtMyString to set the MyString property. What am I missing here?

<asp:TextBox ID="txtMyString" Text='<%# MyString %>' runat="server" /> private string myString; protected string MyString { get { return myString; } set { myString = value; } } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) myString = "1 way test works"; DataBind(); } 
+4
source share
1 answer

If you are doing two-way data binding, you need to use the Bind () method for the data block.

 <asp:TextBox ID="txtMyString" Text='<%# Bind("MyString") %>' runat="server" /> 

However, the last time I checked, this was only confirmed if the text field was inside a template control such as a Gridview, FormView or DetailsView.

+3
source

All Articles