Reputation: 20094
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
Reputation: 11
OnClientClick='<%# "GetId(" +Container.DataItemIndex.ToString()+ "); " %>'
Upvotes: 1
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
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