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.
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
}
Try to Add OnClientClick="return false;"
<asp:Button ID="btnApprove" runat="server" Text="Approve" CssClass="button" OnClick="btnApprove_Click" OnClientClick="return false;"/>
How about using RegisterOnSubmitStatement instead?
Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "key", "alert('Button Approve Clicked')");
Related
I have an asp:Button that is in a JavaScript dialog window. It has an OnClick event called DialogWindowButton_Click as you can see in the code below. The event is not firing and I have put breakpoints in the C# file and it is not even entering the function. I'm not sure why and have looked at other forum posts to try to figure this out. I have 1) deleted the button and recreated the button and OnClick event themselves (this didn't work), and 2) added CausesValidation="False" to the asp:Button tag. Neither avenue has worked. What I have is shown below:
<div style="margin:auto; width:100px; padding-bottom:15px;">
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
OnClick="DialogWindowButton_Click" CausesValidation="False"/>
</div>
Then in the C# file, I have:
...
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DialogWindowButton_Click(object sender, EventArgs e)
{
DialogWindowButton_ClickHelper();
...
}
protected AddressBookEntry DialogWindowButton_ClickHelper()
{
...
}
...
I have the correct file for the CodeBehind tag as well as for the Inherits tag. In the C# file you can see that the original OnClick event calls on a helper function defined directly below it, but breakpoints in the top of DialogWindowButton_Click() aren't being reached. There are no build errors either. Could there be something else I'm missing? Thank you!
You need to set UseSubmitBehavior to false (default is true):
<asp:Button ID="DialogWindowButton" runat="server" Text="Save Entry"
UseSubmitBehavior="False" OnClick="DialogWindowButton_Click" CausesValidation="False" />
From Reference:
Use the UseSubmitBehavior property to specify whether a Button control
uses the client browser's submit mechanism or the ASP.NET postback
mechanism. By default the value of this property is true, causing the
Button control to use the browser's submit mechanism. If you specify
false, the ASP.NET page framework adds client-side script to the page
to post the form to the server.
I am trying to change the text on a label from a custom event. When I update the text through a button click event (Button1_Click), everything works. However, when I try to update the same label in a custom event(ArReceiver_OnArDataUpdate) nothing happens. ArReceiver_OnArDataUpdate is getting triggered properly.
Why does my event react differently than a button click event? What do I need to do to get my event working?
This is an out of the box web project in VS2013. Here's the code to explain better:
Code Behind:
namespace WinReceiver
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArReceiver.OnArDataUpdate += ArReceiver_OnArDataUpdate;
}
void ArReceiver_OnArDataUpdate(object sender, ArEventArgs e)
{
labelVoltage.Text = "Reset";
UpdatePanel1.Update();
}
protected void Button1_Click(object sender, EventArgs e)
{
labelVoltage.Text = "Button Clicked";
UpdatePanel1.Update();
}
}
}
Default.aspx: (The site.master has the ScriptManager in it by default)
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelVoltage" runat="server" Text="..."></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
EDIT:
I did find this question that says it's not possible, but can someone explain why this may not be possible? I'm ok with a page refresh too, but when I tried Respone.Redirect I got exceptions.
If OnArDataUpdate is a static event, it's likely to be fired when the ASP.NET page rendering process is out of scope, and thus the label is not existent because it isn't in scope of the page being processed (since ASP.NET is stateless).
However, if ArReceiver is a control on the page, it should process fine, and the issue could be within the control or something overriding it (depending on when the event fires, it may get overridden by viewstate).
I Have a page(manage-darkhast-maghale.aspx) that include a gridview and sqldatasourse.one of the gridview columns is a hyperlink that on click redirects users to (pasokhmaghale.aspx?darkhastMaghaleId={0})
in second page there is a formview and sqldatasourse. after double click on update button on edit template on code behind I have typed below codes to redirect user to first page.
protected void UpdateButton_Click(object sender, EventArgs e)
{
Response.Redirect("manage-darkhast-maghale.aspx");
}
but, after running site and clicking on update button only the page begin redirect and the data has no change on gridview and database. anyone can help me please?
Without seeing the code it is very hard to say. From the information you have provided I would say that you need to leave the CommandName attribute on your Update button in the EditItemTemplate i.e.
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
I would add the following event to your FormView that will execute when the record has been updated:
<asp:FormView ID="FormView1" runat="server" OnItemUpdated="FormView1_ItemUpdated"...
In your code behind for the FormView you should have the following:
protected void FormView1_ItemUpdated(object sender, System.Web.UI.WebControls.FormViewUpdatedEventArgs e)
{
if (e.Exception == null && e.AffectedRows > 0)
{
Response.Redirect("manage-darkhast-maghale.aspx");
}
}
I have the following ASP-Markup:
<asp:Button runat="server" ID="btnSubmit" meta:resourcekey="btnSubmit" CssClass="submit" OnClick="btnSubmit_Click" />
And the following Code-Behind:
protected void btnSubmit_Click(object sender, EventArgs e)
{
}
For some reason, the Button Click Event does not fire ... it works on other pages with the exact same markup, I've tried doing "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i" - didn't make a difference.
Also, there is nothing AJAXy going on in this page.
Any help would be greatly appreciated.
Thanks,
Dennis
Try disabling the ValidationControls and see if the event fires.
Also try adding CauseValidation=false to the markup of your Button.
How to find Whether a hyperlink is clicked or not in ASP.net C# in runtime?
I want to write code on like that
Response.Redirect("Default.aspx");
If you want to execute server code upon a click in a link, then you should use the ASP.NET control <asp:LinkButton>
This is just like a button and will allow you to hook up Server Side Events and at the end you can just redirect the viewer to any page.
You would attach either the event in the code behind, or in the ASPX / ASCX of your link in question like so:
<asp:LinkButton ID="linkGoSomewhere" runat="server" Click="linkGoSomewhere_Click" />
OR
linkGoSomewhere.Click += (linkGoSomewhere_Click);
With an event handler looking like so in your code:
public void linkGoSomewhere_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
HOWEVER
In this situation, you don't need a server side control to just send the user somewhere else. You just need a simple hyperlink:
Go somewhere else
if this HyperLink you can do it using javascript but if it is LinkButton you can do it inside onclick event
<asp:LinkButton ID="MyLnkButton" runat="server" onClick="MyLnkButton_Click" Text="Click Me!">
protected void MyLnkButton_Click(Object sender,EventArgs e)
{
Response.Redirect("Default.aspx");
}
The onclick server side handler can be added to achieve this.
<asp:LinkButton ID="LinkEditLine" runat="server" Text="Edit" onclick="lnkEdit_Click"/>
You can determine this with the Click event of the LinkButton