How can I two-way bind a TextBox to a code-behind property in ASP.NET?

╄→尐↘猪︶ㄣ 提交于 2020-01-03 18:31:07

问题


I cannot get any new entries in the textbox:txtMyString to set to the property MyString. 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();
}

回答1:


If you're doing two-way databinding, you need to use the Bind() method of the databinder.

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

However, last time I checked, this was only supported if the textbox was inside a templated control such as Gridview, FormView or DetailsView.



来源:https://stackoverflow.com/questions/1997735/how-can-i-two-way-bind-a-textbox-to-a-code-behind-property-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!