Events not firing

Discussion in 'ASP .Net' started by Mike, Jul 9, 2004.

  1. Mike

    Mike Guest

    Hi,

    I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-(

    namespace WebApplication1

    {

    using System;

    using System.Data;

    using System.Collections;

    using System.Drawing;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.HtmlControls;

    using System.ComponentModel;

    /// <summary>

    /// Summary description for WebUserControl1.

    /// </summary>

    public class Tracking : System.Web.UI.WebControls.WebControl

    {

    private ArrayList arr = new ArrayList();

    private System.Web.UI.WebControls.Table m_Table = new Table();

    private System.Web.UI.WebControls.Button btn = new Button();

    //private System.Web.UI.WebControls.Label lbl = new Label();

    //private System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

    //private System.Web.UI.WebControls.DropDownList lst = new DropDownList();

    public void buildTable(TableTracking trk)

    {

    //m_Table.BorderStyle = BorderStyle.Solid;

    //m_Table.BorderWidth = Unit.Pixel(2);

    //m_Table.BorderColor = Color.DarkBlue;

    System.Web.UI.WebControls.TableRow row = new TableRow();

    System.Web.UI.WebControls.TableCell cell = new TableCell();

    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

    if (trk.Editable==true)

    {

    //btn.Text = "<font face=Verdana size=1>" + "Edit Me";

    btn.Text = "Edit";

    img.ImageUrl = "Images/img_sec/edit_pencil.gif";

    cell.Height = 5;

    //cell.HorizontalAlign = HorizontalAlign.Right;

    cell.Width = m_Table.Width;

    cell.Controls.Add(img);

    cell.Controls.Add(new LiteralControl("&nbsp;"));

    //cell.Controls.Add(new LiteralControl("<font face=Verdana size=1>"));

    cell.Controls.Add(btn);


    row.Cells.Add(cell);

    m_Table.Rows.Add(row);

    }

    else

    {

    btn.Visible = false;

    img.Visible = false;

    }

    m_Table.Width = trk.Width;

    m_Table.Height = trk.Height;

    }

    public void buildItem(ArrayList items)

    {

    System.Web.UI.WebControls.TableRow row = new TableRow();

    System.Web.UI.WebControls.TableCell cell = null;


    //cell.BorderStyle = BorderStyle.Solid;

    //cell.BorderWidth = Unit.Pixel(1);

    //cell.BorderColor = Color.DarkBlue;

    int currentRow = 0;

    int previousRow = 0;

    foreach (Item i in items)

    {

    currentRow = i.Row;

    if (currentRow != previousRow && previousRow != 0)

    {

    m_Table.Rows.Add(row);

    row = new TableRow();

    }

    cell = new TableCell();

    cell.HorizontalAlign = HorizontalAlign.Center;

    cell.Width = i.Width;

    //cell.Height = Convert.ToString(i.Height);

    if (i.Type == "Label")

    {

    System.Web.UI.WebControls.Label lbl = new Label();

    cell.Controls.Add(new LiteralControl("<font style='FONT-WEIGHT: bold' face=Verdana size=1>"));

    lbl.Text = (string)i.Content[0];

    cell.Controls.Add(lbl);

    }

    else if (i.Type == "Image")

    {

    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

    img.ImageUrl = (string)i.Content[0];

    cell.Controls.Add(img);

    }

    else if (i.Type == "DropDownList")

    {

    System.Web.UI.WebControls.DropDownList lst = new DropDownList();

    arr.Add(lst);

    for (int n=0; n<i.Content.Count; n++)

    {

    lst.Items.Add((string)i.Content[n]);

    }

    lst.Visible = false;

    cell.Controls.Add(lst);

    lst.SelectedIndexChanged+=new EventHandler(lst_SelectedIndexChanged);

    }

    row.Cells.Add(cell);

    previousRow = i.Row;

    }

    m_Table.Rows.Add(row);

    }

    protected override void Render(HtmlTextWriter writer)

    {

    Controls.Clear();

    Controls.Add(m_Table);

    base.Render (writer);

    }

    private void Page_Load(object sender, System.EventArgs e)

    {

    if (Page.IsPostBack)

    {

    foreach(DropDownList lst in arr)

    {

    lst.Visible = true;

    }

    btn.Text = "Save";

    }

    }


    #region Web Form Designer generated code

    override protected void OnInit(EventArgs e)

    {

    //

    // CODEGEN: This call is required by the ASP.NET Web Form Designer.

    //

    InitializeComponent();

    base.OnInit(e);

    }


    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion

    private void lst_SelectedIndexChanged(object sender, EventArgs e)

    {

    Console.WriteLine("OK");

    }

    }

    }


    This is how the control is set up in the Web Form:
    <WEBAPPLICATION1:TRACKING id=trck1 runat=server></WEBAPPLICATION1:TRACKING>

    Any idea on what could be wrong?

    Thanks.
    Mike
     
    Mike, Jul 9, 2004
    #1
    1. Advertisements

  2. I may be wrong, but suspect that you actually have one event firing - Page_Load. To figure out why other events are not firing, first understand why Page_Load is firing. It has a delegate defined in private void InitializeComponent()... this.Load += new System.EventHandler(this.Page_Load);

    What that line specifies is that for the this.Load event, execute the code in the Page_Load procedure.
    The syntax is something like this:
    this.Load += new System.EventHandler(this.Page_Load);
    someObjectOrControl.EventName += new System.EventHandler(thisClass.EventProcedureName);


    So, what you need to do is add a delegate (like currently specified for Page_Load) for each of the events you want to handle. You already have one, but I suspect it's in the wrong place. Find the following line in your code:
    lst.SelectedIndexChanged+=new EventHandler(lst_SelectedIndexChanged);
    and move it to your private void InitializeComponent() routine. Also, for fun, try to be consistent with what the framework already does by specifying "new System.EventHandler"... rather than "new EventHandler" (yes, you could probably get away without specifying system because you have using System; but just for consistency, blah blah blah.

    Also, get some indentation going so you can see exactly where your namespace begins and ends, where the class begins and ends, etc. Make your code more readable and you might be able to eyeball some issues.

    Also, don't expect Console.WriteLine to work in an asp.net Web app. you might try Response.Write("Hello World"); instead.

    HTH






    Hi,

    I am adding controls dynamically in a WebForm, but none of these controls' events fire. Here is the class code I am using. I have tried so many things, but nothing works :-(

    namespace WebApplication1

    {

    using System;

    using System.Data;

    using System.Collections;

    using System.Drawing;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    using System.Web.UI.HtmlControls;

    using System.ComponentModel;

    /// <summary>

    /// Summary description for WebUserControl1.

    /// </summary>

    public class Tracking : System.Web.UI.WebControls.WebControl

    {

    private ArrayList arr = new ArrayList();

    private System.Web.UI.WebControls.Table m_Table = new Table();

    private System.Web.UI.WebControls.Button btn = new Button();

    //private System.Web.UI.WebControls.Label lbl = new Label();

    //private System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

    //private System.Web.UI.WebControls.DropDownList lst = new DropDownList();

    public void buildTable(TableTracking trk)

    {

    //m_Table.BorderStyle = BorderStyle.Solid;

    //m_Table.BorderWidth = Unit.Pixel(2);

    //m_Table.BorderColor = Color.DarkBlue;

    System.Web.UI.WebControls.TableRow row = new TableRow();

    System.Web.UI.WebControls.TableCell cell = new TableCell();

    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

    if (trk.Editable==true)

    {

    //btn.Text = "<font face=Verdana size=1>" + "Edit Me";

    btn.Text = "Edit";

    img.ImageUrl = "Images/img_sec/edit_pencil.gif";

    cell.Height = 5;

    //cell.HorizontalAlign = HorizontalAlign.Right;

    cell.Width = m_Table.Width;

    cell.Controls.Add(img);

    cell.Controls.Add(new LiteralControl("&nbsp;"));

    //cell.Controls.Add(new LiteralControl("<font face=Verdana size=1>"));

    cell.Controls.Add(btn);


    row.Cells.Add(cell);

    m_Table.Rows.Add(row);

    }

    else

    {

    btn.Visible = false;

    img.Visible = false;

    }

    m_Table.Width = trk.Width;

    m_Table.Height = trk.Height;

    }

    public void buildItem(ArrayList items)

    {

    System.Web.UI.WebControls.TableRow row = new TableRow();

    System.Web.UI.WebControls.TableCell cell = null;


    //cell.BorderStyle = BorderStyle.Solid;

    //cell.BorderWidth = Unit.Pixel(1);

    //cell.BorderColor = Color.DarkBlue;

    int currentRow = 0;

    int previousRow = 0;

    foreach (Item i in items)

    {

    currentRow = i.Row;

    if (currentRow != previousRow && previousRow != 0)

    {

    m_Table.Rows.Add(row);

    row = new TableRow();

    }

    cell = new TableCell();

    cell.HorizontalAlign = HorizontalAlign.Center;

    cell.Width = i.Width;

    //cell.Height = Convert.ToString(i.Height);

    if (i.Type == "Label")

    {

    System.Web.UI.WebControls.Label lbl = new Label();

    cell.Controls.Add(new LiteralControl("<font style='FONT-WEIGHT: bold' face=Verdana size=1>"));

    lbl.Text = (string)i.Content[0];

    cell.Controls.Add(lbl);

    }

    else if (i.Type == "Image")

    {

    System.Web.UI.WebControls.Image img = new System.Web.UI.WebControls.Image();

    img.ImageUrl = (string)i.Content[0];

    cell.Controls.Add(img);

    }

    else if (i.Type == "DropDownList")

    {

    System.Web.UI.WebControls.DropDownList lst = new DropDownList();

    arr.Add(lst);

    for (int n=0; n<i.Content.Count; n++)

    {

    lst.Items.Add((string)i.Content[n]);

    }

    lst.Visible = false;

    cell.Controls.Add(lst);

    lst.SelectedIndexChanged+=new EventHandler(lst_SelectedIndexChanged);

    }

    row.Cells.Add(cell);

    previousRow = i.Row;

    }

    m_Table.Rows.Add(row);

    }

    protected override void Render(HtmlTextWriter writer)

    {

    Controls.Clear();

    Controls.Add(m_Table);

    base.Render (writer);

    }

    private void Page_Load(object sender, System.EventArgs e)

    {

    if (Page.IsPostBack)

    {

    foreach(DropDownList lst in arr)

    {

    lst.Visible = true;

    }

    btn.Text = "Save";

    }

    }


    #region Web Form Designer generated code

    override protected void OnInit(EventArgs e)

    {

    //

    // CODEGEN: This call is required by the ASP.NET Web Form Designer.

    //

    InitializeComponent();

    base.OnInit(e);

    }


    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    this.Load += new System.EventHandler(this.Page_Load);

    }

    #endregion

    private void lst_SelectedIndexChanged(object sender, EventArgs e)

    {

    Console.WriteLine("OK");

    }

    }

    }


    This is how the control is set up in the Web Form:
    <WEBAPPLICATION1:TRACKING id=trck1 runat=server></WEBAPPLICATION1:TRACKING>

    Any idea on what could be wrong?

    Thanks.
    Mike
     
    Robert Howells, Jul 9, 2004
    #2
    1. Advertisements

  3. Hi Mike

    Robert is right about the missing delegates. They are usually added
    automatically to the InitializeComponent method when you drag a control
    onto the form in VS.net. A word of warning though. I have had a lot of
    trouble, as have a lot of other people if you check through the
    newsgroups, with manually adding items to the InitializeComponent
    method. There is a good reason why the comment says 'do not modify
    the contents of this method with the code editor.' It seems that each
    time you go into design view, or make a change in design view, Visual
    Studio deletes and rewrites the contents of this method. I have found
    that often it isn't too successful in doing this if you have altered the
    contents of the method and consequently events seem to stop firing as
    the delegates have been erased. This has been fixed in .NET 2.0 with the
    introduction of partial classess but in 1.1 the best workaround I could
    come up with is to not put them in there in the first place but to put
    them in the constructor where they will be left alone by VS.

    Hope this helps

    Graham


     
    Graham Pengelly, Jul 9, 2004
    #3
  4. Mike

    Mike Guest

    Thanks Guys, your explanations were very helpful. I also found that removing the "Controls.Clear" statement from the "Render" method helped. With regards to the event handlers and delegates, I decided to keep them just after the instantiation of the dynamic control that I add to the Web Form.

    Again, thanks a lot.
    Mike



    Hi Mike

    Robert is right about the missing delegates. They are usually added
    automatically to the InitializeComponent method when you drag a control
    onto the form in VS.net. A word of warning though. I have had a lot of
    trouble, as have a lot of other people if you check through the
    newsgroups, with manually adding items to the InitializeComponent
    method. There is a good reason why the comment says 'do not modify
    the contents of this method with the code editor.' It seems that each
    time you go into design view, or make a change in design view, Visual
    Studio deletes and rewrites the contents of this method. I have found
    that often it isn't too successful in doing this if you have altered the
    contents of the method and consequently events seem to stop firing as
    the delegates have been erased. This has been fixed in .NET 2.0 with the
    introduction of partial classess but in 1.1 the best workaround I could
    come up with is to not put them in there in the first place but to put
    them in the constructor where they will be left alone by VS.

    Hope this helps

    Graham


     
    Mike, Jul 10, 2004
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.