Button Click Event does not fire - c#

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.

Related

OnClick Event Not Firing on Button Click ASP.NET

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.

OnClick event of button fires everytime on PageLoad

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')");

How to execute button click if PostBackUrl redirect to different server

Hi I have a button which have a PostBackUrl url.
<asp:Button ID="Button1" runat="server" PostBackUrl="https://www.vine.com/test.jsp" />
I want to create a asynchronous mail on the click on same button.I am using the onclick="Button1_Click" event but it's not working.
Please help me the easiest way to create this. Thanks
handle the redirect codebedind
protected void Button1_Click(object sender, EventArgs e)
{
//do mail stuff here
HttpContext.Current.Response.Redirect(#"https://www.vine.com/test.jsp");
}

Change asp:Label text from C# code behind

I've been trying with this for an hour or so; just can't seem to figure out.
I have an asp:Button on an aspx page, required to complete a couple of functions, one of which is to change the text of an asp:Label. This seems like it should be simple and other online posts indicate that I'm approaching the problem correctly but...
The problem is simple but it's killing me. In an effort to debug/troubleshoot, I've stripped the code right back to very basics:
protected void Page_Load(object sender, EventArgs e)
{
allValidationMsg.Text = "Original text";
}
protected void btnRegister_Click(object sender, EventArgs e)
{
allValidationMsg.Text = "Text changed";
}
When the button is clicked, nothing happens. I'm sure it's something simple that I'm missing.
Update:
<asp:Label id="allValidationMsg" runat="server" height="22px" ForeColor="Red"></asp:Label>
<asp:Button class="navbutton" ID="btnRegister" runat="server"
Text="Register User" OnClick="btnRegister_Click" />
I think when you click on the Button, Page_Load is called again and the original text remains. Try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
allValidationMsg.Text = "Original text";
}
Apart from this, I assume you register the event handler for the button in the Markup as I cannot see it anywhere in your code-behind
<asp:Button id="Button1" runat="server" OnClick="btnRegister_Click" />
Possibly you have forgotten to bind button click to the handler.
You could do it something like that in code-behind:
mybutton.Click+=btnRegister_Click;
Or in aspx:
<asp:Button id="Button1"
Text="Click here for greeting..."
OnClick="btnRegister_Click"
runat="server"/>
Solved; the problem appears to have been with the use of a CompareValidator. Don't really understand why but when this validator is commented out, problem solved. Funnily enough, RequiredField and RegularExpression validators on same page cause no issues..
Hey I think I know what it is. In your markup does your label look like this??
<asp:Label ID="Label1" runat="server" >Some text</asp:Label>
You want this in your markup:
<asp:Label ID="Lable1" runat="server" text ="some text"></asp:Label>
I have had this happen before. If you change the markup to that. It should work.
I can see that you wrote "onclick" instead of "OnClick" in the markup (case). Possibly it is the cause of the issue.
UPDATE1
Could you try to do so (check if postback works):
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
allValidationMsg.Text = "Original text";
}
else
{
allValidationMsg.Text = "After postback";
}
}
If the text is changing after pressing the button?
UPDATE2 Also, please try to make some changes in the text to understand if a new version really deploys (to exclude strong caching issues).
UPDATE3 You can try to do binding in codebehind (and delete it from aspx).

On click event of image button

I have a problem that when I click on the image button instead of redirect me to the corresponding page, it will just redirect back to the same page.
This is the code in asp;
<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" />
And this is in my page load in the code behind;
header1.Attributes.Add("onclick", "~/ChildSelection.aspx");
Any ideas why this is happening?
Your Image button should have an onclick event.
<asp:ImageButton ID="header1" ImageUrl="Resources/Icons/Header1.jpg" runat="server" OnClick="header1_Click" />
protected void header1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/ChildSelection.aspx");
}
The onclick event executes javascript if I'm not mistaking. OnClick refers to a server-side event. Try setting the window's location to redirect to the corresponding page.
header1.Attributes.Add("onclick",
string.Format("window.location = '{0}'", ResolveClientUrl("~/ChildSelection.aspx")));
I guess you can achieve it by replacing it by
header1.Attributes.Add("PostBackUrl", "~/ChildSelection.aspx");
Onclick is an event. You might also consider using a hyperlink with ImageSrc property instead of an ImageButton.
[Edit]
If you are just trying to redirect a better approach could be
<asp:HyperLink ID="header1" runat="server" ImageUrl="Resources/Icons/Header1.jpg">Click Here</asp:HyperLink>
And in code behind
header1.NavigateUrl = "~/ChildSelection.aspx";
Use the following code:
<asp:ImageButton ID="header1" src="Resources/Icons/Header1.jpg" runat="server" PostBackURL="~/ChildSelection.aspx"/>

Categories