how to escape characters when using server side delimiters - asp.net

So, currently, within an asp gridview, I have the following
<span id="btnEdit" runat="server" onclick="ShowEditCriteriaFilterDialog('<%#Eval("intSMCID")%>', '<%#Eval("strDescription")%>')" class="linkText">Edit</span>
What I'm essentially looking for is the syntax for quotes/double-quotes to actually accomplish this properly, as what I have above doesn't properly work.
First of all, if i encapsulate the entire onclick with single quotes, and not put any other quotes inside, it works for rendering purposes, but when i actually click the link at runtime, nothing happens.
If I encapsulate the entire onclick with double-quotes, like most properties of an ASPX element, it doesn't render properly, and everything after the comma which is after the first <%#Eval %> statement shows up as actual text on the screen. This leads me to believe there needs to be some escaping done to prevent it from thinking the click handler ends somewhere in the middle of that <%#Eval %> statement.
note that if I take away runat="server" and just encapsulate it in double-quotes, that seems to work better...but i need the span to be a server-side control for alot of other functionality I have in the page's code behind where I need to access the control through FindControl

The Eval function needs double-quotes, so you have to wrap the attribute value in single quotes.
You can't mix static content and <%# ... %> blocks in a single property value for a control with runat="server", so you need to build the entire string within a single <%# ... %> block.
Within a string in a <%# ... %> block, you can use &apos; to insert a single quote:
EDIT That only works in .NET 4.0; in 2.0, it generates &apos;, which won't work. Use double-quotes instead:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"), Eval("strDescription")) %>'
Depending on your data, you might also need to encode the string value for JavaScript:
onclick='<%# string.Format(
"ShowEditCriteriaFilterDialog(\"{0}\", \"{1}\")",
Eval("intSMCID"),
HttpUtility.JavaScriptStringEncode(Eval("strDescription", "{0}"))) %>'
(Code wrapped for readability - it needs to be on a single line.)

I think the solution is
onclick='<%# "ShowEditCriteriaFilterDialog(" +Eval("intSMCID") + ","+ Eval("strDescription") +" );" %>'

Related

ASP.NET tags don't expand in OnClientClick

I have the following two buttons:
<asp:Button ID="btnVote" runat="server" Text="Vote!" PostBackUrl="<%$RouteUrl:id=2, routename=Results%>"/><br />
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="location.href='<%$RouteUrl:id=2, routename=Results%>'"/>
The first <%$ %> expands as intended, while the second (identical!) one gets used as typed ( = not expanded). I am very new to ASP.NET, coming from PHP, and this is from my learning/test site.
What am I doing wrong and how can I fix it?
The ASP.NET expression syntax (<%$ ... %>) can only be used to directly assign values to properties of server controls. I think the problem you're having is that the expression syntax is embedded within a string, and not directly bound to the "OnClientClick" attribute.
Trying changing your second button to
<asp:Button ID="btnResults" runat="server" Text="Results ->" OnClientClick="<%$RouteUrl:id=2, routename=Results%>" />
If that works, you may need to modify your expression to return that extra text you need. Or, create another route that returns said info (the current value wrapped in the "location.href" attribute).
You can find more information about these expressions here: ASP.NET Expressions Overview.

Why does databound property of ASP.NET usercontrol work only without quotes?

I have a custom ASP.NET user control implemented fully in code-behind. The control has one string property and its declarative markup looks like this:
<uc:MyControl ImageUrl="/Content/images/" runat="server" />
Most likely the actual declaration will use data binding syntax like this:
<uc:MyControl ImageUrl="<%# PageInfo.ImageUrl %>" runat="server" />
Now here's the odd part pertaining to my question. If I use the above syntax, the data binding does not work and the value of ImageUrl at run-time is the string literal of whatever is between quotes. However, if I remove the double quotes, it works as expected:
<uc:MyControl ImageUrl=<%# PageInfo.ImageUrl %> runat="server" />
The same behavior occurs with both double and single quotes. I am puzzled by this and although the code is working it's really not optimal as putting values in quotes is the norm and the approach to make the data binding work is decidedly unorthodox.
Anyone have any ideas on why this only works without quotes?
I found the problem...it was something to do with the file (probably encoding, but not sure). On a whim, I copied the code to a new file and deleted the original. Now the data binding works with the quotes as it should.

