5

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

Scott Fortier
  • 63
  • 1
  • 3

2 Answers2

0

I think the solution is

onclick='<%# "ShowEditCriteriaFilterDialog(" +Eval("intSMCID") + ","+ Eval("strDescription") +" );" %>'
GeorgesD
  • 1,062
  • 5
  • 7
  • with this solution, if I click the edit link...nothing happens, although it does render properly on screen – Scott Fortier Dec 13 '12 at 18:04
  • I will add that with this, when I view source on the line of code corresponding to this link, it seems as though the eval is happening properly, as this is what the view source shows: Edit which seems to be what i want, but yet...clicking the link seems to not cause the browser to do anything at all. I can't seem to figure out how to format comments for code :( – Scott Fortier Dec 13 '12 at 18:59
  • @ScottFortier: You'll probably see a script error in the console, because the description doesn't have quotes around it. – Richard Deeming Dec 13 '12 at 19:03
  • That's true...I do see a script error for every line of code corresponding to a row in the gridview corresponding to one of those links – Scott Fortier Dec 13 '12 at 19:06
0

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 &amp;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.)

Richard Deeming
  • 23,013
  • 5
  • 65
  • 114
  • Can you think of a replacement for JavaScriptStringEncode that's in older versions of .net. I believe that is .NET 4 or 4.5. This project is .NET 2.0 (outside of my control) – Scott Fortier Dec 13 '12 at 18:07
  • @ScottFortier: There's a sample for .NET 2.0 here: http://www.west-wind.com/weblog/posts/2007/Jul/14/Embedding-JavaScript-Strings-from-an-ASPNET-Page (It's only missing a case for `'`, which should follow the other cases.) – Richard Deeming Dec 13 '12 at 18:12
  • Alright, first option/solution in your answer compiles/runs/displays correctly but like the other answer, doesn't actually do anything when clicked, and provides a script error to the browser. This is what the tag looks like in view source: Edit – Scott Fortier Dec 13 '12 at 19:10
  • @ScottFortier: It looks like the `'` trick doesn't work in .NET 2.0; you'll need to use double-quotes instead. I'll update my answer. – Richard Deeming Dec 13 '12 at 19:25
  • OH MY GOD it works thank you so much!! i've been fiddling with this stuff for like 2 days now, I'm just so happy it's finally taken care of, I wish there was more I could do to thank you than just click the green checkmark to recognize you – Scott Fortier Dec 13 '12 at 19:49
  • Except it is the solution I gave. I only forgot the escape character and the double quote before and after the parameter strDescription – GeorgesD Dec 13 '12 at 20:36
  • @GeorgesD: You posted your answer at the same time as me, and you missed a vital part of the solution. I hope you're not trying to imply that I copied your answer? – Richard Deeming Dec 17 '12 at 12:27