Strange problem - javascript cannot be assigned for link

It seems that for many years I have been doing the same task; but at present I am completely stuck in the simplest thing, and this caused mykite irritation.

I need to assign javascript to an ASP.Net link (or hyperlinks, it doesn't matter) Which seems simpler?

A billion times I did it with code like this

        //lbHeader.Attributes.Add("onclick",
        //    string.Format("ToggleSelectionPopup('{0}');return false;", panelContainer.ClientID));

But that will not work. The HTML I get contains javascript: __ doPostback (blahblahblah) and ignores my onclick function of my client. Instead, the page shifts to postback when I click the button (even though the onclick function is correctly attached to the "view page source" HTML

I tried to apply some tricks and approaches, for example the following

1) to replace LinkButton with Hyperlink - the effect is the same, the page goes to "NavigateURL" when I click href.

2) in order to assign the “OnClientClick” properties in the declarative layout - the same effect, the page goes to the postback anyway and ignores the “onclick"

3) to assign "javascript: ToggleSelectionPopup (blahblahblah)" as a NavigateURL hyperlink and as a PostBackURL in a linkButton. Does not work and

None of the tricks helped. What am I doing wrong, any ideas?

I just need to get rid of the shitty server performance of this control, but how can I do this? Of course, I can do LiteralControl "manually", but I would not want to use such an approximate approach.

Help get a high rating

+3
8

LinkButton javascript OnClientClick. (). , , OnClientClick, "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>
+4

ASP.NET Button LinkButton . Javascript, OnClientClick, , .

HyperLink :

<asp:HyperLink runat="server" ID="hyperlink1" NavigateUrl="JavaScript:alert('!');">Test</asp:HyperLink>

, lbHeader. , javascript , - ?

+1

HtmlAnchor.

onclick return false; , .

<a href="not_importans.htm" onclick="javascript:someothermethod();return false;">Trial Link</a>

. HTML LinkButton? , - onclick.

0

"javascript:" ? [Silly me]

, , :

myLinkButton.SetAttribute("onclick", "javascript:if(someCondition()) return false;");

... , , Attributes.Add.

. , - . Render LinkButton. HTML , javascript .

0

.NET, Html/Javascript/Ajax, , , onclick, : href= "JavaScript: (0);" . , onclick.

0

It’s a little incomprehensible what you are trying to do. You have an invalid if statement in your javascript. Try it (it works - I tried):

hrefHeader.NavigateUrl = String.Format("javascript:toggleSelectionPopup('{0}');", panelContainer.ClientID);
0
source

Are you sure you are not getting a runtime error during your call before toggleSelectionPopup?

0
source

Thanks everyone, I solved the problem

javascript: void (0) turned out to be the correct way.

Having described my hyperlink as follows

<asp:Hyperlink  ID="hrefHeader" Text="lbHeader" runat="server"  Font-Size="Large" Font-Italic="true" 
 CausesValidation="False"  NavigateUrl="javascript:void(0);" ForeColor="#0033CC" >
</asp:Hyperlink>

"onclick" started to fire.

0
source

Source: https://habr.com/ru/post/1703297/


All Articles