azamsharp
azamsharp

Reputation: 20094

OnClientClick and Calling Custom JavaScript Function

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"/>

Upvotes: 0

Views: 9037

Answers (3)

Pa3ck
Pa3ck

Reputation: 11

OnClientClick='<%# "GetId(" +Container.DataItemIndex.ToString()+  "); " %>'

Upvotes: 1

Code Monkey
Code Monkey

Reputation:

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"/>

Upvotes: 0

azamsharp
azamsharp

Reputation: 20094

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 +"')"; }

Upvotes: 1

Related Questions