Q: button_click event does not work

G

Guest

Hello,
I created the following function by double clicking Button1:

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write( "Button1_Click" );
}

This does not print anything when I click Button1. What might be the problem?
Thanks,
Jim.
 
J

James Steele

Hi Jim.

Have you put a break point on the Response.Write.. line? Make sure that
your page is getting posted back to the server.

Have you created a handler for Button1_Click function? In VB.NET you
could use the Handles clause.

Private Sub Button1_Click () Handles button1.Click

Make sure the button is runat=server etc...

Perahps you could show us your code.


It should look something like this:

/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new
System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
Response.Write("Button1_Click");
}
}
}


and the aspx page should look something like....

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET
7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 65px;
POSITION: absolute; TOP: 72px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>
 
G

Guest

James, you are big help. I did not have definitions in #region Web Form
Designer. working fine now.
Thanks,
jim.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top