inline page code for sever controls never works

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>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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");

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x