VWD/C#/aspx: event handler doesn't trigger

Associate
Joined
9 Dec 2008
Posts
387
Code:
.
.
.
                Button bt = new Button();
                bt.Text = "Click Me";
                bt.Click += new EventHandler(bt_Click);
.
.
.
    void bt_Click(object sender, EventArgs e)
    {
        lbl1.Text = "Hello";    
    }
Using Visual Web Developer. It's a webforms page. Above shows how in a procedure I'm dynamically creating a button and then binding an event handler to it. Clicking the button just reloads the page and doesn't perform the action in the event handler. :confused:
 
rebind lbl1.
Thanks for reply. Um, lbl1 is not dynamic. It's created in the non-code file:
Code:
<asp:Label id="lbl1" text="No text yet" runat="server" />

edit: in other words, the lbl1.Text property is accepting and displaying-on-page string values that are dynamically set to it via other events (that's confirmed). So the button event handler is not firing.
 
Last edited:
Can you post the entire code file please? Impossible to see what's going on without the full picture of the context.
Ok here goes. Why I'm creating a button inside a dynamically created table is beyond scope, but that's the context. Here's the code portion:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
public void MakeTable(object sender, EventArgs e)
    {
            HtmlTableRow rownp = new HtmlTableRow();
            HtmlTableCell cellnp = new HtmlTableCell();
            //THE DYNAMIC BUTTON IS HERE **************************************
            Button bt = new Button();
            bt.Text = "Click Me";
            bt.Click += new EventHandler(bt_Click);
            cellnp.Controls.Add(bt);
            rownp.Controls.Add(cellnp);
            TheTable.Rows.Add(rownp);
    }

protected void Page_Load(object sender, EventArgs e)
    {
    }

    void bt_Click(object sender, EventArgs e)
    {
        lbl1.Text = "Hello";
    }
}

And the non-code portion. The static "lbl1" label is created in here. A staticaly created button triggers the MakeTable procedure above which contains the dynamic button "bt" and it's event assignment which will make "lbl1" say "hello" ... obviously that's not what the intended website will do... I'm testing the idea of dynamically created buttons in a dynamic table.
Code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Button ID="btMakeTable" Text="Make Table" OnClick="MakeTable" runat="server" />
<asp:Label ID="lbl1" Text="No text yet" runat="server" />
<table id="TheTable" border="0" cellpadding="0" cellspacing="0" runat="server" />
</asp:Content>

Cheers
 
Last edited:
You still need to bind it because you are dynamically adding components to it.
Ok. Bind it to what? (sorry never b4 made dynamicly created controls before) Constructing the table works and displays everything including the buttons (no compilation errors). But without binding it, controls within it cannot trigger events?

Cheers
 
Ok. Bind it to what? (sorry never b4 made dynamicly created controls before) Constructing the table works and displays everything including the buttons (no compilation errors). But without binding it, controls within it cannot trigger events?

Cheers
No to anything, per se, just:

Code:
TheTable.DataBind();

You'll probably also need to do this within the event handler itself (i.e. right after you've changed the label)

Code:
lbl1.Text = "some text";
lbl1.DataBind();

Has been an absolute age since I have used ASP forms though. If you can, I'd encourage moving over to MVC.
 
no you dont have to databind the label

If he puts lbl1.Text = "some text"; into Page_Load it will work...

Stelly
Yeah I know that. I want the text to change inside the event handler of the dynamically created button to show that it's event is triggering.

Ok another way of interpreting what you say is set the "value" in a staticly declared HiddenField to "Hello" (in the button's event) and then assign that value to lbl1.Text in the Page_Load. But that didn't work. The event just doesn't trigger.

Also databind made no difference.
Code:
public void MakeTable(object sender, EventArgs e)
    {            
            TableRow rownp = new TableRow();
            TableCell cellnp = new TableCell();
            Button bt = new Button();
            bt.Text = "Click Me";
            bt.Click += new EventHandler(bt_Click);
            cellnp.Controls.Add(bt);
            rownp.Controls.Add(cellnp);
            TheTable.Rows.Add(rownp);
            TheTable.DataBind();  //**********************
            
    }

void bt_Click(object sender, EventArgs e)
    {
        lbl1.Text = "Hello";
        lbl1.DataBind();       //**********************
    }
 
Last edited:
trying to do MVC and this at the same time is not helping :)

I personally think that when its posting back its losing the controls in the viewstate.

Stelly
The viewstate should be retained by default.

edit: I just explicitly set the EnableViewState property of the static Label object "lbl1" to true. Still shows that the button's event not trigging. I wonder what property still needs to be set to make this button functional...
Code:
            HtmlTableRow rownp = new HtmlTableRow();
            HtmlTableCell cellnp = new HtmlTableCell();
               Button bt = new Button();
               bt.Text = "Click Me";
               bt.Click += new EventHandler(bt_Click);
            cellnp.Controls.Add(bt);
            rownp.Controls.Add(cellnp);
            TheTable.Rows.Add(rownp);
 
Last edited:
I have a theory. I think the Button "bt" control needs to be added with the ".Add" method to the asp object "BodyContent". I just don't know how to do that. I don't know how to access the object BodyContent and its ".Controls.Add" method...
Code:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
That's what contains the other static controls.

p.s. I keep hitting ctrl-s after editing my posts... :)
 
trying to do MVC and this at the same time is not helping :)

I personally think that when its posting back its losing the controls in the viewstate.

Stelly

Same time?! :p

DataBinding controls registers them in the view state. The ASP life cycle is annoying as hell, and you need to databind your controls for every cycle - thus my suggestion to try binding the label in the event :p
 
Back
Top Bottom