Escape double quotes in asp.net Onclick string

I am trying to insert dynamic data into a onclick property for a control the code looks like this
onclick="openRadWindow('<%#Eval("ID";) %>','<%#Eval("ParentObjectID") %>');"
I cant get it to fire and the trouble seems to be the double quotes, what is the correct method on escaping the quotes so that this fires.
You can do use a format string, like this:
onclick='<%# string.Format("openRadWindow(\"{0}, {1}\");", Eval("ID"), Eval("ParentObjectID")) %>'
Is the event just not firing or are you getting any javascript errors as well. Also, I would look at the HTML after the page has been rendered and make sure that the server tags are being processed correctly. There are certain uses that cause them not to actually be processed and will remain <%# Eval("ID") %>.
Thanks to all I was able to get it working correctly using a different method. in the code behind I created a function and in the function I put the following code
Return String.Format("openRadWindow({0},{1});", photo.ID, photo.ParentObjectID)
and in the aspx I added onclick="<%#MyFunction(DirectCast(Container.DataItem,Photo))%>
First thing I see is that semi-colon after "ID" - I think that might be causing your problems.

referencing appSettings from usercontrols

In my .ascx usercontrol i'm trying to dynamically generate links using a value i've stored in web.config.
link
and when i try running, i get a parser error
Literal expressions like '<%$appSettings.MYPATH %>' are not allowed. Use <asp:Literal runat="server" Text="<%$appSettings.MYPATH%>" /> instead.
I know i'm probably missing something relatively minor.
<%= ConfigurationManager.AppSettings["myKey"] %>
EDIT:Don't forget the =
link
should work (it at least does on the IIS server I use). (Unfortunately it's more verbose)
Try this instead
.ascx
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in code behind
Literal1.Text = "link"
More accurate answer will be next:
Link
Use a colon instead of a dot and add runat="server":
link
The documentation isn't very clear on this point, but ASP.Net Expressions are for use within server tags. Thus, if you want to use one in a plain html tag, you must add runat="server" so that the tag is processed on the server where the expression will be evaluated.

How do I make my ASP.NET server control take an embedded code block as a property value?

I have a custom server control with a property of Title. When using the control, I'd like to set the value of the title in the aspx page like so:
<cc1:customControl runat="server" Title='<%= PagePropertyValue%>' >
more content
</cc1:customControl>
When I do this, however, I am getting the exact String <%= PagePropertyValue%> being displayed rather than the Property Value that I would like to see.
So after trying the databinding expression (as suggested below). I don't get the string literal that looked bad but I don't get anything else either.
<cc1:customControl runat="server" Title='<%# PagePropertyValue%>' >
more content
</cc1:customControl>
What do I need to do to my custom control to take this sort of value? Or is there something I need to do to the page.
You cant. <%= %> will write the string directly to the response-stream, which happens after the server control is constructed. See this post for an explanation.
So its either codebehind, or <%# + databinding as Zachary suggests.
As a followup to my own question, I have discovered that what I really wanted was to use ASP.NET Expressions using the <%$ syntax, since what I wanted to do was put in localized content.
This can be done with apparently no extra handling on the server control side.
<cc1:customControl runat="server" Title='<%$ Resouces: ResourceFile, ContentKey %>' >
more content and controls
</cc1:customControl>
This works just fine.
Try using databinding syntax:
<%# PagePropertyValue %>
For the bind property value to work correctly as suggested, you will have this in the aspx or ascx file :
<cc1:customControl runat="server" Title='<%# PagePropertyValue %>' >
more content
</cc1:customControl>
You will then need to actually bind data in your page wich you have to add this in you code behind file (code in C#)
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
That way it will bind the data in your ascx or aspx file.
Note that this is specific to control attributes. When using the <%= syntax outside control attributes meaning anywhere else in the page the syntax works as expected. So this
<%=GetCapitalUserName()%>
would call the correct method and inject the result of the call in the page.

Resources