0

Say I have a simple web app written in asp.net/vb.net. The web page has one button. When you click it, it does some junk in the code behind. What I would like to do, is when the page reloads, after the server has done its thing, to execute a JavaScript function.

Now the only stipulation is that it can only happen if the button was clicked. Not just on any page load. Is there a simple way to do this?

3
  • @User489041-You need to call button click event or you need to call java script function on page load? Commented Jan 18, 2012 at 22:02
  • @DotNetter As soon as the vb.net code finishes up, I need the JavaScript function to be called. Commented Jan 18, 2012 at 22:04
  • @User489041-So you are calling that function from the button click.If so on page_load you can call button click event as Button1_Click(Nothing, Nothing) Commented Jan 18, 2012 at 22:04

3 Answers 3

1

Try the below script

ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "Script", "alert('Alert Message');", True)

in page load complete method.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do this way:

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Button1.Attributes.Add("onClick", "yourfunction();");
    End Sub

Or else do this way:

<asp:Button runat="server" ID="btnAdd" Text="My Button" OnClientClick="return validate()" OnClick="btnAdd_Click" />

Comments

0

You can call Page.ClientScript.RegisterStartupScript() in your code behind (see http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx)

Like so:

Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "alert('javascript called');", true);

3 Comments

I executed this in my vb.net code, but it never shows the javascript alert. Is there more too it?
The page is just reloading after the button click, it's not redirecting somewhere else, is it? If it's just reloading, if you view the source of the page do you see a <script> tag created by that call?
Where did you add it? It should be in the btnAdd_Click event, see if it shows up in the page source like Dan said.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.