Visual Web Developer html button onclick event didn't fire - c#

Below is my code:
default.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="TestAjax._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
//I set an onClick event on this button:
< input type = "button" id="btnCl" onclick = "doJob" />
</asp:Content>
default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
//fires on page load;
}
void doJob()
{
//code here;
}
The question is:
Why didn't the onclick event trigger? (On default.aspx btnCL)
Thanks

You need to use an asp:Button control like this:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
and then have the event method like this:
protected void Button1_Click(object sender, EventArgs e)
{
// your code here
}
When using a regular input tag, onclick is for javascript, you have to instead use the ASP.NET control asp:Button in order to hook up the C# method.

As stated in my comment in OP your current method is making a client-side call (so looking for a JavaScript function called doJob. You need to make a server-side call so need to use an <asp:Button> control. Some example of how you could achieve this:
Web-Page (.aspx)
<asp:Button ID="btnDoJob" Runat="server" Text="Do Job" OnClick="btnDoJob_Click" />
Code-Behind (.CS)
protected void btnDoJob_Click(object sender, EventArgs e)
{
// Do your action here...
}

Because dojob() is a server-side function, but onclick on that input element is a client-side event. You're probably getting an error in your JavaScript console saying that dojob() is an undefined function.
Use an asp:Button instead of an input to make use of server-side click events. Also, dojob() should be protected. By not declaring a protection level I think the default is private so the page controls might not even be able to see it. It should match the event handler for a button click:
protected void dojob(Object sender, EventArgs e)
{
}

Related

Asp button is called by default postback method

ASPX code :
<asp:Button ID="medicalSub" runat="server" ValidationGroup="medical" Text="Save" CausesValidation="false" UseSubmitBehavior="false" ClientIDMode="Static" OnClick="medicalSub_Click" />
ASPX.CS code :
protected void medicalSub_Click(object sender, EventArgs e)
{
console.writeline("hello");
}
Error
According to that stack trace, the problem is not calling the click method. Instead, it's failing trying to load viewstate for a dropdown list control. This is long before trying to handle any events.
Try this:
copy and paste this in your page load section of your code and check if that helps:
If(!IsPostBack)
{
}
Or the following will hep:
<%# Page so just add the rest => EnableEventValidation="false" %>

How to Handle Events of User Control Grid View?

I Have a user control as a grid view.
I am adding it dynamically...i have made the method for data source but i am unable to use others events of grid view like "Row_Data_Bound" etc.
I have seen the code on net which says create delegates and add following
protected void Simple_DGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
OnRowDataBound(e);
}
but i get an error here saying The name OnRowDataBound does not exist in the current context
Can anyone help me out with accessing the events of user control grid view in the parent page
EDIT:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="ProjectManagement.UserControl.User1" %>
<div>
<asp:GridView ID="ucGridView" runat="server" SkinID="gvSchedulerSkin" AllowSorting="false" OnRowDataBound="ucGridView_RowDataBound">
<RowStyle Width="20px" />
</asp:GridView>
</div>
this is my ascx page...i want to use this grid view at more than one place(at run time) so i have created a user-control...
Basically i want to call this user control from my code behind and then using its rowdatabound i want to data to be bind with the gridview..
i saw on websites it says use events bubbling...but i do not know how to implement that.
so can u help in that matter..so that i can use rowdatabound normally as i do
thnx in advance
I've created a project locally and this is ok.
I've a control called TestUserControl.ascx. I dragged a GridView control onto the user control in design mode and called it "grdControlsGrid".
This generated the below mark up.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server">
</asp:GridView>
Then I added the event OnRowDataBound by typing "OnRowDataBound=" to the hmtl after runat="server". When you hit equals it gives you the option to create the method for this event. Double click the "Create method" option and this will wire up the event to the method for you.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server" OnRowDataBound="grdControlsGrid_OnRowDataBound">
</asp:GridView>
This code now resides in your user control as per the code below.
Alternatively you can wire the event up yourself in the users controls load.
public partial class TestUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Manually Add event handler
grdControlsGrid.RowDataBound += GrdControlsGrid_RowDataBound;
}
private void GrdControlsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Manually bound event
}
protected void grdControlsGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Auto wired event
}
}
for some reason when auto wiring via the markup you get the event "OnRowDataBound".. but when done in the code behind manually you get "RowDataBound". my guess is that they are one of the same.. but maybe someone else can shed light on that.
Hope that helps.

