Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClick event of button fires everytime on PageLoad

Tags:

I have a button below:

 <asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" />

Event handler of this button on server side is :

 protected void btnApprove_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(Page.GetType(), "key", "alert('Button Approve Clicked')", true);      
    }

Just get alert on the button click from the server side.

My issue is that once I clicked on Approve button, now when I load or refresh my page again this btnApprove_Click event gets executed everytime.

I have many others button on the same form but none shows this strange kind of behaviour. I tried to change this button as HTML but still the same behaviour.

Can anyone please help me to get out of it. Thanks in advance.

like image 618
Garvit Gupta Avatar asked Aug 26 '16 05:08

Garvit Gupta


1 Answers

You should use IsPostBack on Page load to prevent every time page load on button click. You can check every time on Page_Load.

Sample Code

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
         // First Time Load When User Come 
    }
    else
    {
       // Every Time Load When Any click button in page
    }
like image 100
TechnicalKalsa Avatar answered Sep 25 '22 16:09

TechnicalKalsa