tags:

views:

1031

answers:

2

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

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

azamsharp
where are you calling this GetId method from?
TStamper
I just added it for you, just noticed you left the rest of the code out
TStamper
@azamsharp - accept your own answer here, so that this question doesn't show up in the site as *unanswered*
ichiban
A: 

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