Dynamic button event not firing C#

C

chris

Hi All,

I have, in my web app, what I think must be a pretty simple problem.

I create several textboxes dynamically depending on the value from a
dropdown that causes a post back. After the textboxes are created I create a
button this way:

Button btnGo = new Button();
btnGo.Text = "Go >>";
btnGo.ID = "btnGo";
btnGo.Click += new System.EventHandler(this.btnGo_Click);

frm.Controls.Add(btnGo);

I also create the method that handles the button OnClick event (I just do a
redirect here, but nothing inside the method works)

private void btnGo_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://www.microsoft.com");
}

When I click on the generated Button, I only get what looks like a post back.

What do I need to do to get the btnGo_Click method to execute OnClick? Am I
missing something obvious?

Help is much appreciated. Thank you!
 
K

Ken Cox [Microsoft MVP]

Hi Chris,

I don't see where you get your reference for frm. Perhaps showing more code
would help.

Anyway, you could try it like this which seems to work in an ASP.NET 2.0
page.

<%@ Page Language="C#" trace="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Button btnGo = new Button();
btnGo.Text = "Go >>";
btnGo.ID = "btnGo";
btnGo.Click += new System.EventHandler(this.btnGo_Click);
Page.Form.Controls.Add(btnGo);
// or use PlaceHolder1.Controls.Add(btnGo);
}

private void btnGo_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://www.microsoft.com");
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Dynamic button</title>
</head>
<body>
<form id="frm" runat="server">
<div>
</div>
</form>
</body>
</html>

Let us know?

Ken
Microsoft MVP [ASP.NET]
 
C

chris

Thanks for the reply. I should have explained a little better. Here it goes.

I have a dropdown with 4 options: 1,2,3,4.

Each option generates different textboxes depending on the result of a sql
query that brings back a list of field names.

Control frm = FindControl("Form1");

// from query
SqlDataReader readerChild = sqlSelectChild.ExecuteReader();

while (readerChild.Read())
{
string name = "tb" + readerChild[0].ToString();
string label = "lbl" + readerChild[1].ToString();
TextBox txt = new TextBox();
txt.ID = name;
txt.Text = "";
Label lbl = new Label();
lbl.ID = label;
lbl.Font.Bold = true;
lbl.Text = readerChild[1].ToString();
frm.Controls.Add(lbl);
frm.Controls.Add(new LiteralControl("<br>"));
frm.Controls.Add(txt);
frm.Controls.Add(new LiteralControl("<br>"));
}

//then I create the button:
Button btnGo = new Button();
btnGo.Text = "Go >>";
btnGo.ID = "btnGo";
btnGo.Click += new System.EventHandler(this.btnGo_Click);

frm.Controls.Add(btnGo);

I want to handle btnGo.Click NOT client side OnClick (as I said before, my
mistake)

private void btnGo_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://www.microsoft.com");
}

my page directive looks like this:
<%@ Page language="c#" Codebehind="land.aspx.cs" AutoEventWireup="false"
Inherits="ND.land" %>

I just need to wire btnGo with btnGo_Click

Thanks again.





Ken Cox said:
Hi Chris,

I don't see where you get your reference for frm. Perhaps showing more code
would help.

Anyway, you could try it like this which seems to work in an ASP.NET 2.0
page.

<%@ Page Language="C#" trace="true"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Button btnGo = new Button();
btnGo.Text = "Go >>";
btnGo.ID = "btnGo";
btnGo.Click += new System.EventHandler(this.btnGo_Click);
Page.Form.Controls.Add(btnGo);
// or use PlaceHolder1.Controls.Add(btnGo);
}

private void btnGo_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://www.microsoft.com");
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Dynamic button</title>
</head>
<body>
<form id="frm" runat="server">
<div>
</div>
</form>
</body>
</html>

Let us know?

Ken
Microsoft MVP [ASP.NET]

chris said:
Hi All,

I have, in my web app, what I think must be a pretty simple problem.

I create several textboxes dynamically depending on the value from a
dropdown that causes a post back. After the textboxes are created I create
a
button this way:

Button btnGo = new Button();
btnGo.Text = "Go >>";
btnGo.ID = "btnGo";
btnGo.Click += new System.EventHandler(this.btnGo_Click);

frm.Controls.Add(btnGo);

I also create the method that handles the button OnClick event (I just do
a
redirect here, but nothing inside the method works)

private void btnGo_Click(object sender, System.EventArgs e)
{
Response.Redirect("http://www.microsoft.com");
}

When I click on the generated Button, I only get what looks like a post
back.

What do I need to do to get the btnGo_Click method to execute OnClick? Am
I
missing something obvious?

Help is much appreciated. Thank you!
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top