views:

1723

answers:

9

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

+1  A: 

The ASP.NET controls Button and LinkButton will always do postbacks. The javascript that you might add using the OnClientClick property will execute prior to the postback but will not prevent the postback.

You can use the HyperLink control instead in the following way:

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

I noticed that you are using the variable name lbHeader. Are you sure you are adding your javascript to the right control and not to some random label?

Jakob Christensen
A: 

As an option, you can try HtmlAnchor control.

at onclick attribute return false; is enough to stop redirection.

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

but your code looks ok. do you check HTML output of your LinkButton ? Maybe some other code assigned to the onclick attribute.

Canavar
A: 

Did you just forget to put "javascript:" at the beginning of your string? [Silly me]

The code I use for this kind of thing looks like this:

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

... but I guess that does pretty much the same thing as Attributes.Add.

Your code looks sound to me. My guess is that the attribute's getting over-written by something else in the page lifecycle. Add a breakpoint in the Render of the page and look at your LinkButton's attribute collection there. Also check the rendered HTML and see if your javascript's appearing there at all.

teedyay
the onclick event handler is implicity a script call
annakata
Rendered HTML is ok, I have checked "View source". The reason is that NavigateURL fires instead of "onclick" Since I have never got stuck with it before, I suppose that "onclick" overrode NavigateUrL ,and now it doesn't?I have no idea why, maybe I use now Mozilla instead IE before? I need check
The browser shouldn't matter, and like you say: I use this code every day. Could you cut and paste exactly what you see for the onclick and the href when you view source? Thanks.
teedyay
+2  A: 

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>
Shogun
A: 

As I'm not into .NET, but in Html/Javascript/Ajax, maybe I'm wrong, but if you use the onclick way, for some browsers, you will also have to add : href="javascript:void(0);". This way, only the onclick will be executed.

RVeur23
A: 

I have already tried the approach using Hyperlink

that's aspx

<asp:Hyperlink  ID="hrefHeader" Text="lbHeader" runat="server"  Font-Size="Large" Font-Italic="true" 
 CausesValidation="False"  NavigateUrl="JavaScript:alert('lbHeader.Click!')" ForeColor="#0033CC" >
</asp:Hyperlink>

that's code-behind

  protected void Page_Load(object sender, EventArgs e)
    {
        hrefHeader.Text = HeaderText;
        //hrefHeader.NavigateUrl = string.Format("javascript: ToggleSelectionPopup('{0}')", panelContainer.ClientID);
        lblPanelTitle.Text = PanelTitle;
        RegisterClientScript();
        hrefHeader.Attributes.Add("onclick",
            string.Format("ToggleSelectionPopup('{0}');return false;", panelContainer.ClientID));
    }

That's HTML source

<a id="ctl00_MDSPages_popupUsers_hrefHeader" CausesValidation="False" onclick="ToggleSelectionPopup('ctl00_MDSPages_popupUsers_panelContainer');return false;" href="JavaScript:alert('lbHeader.Click!')" style="color:#0033CC;font-size:Large;font-style:italic;">Choose Users ... </a>

Obviously I get alert clicking the hyperlink, instead of script defined in "onclick" attribute which is still ignored


I have just tried the proposed approach

        hrefHeader.NavigateUrl = string.Format("javascript:if toggleSelectionPopup('{0}') return false;", panelContainer.ClientID);

Here is javascript

//

Here is copy-pasted url of hyperlink

javascript:if%20toggleSelectionPopup('ctl00_MDSPages_popupUsers_panelContainer')%20return%20false;

It also doesn't work, function just doesn't call

A: 

It is a bit unclear what you are trying to do. You have an invalid if statement in your javascript. Try this (it works - I tried it):

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

Are you sure you're not getting a runtime error within your call to toggleSelectionPopup?

teedyay
A: 

Thank's everybody, I have solved the issue

javascript:void(0) appeared to be a right way.

Having described my hyperlink the following way

<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.