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

10,349

Solution 1

OK so the basic problem here is that if you use an inline expression you can NOT use it to set a property of a server-side control outside of a binding context (using a binding expression). I have inferred that this is probably because of the timing of the evaluation of these inline expressions. You can, however, render client-side markup in this way. If you want to keep the functionality purely in your aspx file, this is the way to do it.

Edit: Based on input from Justin Keyes, it appears it IS possible to use a binding expression to set the property. You need to manually invoke Page.DataBind() to trigger the textbox to evaluate the expression (see answer below).

For instance this:

<asp:Label ID="lbl" runat="server" Text="<%= Now.ToShortDateString() %>"  />

Will produce this output:

<%= Now.ToShortDateString() %>

On the other hand this:

<%= "<span>" & Now.ToShortDateString() & "</span>"%>

Will produce this output:

7/27/2011

The "normal" way to solve this problem is just to set the Label.Text properties in a Page.Load event handler or another appropriate event handler depending on your needs, as below. This is the way I believe most people would prefer to do it, and is most easily understandable in my opinion.

Markup:

<asp:Label ID="lbl" runat="server" />

Code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    lbl.Text = Now.ToShortDateString()
End Sub

Solution 2

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:

<input ID="Textbox1" Type="Text"  
    Value='<%= new ContextItem("title").Value %>' />

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:

<asp:TextBox runat="server" Text="<%# new ContextItem("title").Value %>" />

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().

Solution 3

You can set the text on a page in a similar way.

<asp:TextBox id="TextBox1" runat="server" Text='<%#GetValue('Title)%>' />

But in order for this to work, you will need to DataBind the control on Page_Load. For multiple TextBox controls you could just loop through each and databind them so that you do not have to hard code the databinding of each.

I am not sure what your ContextItem is though, so you would have to modify my code.

Solution 4

The short answer is NO, you can only use this kind of code with databindings, that means inside a GridView for example. But you can use this in the head section.

I use it to prefix my urls sometimes with something predefined. Example

  <script src="<%=Utils.GetGeneralPrefix()%>/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

In that case it works.

Hope it helps.

Share:
10,349
Adler
Author by

Adler

Updated on June 23, 2022

Comments

  • Adler
    Adler almost 2 years


    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 more convenient if it possible to do it like this:

    <asp:TextBox runat="server" Text="<%= new ContextItem("title").Value %>" />
    

    Unfortunately the above can't be done..
    The issue is that this text box element repeats it self several times in the page and my question is:

    Are there any suggestions how to make it cleaner then to write it again and again in the code behind?
    Thank, Adler

  • Adler
    Adler almost 13 years
    I've already done this, thank you but as far as i tried it it just not possible.. have you done this before- insert the inline code inside the element?
  • Adler
    Adler almost 13 years
    thanks Justin.. i've tried this too but it didn't work also.. have you done this before with the '#' and it worked for you?!
  • mikeschuld
    mikeschuld almost 13 years
    We do it all over the place in our code. Most look something like this: Text='<%# DataBinder.Eval(Container.DataItem, "Name") %>'
  • Justin M. Keyes
    Justin M. Keyes almost 13 years
    see my updated answer. If it still isn't working, then something else is wrong. I'm suspicious about new ContextItem("title").Value. What is the exact exception message you are getting? (In the future, always post the exception details.)
  • Adler
    Adler almost 13 years
    The exception is = "Server tags cannot contain <% ... %> constructs." and in case I use ' ' instand of " " it's simply copy the <%...%> as text! (what i'm trying to do is not with data binding to a list view or something)
  • pseudocoder
    pseudocoder almost 13 years
    It works fine in the body, just not on a server-side control.
  • k-dev
    k-dev almost 13 years
    thanks, the problem is that asp.net take server controls and render the appropriate html, I suppose that in that case it doesn't eval code inside tags, it just take it as plain text. Actually resharper shows it as a warning.
  • k-dev
    k-dev almost 13 years
    I agree with you, in webforms everybody expect to see the code in code-behind page, not mixed with the html, it is not Asp.Net MVC.
  • Tim B James
    Tim B James almost 13 years
    @pseudocoder where are you getting the Label control from? The OP asks about a TextBox control not a Label? And your reference to not being in a databound control does not have any relevance to a TextBox, as a TextBox can be databound itself.
  • pseudocoder
    pseudocoder almost 13 years
    You're right of course. Sorry about that. I'm not sure about the ability to use a Binding Expression in a textbox property though. I have never seen how you can databind a textbox outside of the context of a containing databound control.
  • Justin M. Keyes
    Justin M. Keyes almost 13 years
    @pseudocoder, I tried The Moof's Page.DataBind() trick and confirmed that it works. Any Control (not just Repeaters etc.) can be forced to evaluate <%# expressions by calling Control.DataBind(). See my updated answer.
  • user1027620
    user1027620 over 12 years
    you have no idea what mvc is do ya?