OnClientClick and calling a 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"/>

      

0


a source to share


3 answers


Here is the answer:

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

      



And the GetId method on the server side:

protected line GetId (line index) {return "moveComment ('txtComment _" + index + "')"; }

+1


a source


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

      



+1


a source


I believe adding "return false"; to your expression OnClientClick will solve your problem. This will prevent the button from doing 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"/>

      

0


a source







All Articles