Risho
Risho

Reputation: 2647

Is it possible to pass data to a javascript function from a asp.net Eval method?

I want to display data in the modal popup.

Here is what I have, a grid with a link button that has a onclientclick something like this: OnClientClick='showComfirm(this, <%# Eval("Message") %>); return false;'.

Then in the javascript,

function showConfirm(source, message){
     this._source = source; 
     document.getElementById('lblmessage').text=message; // not sure if this proper syntax though.
     this._popup = $find('mdlPopup');
     this._popup.show();     
 }

UPDATE

The javascript errors I was getting were due to the text which the varialble "message" contains. The varialbe contains plain English text such as a email message, with upper case, lower case letters, exclamation marks, commas, periods, appostrophies, double quotes, space, etc. So is there a workaround for this? Thanks, R.

Upvotes: 0

Views: 877

Answers (2)

Jrud
Jrud

Reputation: 1004

Yes it is possible, just straighten out quotes and such and you should have it (you may need to add some quotes around the message for the string to actually concatenate and become a string in the javascript properly).

To debug any problems with it, just view source in your browser when you've rendered the control and make sure that the javascript produced by the line given is correct.

Example:

You want this:

OnClientClick='showComfirm(this, "Are you sure you want to do this?"); return false;'

and NOT this:

OnClientClick='showComfirm(this, Are you sure you want to do this?); return false;'

UPDATE

I was able to get this to work:

<asp:LinkButton ID="LinkButton1" runat="server" onclientclick='<%# "alert(\"" + Eval("Message") + "\");" %>' Text='<%# Eval("Message") %>'></asp:LinkButton>

of course, I'm using C#, the VB equivalent would be:

<asp:LinkButton ID="LinkButton1" runat="server" onclientclick='<%# "alert(""" + Eval("Message") + """);" %>' Text='<%# Eval("Message") %>'></asp:LinkButton>

If this still isn't working, try to debug the JavaScript using either IE & VS or FireFox & FireBug. Perhaps the error is in the function and not the call.

Note that LinkButton controls automatically create HRef code for postback, so right after my alert message, I get a post-back which doesn't interfere with my test, but it may with your real situation. Perhaps a simple < a > tag would work better for you than a LinkButton control.

If you STILL can't get this to run, can you please respond with the JavaScript error you are receiving?

Upvotes: 1

aikixd
aikixd

Reputation: 514

Yes, like that.

OnClientClick='<%# "showComfirm(this, \"" + Eval("Message") + "\"); return false;" %>'

The <%# %> is not just displaying the data, it binds the data to the field, so if you use single quotes, only a bind expression can be inside.

Upvotes: 0

Related Questions