473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Problem getting button click to fire


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
Nov 16 '05 #1
3 1871
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("<html><head><title>Title</title></head><body><p>This is a
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

"Epetruk" <no****@blackhole.com> wrote in message
news:32*************@individual.net...
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

Nov 16 '05 #2
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

Francois wrote:
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("<html><head><title>Title</title></head><body><p>This
is a
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

"Epetruk" <no****@blackhole.com> wrote in message
news:32*************@individual.net...

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



Nov 16 '05 #3
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:
"Epetruk" <no****@blackhole.com> wrote in message
news:32*************@individual.net...
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

Francois wrote:
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("<html><head><title>Title</title></head><body><p>This
is a
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

"Epetruk" <no****@blackhole.com> wrote in message
news:32*************@individual.net...

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


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
1
by: Steven | last post by:
Hello, I have a Javascript function which will get executed only when I click a Image Button on my aspx page (code behind - C#). C# InImgBut on click function --> {...
3
by: Michael Johnson Jr. | last post by:
The problem is the following pseudo code causes you need to click buttons twice to get event. This is notable via debugging and single stepping. I can't seem to quite figure out how to do...
3
by: Stuart | last post by:
Hi there I am using a button column>SELECT and a datagrid.Item_Command to take a value from the datagrid (in this case a customer account number) - store that value in to a session variable and...
1
by: Klaus Jensen | last post by:
Hi! This has been annoying me all day, and I can't get it to work It is really driving me nuts! Basicly this simple webapp created to illustrate my problem, renders five buttons, and adds a...
4
by: Fueled | last post by:
Hi everyone! I've made quite a lot of research on this, and I've tried a couple of proposed solutions. Nothing has worked for me, but I feel there's not much I'm missing. So I'm turning to this...
2
by: lostdreamz | last post by:
Ok, my situation is like this. I have a form with a few text boxes each with a 'validating' event, OK & Cancel buttons, and have the AcceptButton/CancelButton/DialogResult properties all set. ...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
4
by: Alan Silver | last post by:
Hello, If you add (say) two text boxes to a form, then add a validator with some simple rule for one of them, you can observer the following problem:- Type some erroneous value in the text...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.