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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.