0

I recently posted asking how to pass databound data to a javascript function. With help, I got the results I wanted; however, I ran into another problem immediately after.

    <asp:ImageButton ID="ImageButton1" CommandArgument='<%# Eval("MECNUM") %>' runat="server" ImageUrl="printer.png" OnClick="Display_OnClick"
                OnClientClick='<%# Eval("MECNUM", "Display_OnClientClick({0})") %>' />

Lets say that MECNUM came to be XYZ. Unfortunately it throws an error, saying XYZ is an undefined variable. What I really want is the string "XYZ" to be passed. Putting XYZ.toString() didn't work as the problem is still variable undefined. Any thoughts?

    function Display_OnClientClick(mecnum) {
        alert(mecnum);
    }

Thanks a ton for the help

Carlos Mendieta
  • 860
  • 16
  • 41

1 Answers1

2

You need to enclose that into double quotes . Like this.

'<%# Eval("MECNUM", "Display_OnClientClick(\"{0}\")") %>'

Why you need to do this?

If you don't do that it will generate javascript like this - Display_OnClientClick(XYZ). So clearly XYZ need to be either javascript variable or defined as javascript variable earlier. To cover up this, you need to have Display_OnClientClick("XYZ"). Here it will say that XYZ is a string and will work.That is the only reason here.

If you are facing issue with quotes escaping ( js error), you might refer this - how to escape characters when using server side delimiters

Community
  • 1
  • 1
Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48