ASP.NET TextBox - is it possible to initialize text attribute with in line code <% %>

前端 未结 5 946
傲寒
傲寒 2021-01-18 12:51


I need to initialize the text attribute of the text box element with a property from some where else when actually I can simply do this from code but it will be much m

5条回答
  •  無奈伤痛
    2021-01-18 13:42

    Option 1: don't use server controls

    If you aren't accessing the value on the server, just use plain HTML instead of an ASP.NET server control:

    
    

    Option 2: use Page.DataBind()

    If you change your code to use <%# instead of <%= (as below) and call Page.DataBind(), it will work (I've tested it). Change your markup to this:

    " />
    

    And in your logic, call Page.DataBind() in the Load event like this:

    protected void Page_Load(object sender, EventArgs e) {
        Page.DataBind(); 
    }
    

    Even though the TextBox is not contained in a typical "data bound" control such as a Repeater or GridView, calling DataBind() on a control will force it to evaluate <%# ... %> statements.

    The Moof's comment (below) is correct. This post also mentions Page.DataBind().

提交回复
热议问题