Inline Page Code for Sever Controls Never Works

inline page code for sever controls never works

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

Workaround for ParseControl not supporting inline code blocks?

ParseControl indeed does not support code blocks because it never causes any compilation.

One approach that should work but is more complicated is to instead rely on a UserControl served by a VirtualPathProvider.

You could then in theory wrap that into a simple API like ParseControl, but it would actually compile the user control.

You'd have to do proper caching, to make sure you don't end up compiling on every call, which would kill perf.

Dynamic Languages would work better for something like this :)

Inline code is not executing as the code behind

Since <%# %> is a databinding expression, I'm pretty sure you have to call Page.DataBind(). Give it a try:

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

Inline script does not resolve in ASP.Net custom control

This has now been cleared up my MS. The issue I discovered was caused by the fact that the "action" attribute in server forms had no effect prior to .NET 2 SP2, but now can be set. Code render blocks have never worked in attribute values - this is explained towards the end of this post.

This was a consequence of a deliberate change in behaviour introduced in Microsoft .NET Framework 3.5 SP1. Prior to the service pack, action and method attributes on server side FORM tags could not be over-ridden. If specified they would be replaced by ASP.NET with "POST" and "page name".

Previously, the ASP.NET page parser did not prevent one specifying these attributes although the documentation advised against it for the action attribute:
http://msdn.microsoft.com/en-us/library/k33801s3.aspx

In particular the comment (in the context of the FORM element):

• "The opening tag must not contain an action attribute. ASP.NET sets these attributes dynamically when the page is processed, overriding any settings that you might make. "

The issue that was originally reported by Josh, where the code block was not being interpreted is not new behaviour but is a known bug - code render blocks cannot be used within server control attributes. This is reported as a "Connect" bug:
http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=109257
which contains the following:
" Attributes of server controls cannot take an inline expression as value. This explains the unexpected behaviour as seen with: " <link href="<%=RootPath %> ..." However, inline code can be used for values of attributes."

inline code on server control properties

So...I did some more research and found Page.DataBind() is not a good thing, is better to call DabaBind on every single control you need, as @Muhammad Akhtar says, both ways renders the same so I prefer to use inline code because it seems clearer, now I have

<asp:Label ID="lbName" runat="server" Text="<%# SiteDetail.Name %>"/>

and code behind:

if (!IsPostBack)
{
lbName.DataBind();
}

Calling function in usercontrol inline code doesn't always work

As mentioned in another answer, the # means it will require databinding to be executed.

So to answer your question "How to make it run outside of the repeater" the simple answer is to call myBox.DataBind().

Your question is very similar to asp.net inline code <%# MyboolVal %>. The problem is that <%= is equal to Response.Write and outputs straight HTML, so it won't work when setting the visible property.

Inline code in head tag - ASP.NET

The reason the output is being rendered like so:

href="<%=Config.ResourcesDomain %>/images/style.css"

Is because ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal.

This is a strange quirk of marking the head section as a server control, where certain elements are treated as server controls (even without being marked explicitly with the runat="server" attribute).

Removing the quotations around the href attribute resolves the issue:

href=<%= Config.ResourcesDomain %>/images/style.css

Doing so stops the link element being treated as a server control, thus executing the code block and rendering the correct URL.

However, the above writes the href value out without quotes. Using the following, will add the quotes to the link tag:

href=<%= String.Format("'{0}'", Config.ResourcesDomain) %>/images/style.css

Hope this helps.

Edit

Strangely, if you use double quotes for the href attribute, and include double quotes within the code block this also resolves the issue:

href="<%= "" + Config.ResourcesDomain %>/images/style.css"

However, none of the above are particularly elegant solutions, and setting the URL from the code behind is probably the way to go.



Related Topics



Leave a reply



Submit