I seem to manage the same task many times beforehand; but currently I got thoroughly stuck at the simplest thing and it have made mequite irritated
I need to assign javascript to ASP.Net linkbutton (or hyperlink, does not matter) What seems to be easier?
Billion times I did it with code like like that
//lbHeader.Attributes.Add("onclick",
// string.Format("ToggleSelectionPopup('{0}');return false;", panelContainer.ClientID));
But it doesn't work. HTML I get contains javascript:__doPostback (blahblahblah) and ignores my client "onclick" function. Instead it the page goes to postback when I click the button (despite "onclick" function attaches properly in the "view page source" HTML
I tried to apply some tricks and approaches like following
1) to replace LinkButton with Hyperlink - effect still same, page forwards to "NavigateURL" when I click the href.
2) to assign "OnClientClick" properety in the declarative layout - the same effect, page goes to postback anyway and ignores "onclick"
3) to assign "javascript:ToggleSelectionPopup(blahblahblah)" as NavigateURL of the hyperlink and as PostBackURL of the linkButton. Doesn't work either
Neither of tricks helped. What do I do wrong, any ideas?
I just need to get rid of shitty server performance of that control, but how can I do it? Certainly, I can make LiteralControl with " manually, but I'd like not to use such a rough approach.
Help highly appreciated
You could use the LinkButton control and add a javascript function call to OnClientClick. This will be called before the clientside validation and before the postback (obviously). Should you need to execute this after validation but before the postback then you could call the clientside validation manually in your OnClientClick using 'Page_ClientValidate()';
<script type="text/javascript">
function someJsYouWant()
{
alert('HelloSO');
}
</script>
<asp:LinkButton ID="btnSearch"
runat="server"
OnClientClick="Page_ClientValidate(); someJsYouWant();"
onclick="btnSearch_OnClick">Search</asp:LinkButton>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With