Escape double quotes in asp.net Onclick string - asp.net

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.

Related

how to escape characters when using server side delimiters

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") +" );" %>'

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.

CKEditor breaking custom .NET tags by converting single quotes to double quotes

At the client's request, we just upgraded a custom CMS system for a large site from FCKEditor 2.x to CKEditor 3.5.3.
Inside an ItemTemplate I have a custom UserControl tag in which the attributes are populated by DataBinding, like so:
<my:Viewer runat="server">
<ItemTemplate>
<my:CustomTag runat="server"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageUrl") %>' />
</ItemTemplate>
</my:Viewer>
So, the point is that the above works just fine. However, when the HTML is put into the latest CKEditor, CKEditor changes the ImageUrl attribute to use double-quotes instead of single quotes. Once it's changed to double quotes, it causes a parsing error on the .aspx page. Changing: "ImageUrl" to "ImageUrl" works, but it's not ideal for our client who is going to have to update every page that exists in a very large CMS system. So, I'm asking this question hoping someone might know of a way to toggle CKEditor to use single quotes in HTML attributes by default instead of double quotes to reduce the amount of work my client is going to have to do.
I'm only looking for easy configuration-type changes, not patching the editor, etc.
This should do what you want
Taken from here
http://cksource.com/forums/viewtopic.php?f=11&t=20647&sid=f47526ecfb1f2303ad0b923ceed7aafe&start=10
To avoid CKEditor changing special chars:
switching in source view:
CKEDITOR.instances.TEXT.on( 'mode', function(ev) {
if ( ev.editor.mode == 'source' ) {
var str=ev.editor.getData();
str=str.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
ev.editor.textarea.setValue(str);
}
});
When save edited document:
var html=CKEDITOR.instances.TEXT.getData()
html=html.replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, "\"");
I'm going to say that the " solution that I mentioned being too much work is simply the only answer...just to put some closure on this. Or, if I can find a way, I'll withdraw the question. Thanks rqmedes for trying...I'd actually forgotten all about this question until I got your response
:)

How to bind Request.QueryString[""] to Eval() in ASP.NET

ImageUrl='<%#Eval("Name","../Master Pages/DisasterImages/") %>'+'<%#Eval("Request.QueryString["DisID"].ToString()/{0}") %>'
DisID is a folder name.
What I want is to display the images in the directory.
I have done it using a repeater control. The problem is now I want to get the folder name in the QueryString. How can I do this? Above is the code I have tried without achieving the desired outcome...
Any help would appreciated.
Thank you!
use <%= instead of <%#. Example:
<%= Request.QueryString["DisID"] %>
I have noticed another issue with your code, you need to be careful when using quotes inside a string. You need to escape them. Therefore
"Request.QueryString["DisID"].ToString()/{0}"
should look like this
"Request.QueryString[\"DisID\"].ToString()/{0}"
Notice the backslash that serves for escaping characters.

Attribute "runat" is not a valid attribute. Did you mean "content" or "target"? How to solve this asp.net validation error

see here this error - http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.sitecore.net%2F
runat="server" is asp.net server control syntax. It must not come in html. It is interpreted by ASP.NET. you must remove this attribute.
Possible Reason for this:
1. I think the template is dynamically created. the developer make static site and cut copy paste on server side to make it dynamic but use control as response.write and forgot to remove runat="server" because it must be html content in response.write.
NOTE: No ASP.NET server control gives runat="server" in HTML. It is hardcoded in your code. remove this from both anchor and image tag.
Er... remove the attributes? They aren't valid HTML, and they're only meaningful when interpreted by ASP.NET.
Your pages should not be rendered with runat="server", so something's definitely going wrong here. What does the part of your aspx look like, that corresponds to one of the elements that is giving this validation error?

Resources