0
votes

i have 2 server side asp.net buttons , i need to automate the buttons clicks. i.e. After page_load, i need to click button1 and after its results are shown on the page, wait for 10 seconds and click button2 .

i tried the following sample code

 protected void Page_Load(object sender, EventArgs e)
    {
        Button1_Click(Button1, null);
        Thread.Sleep(10000);
        Button2_Click(Button2, null);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        changeLabel.Text = "Button1";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        changeLabel.Text = "Button2";
    }
}

i had 2 obeservations (maybe useful):

  1. always Button2_Click(Button2, null); event is the latest when the page is fully loaded (which is obvious).

  2. Page_Load(object sender, EventArgs e) doesnot hit atall when programatically clicked the button.

Any idea how to achieve the solution.

2
This seems like an odd scenario for a webpage that uses ASP buttons. You'll need client-side code if you want to wait ten seconds after the page has loaded. Have you looked into programmatically submitting HTML forms using JavaScript?Matthew Haugen
@MatthewHaugen: thanks fro the quick response.i have got no idea how to achieve this.ismail baig

2 Answers

2
votes

What you are doing in your example is to call the handlers of button1 and button2's click events, that is not the same as having the user click the buttons, and for the form to postback to the server.

If you want the buttons to click them selves and having the post back to the server you need to add javascript that clicks the buttons for you.

If you want a javascript which hits a button for you after X seconds, i would do something like this: in your aspx page:

<asp:button ID="Button1" runat="server" ClientIDMode="Static" OnClick="Button1_Click">
</asp:button>

in your javascript files or some block on your page:

<script type="text/javascript">
    setTimeout(function(){
        document.getElementById("Button1").click();
    }, 10*1000); // 10 seconds
</script>

Important to note here is that you have to either set ClientIDMode="Static" on your button, otherwise it might have a very obscure name if you are using master pages, or you can do:

<script type="text/javascript">
    setTimeout(function(){
        document.getElementById("<%= Button1.ClientID %>").click();
    }, 10*1000); // 10 seconds
</script>

if you have the javascript in your .aspx file rather then its own .js file.

ps: if you do Thread.Sleep(X) in an aspx page, you will only make the users browser wait for the X milliseconds more for the page to load, code run before the sleep will not be submitet to the clients browser in the way i think you want it to do.

0
votes

Button_Click is server event, when invoked from client, browser post the relavent data to server and request of new page content, at that time Page_load is invoked.

if you want to do some thing, encapsulate action to some method and call that method in pre-render. Or other wise, use JavaScript.

protected void Page_PreRender(object sender, EventArgs e)
    {
        UpdateButton1()
        Thread.Sleep(10000); // no need to put sleep
        UpdateButton2();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        UpdateButton1();
    }

    protected void UpdateButton1()
    {
        changeLabel.Text = "Button1";
    }