3

I have a menu on my aspx page which looks beautiful. It's exactly what I need (found it here).

Problem that I have is this: I need (somehow) to kick off a button control from the javascript. I realize that in the example, the menu items are simply href links, but I'm wondering how I could possibly do a postback and kick off my Button1_OnClick event.

Any ideas?

Thanks, Jason

Please note that Button1 is an ASP button, with server-side VB.NET code behind it

4 Answers 4

2

You'll need something like

 __doPostBack(*Button1's clientId*,"OnClick") ;
Sign up to request clarification or add additional context in comments.

2 Comments

I would recommend using the ASP.NET functions to generate the __doPostBack code. However, this should work on all browsers (with JavaScript).
That is, use Page.ClientScript to get a ClientScriptManager and use GetPostBackEventReference to generate the __doPostBack stuff. +1 For not using click().
1

Button1 will have a property called "ClientID". You can echo that out to the HTML code to get the object by document.getElementById("<%= Button1.ClientID %>"), and from there you only have to invoke the click like normally.

So:

document.getElementById("<%= Button1.ClientID %>").click();

As noted by a commentator, the .click() method won't work in all browsers. Here is one another way to click a button in JavaScript (should also work with links):

// Where 'button' is the value of the document.getElementById() function:
if (button.dispatchEvent) {
    var e = document.createEvent(“MouseEvents”);
    e.initEvent(“click”, true, true);
    button.dispatchEvent(e);
}
else {
    button.click();
}

6 Comments

Thanks for pointing that out. I edited my response accordingly.
Can't seem to find the 'ClientID' for my button. It's a standard ASP button, with the ID set to 'PrintBatch'. Looks like this: <asp:Button ID="PrintBatch" runat="server" Height="21px" Text="Print Batch" Width="102px" BackColor="#6699FF" BorderStyle="Double" />
So I guess I'm searching for getElementByID("<%= PrintBatch.? %>")
@Jason ClientID is a property of an ASP.NET Control -- it is some funny long string which equates to the actual ID in the DOM, which is not what the "ID" of the ASP.NET control is set to! (I think ASP.NET 4 allows this to be changed? In any case... the control must also be rendered -- e.g. Visible -- for the ClientID to be of use.)
Tried the following, but no luck: function PrintBatchJS() { document.getElementById("<%= PrintBatch.ClientID %>").click();}
|
0

The click() method of any <a> element simulates a click.

6 Comments

This doesn't address the question. The problem is that he doesn't know how to refer to the button itself from ASP.NET. This is because ASP.NET buttons are on the server-side, and the client-side doesn't reflect it precisely the same.
@Helgi The postback will "know" the button.
@pst: He still needs to identify the button itself from JavaScript. There is no postback until the button is clicked, and the OP is stuck on how to click it from JavaScript.
Here's what I have: <a onclick="PrintBatchJS">Print</a>
function PrintBatchJS() { document.getElementById("<%= PrintBatch.ClientID %>").click();}
|
0

You could make your <a /> links, LinkButtons. You could alternativly keep your <a /> links and put runat="server" on them and that would do it without having to fire a separate button click.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.