0

I am using the following code to call a javascript function but the OnClientClick expression is never evaluated.

<asp:Button ID="btn1" UseSubmitBehavior="false" 
OnClientClick='moveComment(txtComment_<%# Eval("Container.DataItemIndex") %>)'
runat="server" Text="add comment"/>
2
  • Edited code to eliminate scrolling. Commented May 19, 2009 at 20:29
  • 1
    from what it looks like the way you have your button set up it is calling a javascript function and not a codebehind method correct? Commented May 19, 2009 at 20:30

3 Answers 3

1

Here is the answer:

<asp:Button ID="btn1" UseSubmitBehavior="false" OnClientClick='<%#    
GetId(Container.DataItemIndex.ToString()) %>' runat="server" Text="add comment"/>

And GetId method on the server side:

protected string GetId(string index) { return "moveComment('txtComment_"+ index +"')"; }

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

3 Comments

where are you calling this GetId method from?
I just added it for you, just noticed you left the rest of the code out
@azamsharp - accept your own answer here, so that this question doesn't show up in the site as unanswered
1
OnClientClick='<%# "GetId(" +Container.DataItemIndex.ToString()+  "); " %>'

Comments

0

I believe adding "return false;" to your OnClientClick expression will solve your problem. This will prevent the button from making a postback but will still execute your client side javascript function.

<asp:Button ID="btn1" UseSubmitBehavior="false" OnClientClick='moveComment(txtComment_<%# Eval("Container.DataItemIndex") %>); return false;' runat="server" Text="add comment"/>

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.