Reputation: 3307
I have a problem with passing an argument to the client-side event OnClientClicking.
Whenever I pass an Eval argument, there is a postback for some reason, even though I wrote "return false;"
in the OnClientClick
code:
OnClientClick='<%# "toggleContactDetails("+Eval("ItemId")+"); return false;" %>'
I tried different things, including some solutions I found in similar questions.
I also tried this:
OnClientClick='<%# string.Format("toggleContactDetails({0});return false;", Eval("ItemId"))%>'
or playing with the quotation marks; Nothing works.
I know the function isn't even running, since I wrote for testing:
function toggleContactDetails(itemId) {
confirm(itemId);
}
In the javascript file, and no message appears.
Any advice, please?
Upvotes: 2
Views: 3621
Reputation: 911
i found this trick
OnClientClick='<%# JS("alert""" + Eval("item","")+""") %>'
and then in codebehind:
public static string JS(jsname)
{
return string.Format("{0}", js);
}
Upvotes: 0
Reputation: 35564
You have to return a value from the javascript function.
OnClientClick='<%# "return toggleContactDetails("+ Eval("ItemId")+ ");" %>'
Javascript:
<script type="text/javascript">
function toggleContactDetails(itemId) {
return confirm(itemId);
}
</script>
Upvotes: 1
Reputation: 9166
You have in the question this sample:
OnClientClick='<%# "toggleContactDetails("+Eval("ItemId")+"); return false;" %>'
The problem with this is that it is rendered to the browser like this:
onclick="toggleContactDetails(Item Name 1); return false;"
This is broken javascript
(missing quotes around Item Name 1
) so when you click the button, the javascript
does not runt correctly, which means that return false;
is not run. This in turn means that the postback
will be triggered. In order for this to work you must render correct javascript
:
OnClientClick='<%# "toggleContactDetails(\"" + Eval("ItemId") + "\"); return false;" %>'
Only relevant difference is \"
in a couple of places.
Upvotes: 3