asp.net Button.Click event not firing - c#

I am having an ASP.WebSite in which I have added some form controls dynamically on aspx.cs using
form1.Controls.Add( " Object of Control ");
Also a have already added a button on form in aspx file using
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Add" UseSubmitBehavior="false" />
now when I run the program it converts the Button1 into an HTML submit button as below
<input type="button" name="Button1" value="Add" onclick="javascript:__doPostBack('Button1','')" id="Button1" />
and the form tag becomes
<form name="form1" method="post" action="Add1.aspx" id="form1" enctype="multipart/form-data">
when I click the Button1 it submits the form instead of calling the function on Code Behind.
How am I able to call the method Button1_Click specified on OnClick event of Button1?
Please Help.

Have you implemented a click-event handler in codebehind?
protected void Button1_Click(Object sender, EventArgs e)
{
Button btn = (Button)sender;
}
Button.Click Event
I don't know how this question is related to your dynamic control question since Button1 is added declaratively.

I shoud have added the button control dynamically as follow:
b1.ID = "EDIT";
b1.Text = "Add";
b1.Click +=new System.EventHandler(this.Button1_Click);
b1.Style["Position"] = "Absolute";
b1.Style["Top"] = "10px";
b1.Style["Left"] = "20px";
form1.Controls.Add(b1);
It is now calling the function Button1_Click on the click event of Button1.
I this case I have to create the function Button1_Click manually i.e. not just double clicking on the design page.

the controls you are adding dynamically, you have to add it on page load.
Then it will work fine.

Related

Why are my HTML buttons in Visual Studio not creating Event Handlers when double clicked?

