Newbie question - Runtime Button and EventHandler

Discussion in 'ASP .Net' started by Mr Not So Know It All, Mar 21, 2007.

  1. I'm new to NET. This is probably a simple question. I have a table i
    build at runtime (in the page CS file). inside the table is a button.
    i built a simple click event handler that isnt firing (not getting the
    "I'm Back" response write"). can someone please help me figure out
    what im doing wrong?

    here is the code

    aspx file :
    <%@ Page Language="C#" AutoEventWireup="true" Debug="true"
    Inherits="outage_default" %>

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

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Outage Site</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:Table ID="tbl_main" BorderWidth="1" BorderColor="Black"
    Width="100%" runat="server" />
    </div>
    </form>
    </body>
    </html>

    +++++++++++++++++++++++++++++++++++++++++++++++++

    CS File

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Drawing;
    using dbConnection; // database connection objects

    public class outage_default : Page
    {

    protected Table tbl_main;

    void Page_Load(object s, EventArgs e)
    {

    DropDownList ddl_company = new DropDownList();

    ddl_company.SelectedIndexChanged += new
    EventHandler(ddl_company_SelectedIndexChanged);
    ddl_company.Items.Insert(0,new ListItem("Select a
    Company","0"));
    ddl_company.Items.Insert(1,new ListItem("Company A","1"));

    TableRow tr = new TableRow();
    TableCell tc = new TableCell();

    Literal lit_label = new Literal();
    lit_label.Text = "Company ";
    tc.Controls.AddAt(0, lit_label);
    tc.Controls.AddAt(1, ddl_company);
    tc.ColumnSpan = 2;
    tr.Cells.Add(tc);

    tbl_main.Rows.Add(tr);
    }

    void ddl_company_SelectedIndexChanged(object sender, EventArgs e)
    {
    TableRow tr_01 = new TableRow();
    TableCell tc_0101 = new TableCell();

    Button btn_submit = new Button();
    btn_submit.Text = "Submit";
    btn_submit.Click += new EventHandler(btn_SubmitClick);

    tc_0101.ColumnSpan = 2;
    tc_0101.Controls.AddAt(0, btn_submit);
    tc_0101.HorizontalAlign = HorizontalAlign.Center;

    tr_01.Cells.AddAt(0, tc_0101);
    tbl_main.Rows.Add(tr_01);
    }

    void btn_SubmitClick(object sender, EventArgs e)
    {
    Response.Write("I'm Back");
    }
    }
     
    Mr Not So Know It All, Mar 21, 2007
    #1
    1. Advertising

  2. because the button is inside the dropdown event handler somehow the
    button's event handler is not firing? anyone know why this is and a
    solution?
     
    Mr Not So Know It All, Mar 21, 2007
    #2
    1. Advertising

  3. Howdy,

    Dynamic controls are not persited in page's control tree, therefore you have
    to recreate them on every postback. In other words, to make postback events
    fire, you have to reinstantiate the button with the same table structure.
    --
    Milosz


    "Mr Not So Know It All" wrote:

    > because the button is inside the dropdown event handler somehow the
    > button's event handler is not firing? anyone know why this is and a
    > solution?
    >
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 22, 2007
    #3
  4. are you saying i'll have to recreate the table and the button within
    the table again? please provide a code example. thanks for helping
     
    Mr Not So Know It All, Mar 22, 2007
    #4
  5. Howdy,

    Yes i am. Here's the example:

    protected void Page_Load(object sender, EventArgs e)
    {
    CreateTableRowWithDropDownList();

    if (RecreateButton)
    CreateTableRowWithButton();
    }

    private void CreateTableRowWithDropDownList()
    {
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    DropDownList dropDown = new DropDownList();

    dropDown.ID = "ddlWhatever";
    dropDown.AutoPostBack = true;
    dropDown.SelectedIndexChanged += new
    EventHandler(dropDown_SelectedIndexChanged);
    dropDown.Items.Add(new ListItem("Please Select", "0"));
    dropDown.Items.Add(new ListItem("Item1", "1"));
    dropDown.Items.Add(new ListItem("Item2", "2"));
    dropDown.Items.Add(new ListItem("Item3", "3"));

    cell.Controls.Add(dropDown);
    row.Cells.Add(cell);
    table.Rows.Add(row);
    }

    private void CreateTableRowWithButton()
    {
    TableRow row = new TableRow();
    TableCell cell = new TableCell();
    Button button = new Button();

    button.ID = "btnWhatever";
    button.Text = "Click me";
    button.Click += new EventHandler(button_Click);

    cell.Controls.Add(button);
    row.Cells.Add(cell);
    table.Rows.Add(row);
    }

    private void button_Click(object sender, EventArgs e)
    {
    Response.Write("i was clicked!");
    }

    private void dropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
    CreateTableRowWithButton();
    RecreateButton = true;
    }

    private bool RecreateButton
    {
    get
    {
    object value = ViewState["RecreateButton"];
    return value == null ? false : (bool) value;
    }
    set
    {
    ViewState["RecreateButton"] = value;
    }
    }

    Hope it helps

    --
    Milosz


    "Mr Not So Know It All" wrote:

    > are you saying i'll have to recreate the table and the button within
    > the table again? please provide a code example. thanks for helping
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 22, 2007
    #5
  6. thanks for the help. your example makes a lot of sense. i appreciate
    the time and effort. again many thanks
     
    Mr Not So Know It All, Mar 22, 2007
    #6
  7. thanks again,
    i replaced your recreatebutton in pageload with if(ispostback) and it
    worked.

    could you please explain this part of your code?
    private bool RecreateButton
    {
    get
    {
    object value = ViewState["RecreateButton"];
    return value == null ? false : (bool) value;
    }
    set
    {
    ViewState["RecreateButton"] = value;
    }

    }
     
    Mr Not So Know It All, Mar 22, 2007
    #7
  8. Hi there again,

    Yes, of course. It's used to indicate button should be recreated after next
    postback. If I got you right, the logic is to show button only if an item in
    the drop down list is selected (after the first occurrence of the
    selectedindexchanged event). ViewState is a collection that is serialized to
    a hidden field and persisted between postbacks (HTTP protocol is stateless)
    so next time page is posted back, information stored in the viewstate is
    available for the execution. I recommend reading some articles or tutorials
    about page execution basics (lifecycle, viewstate, HTTP request, etc) which
    will help you understand ASP.NET and make your life easier.
    --
    Regards

    Milosz


    "Mr Not So Know It All" wrote:

    > thanks again,
    > i replaced your recreatebutton in pageload with if(ispostback) and it
    > worked.
    >
    > could you please explain this part of your code?
    > private bool RecreateButton
    > {
    > get
    > {
    > object value = ViewState["RecreateButton"];
    > return value == null ? false : (bool) value;
    > }
    > set
    > {
    > ViewState["RecreateButton"] = value;
    > }
    >
    > }
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 22, 2007
    #8
  9. again thanks for the help. do you have any site or book
    recommendations? it seems like a lot of the books or tutorials i see
    dont discuss run-time execution as much as they discuss execution at
    design time.

    am i wrong to think that a lot of the coding should be done at run-
    time( the code-behind-cs'compiled' files) and not at design time (ascx
    or aspx files)? again your help is greatly appreciated.

    dj
     
    Mr Not So Know It All, Mar 23, 2007
    #9
  10. Good afternoon,

    Depends on the knowledge and area you want to cover, i would recommend any
    tutorials or/and books by Dino Esposito (intermidiate and advanced level), or
    books from Microsoft press library (from rookie to advanced):
    http://www.microsoft.com/learning/books/net/aspnet/default.mspx

    Best regards
    --
    Milosz


    "Mr Not So Know It All" wrote:

    > again thanks for the help. do you have any site or book
    > recommendations? it seems like a lot of the books or tutorials i see
    > dont discuss run-time execution as much as they discuss execution at
    > design time.
    >
    > am i wrong to think that a lot of the coding should be done at run-
    > time( the code-behind-cs'compiled' files) and not at design time (ascx
    > or aspx files)? again your help is greatly appreciated.
    >
    > dj
    >
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Mar 28, 2007
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars Netzel
    Replies:
    0
    Views:
    419
    Lars Netzel
    Jul 9, 2004
  2. Rob Meade

    EventHandler AddressOf Question

    Rob Meade, Jun 4, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    426
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Jun 4, 2007
  3. Christian Robert Schulz

    Eventhandler in dynamically loaded and bound controls

    Christian Robert Schulz, Jan 17, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    367
    Christian Robert Schulz
    Jan 21, 2008
  4. SteveR

    add eventhandler at runtime in VB

    SteveR, Feb 7, 2005, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    584
    SteveR
    Feb 15, 2005
  5. Dan  Christensen
    Replies:
    1
    Views:
    289
    Dan Christensen
    Aug 9, 2006
Loading...

Share This Page