Home All Groups Group Topic Archive Search About

Dynamic button event not firing C#

Author
31 Jul 2006 11:48 PM
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!

Author
1 Aug 2006 1:19 AM
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]

Show quoteHide quote
"chris" <ch***@discussions.microsoft.com> wrote in message
news:3E7AE42E-AC64-4B18-9785-8E71587896AE@microsoft.com...
> 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!
>
Author
1 Aug 2006 2:43 PM
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.





Show quoteHide quote
"Ken Cox [Microsoft MVP]" wrote:

> 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" <ch***@discussions.microsoft.com> wrote in message
> news:3E7AE42E-AC64-4B18-9785-8E71587896AE@microsoft.com...
> > 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!
> >
>
>
>