I have added buttons into a web form in Visual Studio 2015 using HTML. So those buttons are in the design tab. When i double click the button in the Design Tab. It is not opening the ASPX.CS page. Is that normal? I was under the impression that i would work like that. Instead i have to go and manually code each event handler.
I have searched google and tried to run repairs on Visual Studio but found no avail.
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="Module2LabExercise.WebForm1" %>
<!DOCTYPE html>
<html>
<head>
<title>
Currency Converter
</title>
</head>
<body>
<form runat = "server">
<div>
Convert:
<input type = "text" ID = "US" runat = "server" />
U.S. dollars to
<select ID = "Currency" runat = "server" />
<br /> <br />
<input type = "submit" value = "OK" ID = "Convert"
OnServerClick = "Convert_ServerClick" runat = "server" />
<input type = "submit" value = "Show Graph" ID = "ShowGraph"
OnServerClick = "ShowGraph_ServerClick" runat = "server" />
<br /> <br />
<img id="Graph" runat="server" src="//:0"/>
<br /> <br />
<p style = "font-weight: bold" ID = "Result" runat = "server"
>
</p>
</div>
</form>
</body>
</html>
`
I was thinking that if i double click the button. It would auto open the Event Handler and create the event handler. If i am wrong. Please correct me and let me know. Just coming here because im out of ideas.
you add that code in your backend of button implementation:
here is code
Response.redirect("ex.aspx");
ex.aspx is your file name
if it not working then add:
Server.Transfer("ex.aspx");
VS won't do this for non-ASP tags, but if you instead put:
<asp:Button ID="Convert" runat="server" />
instead of
<input type = "submit" ID = "Convert" runat = "server" />
Then you'll get that feature
As for
It is not opening the ASPX.CS page. Is that normal?
yes it is normal as the button is an HTML input[type="submit"] with runat="server and not an asp.net wrapped button like: <asp:button>. Hence it is not transpiled by the Asp.net.
And why are you using runat="server" on an HTML button? If you really want a server-side event to be called use the <asp:Button> that way you will be able to create the click-event on double-clicking the button.
But if you still persist on using it you have to 3 ways attach the click event-handler to the HTML button.
Event handler in aspx designer page.
//attach a script tag to your aspx page and add the click event to it.
<script language="C#" runat="server">
protected void Convert_ServerClick_manual1(object sender, EventArgs e)
{
//your code here
}
</script>
Now just add an onserverclick="Convert_ServerClick_manual1" attrubute to the button.
Attach an event handler on the pageload event of aspx.cs page
protected void Page_Load(object sender, EventArgs e)
{
Convert.Click += Convert_ServerClick_manual2;
}
now define the event handler below the pageload event:
private void Convert_ServerClick_manual2(object sender, EventArgs e)
{
//your code here
}
Manual event handler
//just create the event handler for your button on the `aspx.cs` page and paste the handler name to the `HTML` button.
private void Convert_ServerClick_Manual3(object sender, EventArgs e)
{
//your code here
}
Then in your design page use:
<input type = "submit" value = "OK" ID = "Convert" OnServerClick = "Convert_ServerClick_manual3" runat = "server" />
manually create an event handler for the input button[runat="server"] in the <yourPage>aspx.cs and attach it to the HTML button.

asp.net search button just refreshing page

Im trying to create a search button on my website on my homepage which is an aspx page but when I click search all it does is refresh the page instead of preforming the query, any help would be appreciated
heres the code for my index.aspx and the code for my index.aspx.cs page
<asp:TextBox ID="searchtitle" runat="server"></asp:TextBox>
<asp:Button ID="searchitems" runat="server" Text="Search" />
protected void searchitems_Click(object sender, EventArgs e)
{
String stext = searchtitle.Text;
Response.Redirect("search.aspx?searchquery=" + stext);
}
The button is not working because you didnt call the event on the button
<asp:Button ID="searchitems" runat="server" OnClick="searchitems_Click" Text="Search" />
add OnClick attribute with name of the event you want to call and in the given code the event you want to call is searchitems_Click.
definitely when you will click the searchitems page will reload because function for that click is defined on server side.. what you have to do is on the pageload look for the QueryString and get the value of ["searchquery"] then manipulate it as you want..
if you want to not refresh the page use Ajax then...

Click event not being called for HTML submit button

I need to add button in user control. When the button is clicked url should be captured and stored in a session. When I click the button, server click event is not being called. I am in confusion why it is not being called. Did I made anything wrong here? Please suggest.
I have the following code:
ascx:
<input type="submit" id="QuickViewbutton" value="Click to add this page to Quick View"
runat="server" onserverclick="addQuickView" clientidmode="Static"/>
ascx.cs:
In page load:
QuickViewbutton.ServerClick += new System.EventHandler(this.addQuickView);
In protected void addQuickView(Object sender, EventArgs e) function I have written the code for fetching url:
string strUrl = HttpContext.Current.Request.Url.AbsoluteUri;
Session["QuickViewUrl"] = strUrl;

Fire html button using .cs page

i need to fire html buttom after the end of my asp.net c# code
<asp:Button ID="Button3" runat="server"
Text="Button" onclick="Button3_Click" />
<input id="Button2" type="button" value="button" data-type="error" class="growl-type2" runat="server" />
and cs code is
protected void Button3_Click(object sender, EventArgs e)
{
----
i need to fire button2( automatically) in this position
}
Button clicks are just handled events. To fire a button click you simply need to raise the correct event.
You'll need something like: Button2_Click(sender, e)
Alternatively, you can inject some Javascript into the page, which reacts directly to the click of button3 or is caused by the click of button3 and causes the click of button2 through something like: document.getElementById('Button2').click()

Why IE does not refresh the page after a JS call to a click button?

On a ASP.Net page, I am in front of a problem only with IE. Here is the description.
On a page, there is two buttons and one label. The first button is visible and calls a JS function on the click event. This JS function calls the click function of the second button. The second button has an C€ event handler on the click event. The C# event handler edit the label.
In Firefox : the label is correctly edited after the clicks.
In IE (8) : the label is not edited, despite the C€ event handler has been correctly hit.
Also, I observed, in IE, that the Page_Load event is called two times after the JS button click :
Page_Load
button2_OnClick => change of the Label Text
Page_Load => The Label Text is reset :(
In Firefox, the Page_Load is called only once.
My question is : how to make IE refresh correctly the page as Firefox does after a JS button click ?
Below is the sample test code :
1) Page ASPX
<head runat="server">
<title></title>
<script type="text/javascript">
function button1Click(sender, args) {
var button2 = document.getElementById("button2");
button2.click();
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="button1" Text="Click-me!" OnClientClick="button1Click();" />
<asp:Button runat="server" ID="button2" Text="Second" OnClick="button2_OnClick" style="display:none" />
<p />
<asp:Label runat="server" ID="label1" Text="Init" />
</div>
</form>
</body>
</html>
2) C# code-behind :
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void button2_OnClick(object sender, EventArgs e)
{
label1.Text = "Changed";
}
}
The ID of your button will not be button1 or button2 when it's rendered. It will probably be something like ctl001_button1. Therefore your javascript will not work. In ASP.NET 4 you can override this behaviour by using an assigned ClientID.
<asp:Button runat="server" ID="button1" Text="Click-me!"
OnClientClick="button1Click();" ClientIDMode="Static" />
<asp:Button runat="server" ID="button2" Text="Second"
OnClick="button2_OnClick" style="display:none" ClientIDMode="Static" />
As an aside, this alludes to the main problem with ASP.NET Winforms - it tricks developers into thinking that the web is a connected environment.
What actually happens when you click an <asp:Button /> element by default is that a postback is invoked. I.e. Your browser sends a request to the server for a new page. It sends up something called ViewState which is how the server knows what you've done and what to render. There is no "event" handled as such.
I think the problem is with the way you are trying to get the hidden button
var button2 = document.getElementById("button2");
maybe change this to
var button2 = document.getElementById("<%= button2.ClientID %>");
After the buttons are rendered in the browser, the ID is changed by the ASP.Net engine, and not the same as your source.
http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
Hope this helps.

Categories

Resources