inline page code for sever controls never works

2020-03-31 05:49发布

问题:

I tried the following code, I tried to use inline expressions like <%, ,<%= , why I can't use inline expression with server side control?

In my first line of code, I was trying to follow this article, but it does not work as well. Why? http://todotnet.com/post/2009/04/18/Working-around-Cannot-create-an-object-of-type-SystemBoolean-from-its-string-representation.aspx

<asp:TextBox ID="txtDate" runat="server" Text='<%# DateTime.Now.Date.ToString("dd-MM-yyyy")%>' meta:code='<% txtDate.DataBind(); %>'></asp:TextBox>

<asp:TextBox ID="TextBox1" runat="server" Text='<%= DateTime.Now.Date.ToString("dd-MM-yyyy")%>'></asp:TextBox>

<asp:TextBox ID="TextBox2" runat="server" Text='<% DateTime.Now.Date.ToString("dd-MM-yyyy")%>'></asp:TextBox>

回答1:

You can't use code blocks (<%%>, <%=%> and <%:%>) inside a server side control. Binding expressions (<%#%>) are a different matter.

You need to learn about the differences between the shortcut server side code blocks just randomly trying the different ones, as you seem to be doing will teach you nothing.

You can assign the values in the code behind file:

TextBox1.Text = DateTime.Now.Date.ToString("dd-MM-yyyy");


回答2:

When you bind an expression <%# %> you must have to call Control.DataBind() or Page.DataBind() to evaluate it.



标签: c# asp.net css