Problem getting button click to fire

  • Thread starter Thread starter Epetruk
  • Start date Start date
E

Epetruk

Hi,

I'm trying to design a simple web form, but I cannot get the code in the
btnSubmit_ServerClick event to fire. What am I doing wrong?

Here's the codebehind:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace CSharpTest
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton btnSubmit;

private void Page_Load(object sender, System.EventArgs e)
{
btnSubmit = new System.Web.UI.HtmlControls.HtmlInputButton("submit");
btnSubmit.ID = "btnSubmit";
btnSubmit.Value = "Submit Data";

btnSubmit.ServerClick += new
System.EventHandler(this.btnSubmit_ServerClick);
}


private void btnSubmit_ServerClick(object sender, System.EventArgs e)
{
Response.Write("<html><head><title>Title</title></head><body><p>This is a
response.</body></html>");
}
}
}


and here's the HTML:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body bgColor="lightcyan" MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
&nbsp;&nbsp;&nbsp;
<DIV style="Z-INDEX: 108; LEFT: 92px; WIDTH: 550px; POSITION: absolute;
TOP: 71px; HEIGHT: 431px"
ms_positioning="GridLayout">
<INPUT id="btnSubmit" style="Z-INDEX: 114; LEFT: 242px; WIDTH: 120px;
POSITION: absolute; TOP: 258px; HEIGHT: 32px"
type="submit" name="btnSubmit" runat="server" value="Submit Query">
</DIV>
</form>
</body>
</HTML>

TIA,

Epetruk

epetruk at stonefield dot idps dot co dot uk
 
I don't see any page directive in your ASPX code.... on the top of the HTML
code you should have something like:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="true"
Inherits="CSharpTest.WebForm1" %>

As I don't see any init code in your code behind code (the code generated by
Visual studio). It means that you have to make sure AutoEventWireup is set
to true as I show you right up here. Otherwise nothing will happen when you
click on the button as u mentioned!

I tried it with your code and it worked ;) but i had to change the line
Response.Write( said:
response.</body></html>");
to something simpler as it refused to compile.
Response.Write("TEST");

Also you cannot write HTML tags in Response.Write as it will make you have 2
HTML tags in your page, first the one that you write in Response.Write and
the second that is generated by the ASPX page itself.

Hope this helps,

Francois
 
Hello Francois,

I'm afraid I'm still having problems with the code. I've checked
that the AutoEventWireup attribute is set to 'true', and I've
changed the text written in the Response method, but I'm
still unable to get my button click handler to fire.

Is there anything else I need to do? Was there anything else
you changed in the code I sent to make it work?

TIA
 
I am terribly sorry, I forgot to say that you have to comment out the
following line:

btnSubmit = new System.Web.UI.HtmlControls.HtmlInputButton("submit");

What you have to understand is that if you do :
btnSubmit = new System.Web.UI.HtmlControls.HtmlInputButton("submit");
you will create a new instance of an HtmlInputButton, then the reference
btnSubmit will not point anymore to the same object that is declared in the
aspx file:
<INPUT id="btnSubmit" style="Z-INDEX: 114; LEFT: 242px; WIDTH: 120px;
POSITION: absolute; TOP: 258px;
HEIGHT: 32px" type="submit" name="btnSubmit" runat="server" value="Submit
Query">
Then the event will fire to the wrong object and nothing will happen.
When you create a control in the ASPX code, it will be automatically
instanciated. Thus, you never need to instantiate manually the controls that
you have declared in the ASPX file.

Francois.

PS: the full code:
 
Back
Top