Asp.net button is not firing event

I am having an issue with the asp.net button. It is not firing event. I tried setting causes validation to false and removing the java script and validation but it still doesn't work.
<asp:Button ID="Button2" runat="server" Text="Save" onclick="Button2_Click" />
protected void Button2_Click(object sender, EventArgs e)
{
std.AddGuardianInfo(
Convert.ToInt16(DropDownList1.SelectedValue),
TextBox6.Text,
TextBox7.Text,
TextBox8.Text,
TextBox9.Text,
TextBox10.Text);
Response.Redirect("Std_FeeInfo.aspx");
}
Change onclick to OnClick in the markup of the asp:button. so the markup will be like this:
<asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
And an important advise for you: Use css for styling and arranging elements in your page, giving space using a sequence of will not be a good design
You want to check this steps.
#rabiya are you copied this code in some where else ? Then just remove it and double click on your asp button.
If you are using updatepanel on onclick event, this may happen.So Use 'EnableEventValidation="false"'
Example :
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

ASP.NET Button Not Calling Event

I've created an ASP.NET button like so:
<asp:Button ID="btnSaveCoords" runat="server" OnClick="btnSaveCoords_Click" Text="Save Changes" CssClass="btn btn-warning" />
and the event in the OnClick is defined as follows:
protected void btnSaveCoords_Click(object sender, EventArgs
{
System.Diagnostics.Debug.WriteLine("Saving Coordinates...");
}
When I click the button, btnSaveCoords_Click isn't fired. What am I doing wrong?
The firs row in the .aspx markup file should be like this:
<%# Page Language="C#" AutoEventWireup="true"
CodeBehind="WebForm1.aspx.cs" Inherits="SportStore.Views.WebForm1" %>

Button click triggers full page postback on first click

Despite the fact that I have placed my asp:Button inside an UpdatePanel, it is still triggering a postback on the full page the first time it's clicked. Also, the OnClick event isn't being caught the first time I click the button either, but every single time after that everything works fine.
Any ideas what could be causing this problem? See the code below.
(In my Site.Master file)
<asp:ScriptManager runat="server" AjaxFrameworkMode="Enabled" EnablePartialRendering="true" ValidateRequestMode="Disabled">
</asp:ScriptManager>
(In my actual webpage)
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Editor.aspx.cs" Inherits="Technology.WebForm1"
validateRequest="false" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<textarea id="htmlTexarea" runat= "server" style="height: 90%"></textarea>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="testBtn" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Button ID="testBtn" style="" runat="server" ClientIDMode="Static" OnClick="testBtn_Click" UseSubmitBehavior="false" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
My C# codebehind is:
protected void Page_Init(object sender, EventArgs e) {
testBtn.Click += testBtn_Click;
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void testBtn_Click(object sender, EventArgs e)
{
String test = "Helloworld";
}
Is there anything I've left out or have done wrong?
EDIT: I added the following to the C# code behind:
protected void Page_Load(object sender, EventArgs e)
{
//Should return POST, returns GET on first click
String test = Request.HttpMethod;
if (!IsPostBack)
{
//stops here first time
String hello = "Hello world";
}
else {
//should stop here
String hello = "Hello world";
}
}
The first time I click the button the server is getting a GET request and IsPostBack is returning false, without changing anything every other click sends a POST request and IsPostBack is true. Anyone know what could be causing this?
The problem was being caused by the fact that I was going from another page to this page using Server.Transfer(...), I'm not entirely sure how but this was affecting the POST request sent by the page the first time but once the page reloaded itself after the request everything worked. In my master page I changed the code to Response.Redirect(...) and it now works perfectly. Apologies if this isn't the clearest explanation but to be perfectly honest I'm not quite sure why this solved the problem, if anyone could clarify what was going on in the comments I'd really appreciate it.

Categories