Dynamic linkbuttons and event wiring
-
I have searched, with no success, for the answer to my problem. I want to dynamically create a linkbutton and programatically set the command to fire when the button is clicked. I can create the button, but the only event that seems to fire is the Page_Load. A simple example is below: ---- File: Test1.aspx ----
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" Inherits="ReportCard.Test1" %> Untitled Page
---- File: Test1.aspx.cs ----using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NLog; using System.Text; namespace ReportCard { public partial class Test1 : System.Web.UI.Page { public static Logger logger = LogManager.GetCurrentClassLogger(); protected void Page_Load(object sender, EventArgs e) { logger.Info("Page_Load: Postback: {0}", Page.IsPostBack.ToString()); } protected void Button1_Click(object sender, EventArgs e) { logger.Info("Button1_Click"); Table table = new Table(); int i = 1; TableRow tr = new TableRow(); TableCell tdSysCode = new TableCell(); tdSysCode.CssClass = "tdSysCode"; tdSysCode.Text = string.Format("{0:000}", i); TableCell tdSysname = new TableCell(); tdSysname.CssClass = "tdSysname"; LinkButton lb = new LinkButton(); lb.CommandName = "lbViewSystem_Click"; lb.CommandArgument = i.ToString(); lb.Command += new CommandEventHandler(lbViewSystem_Click); lb.Text = string.Format("System {0:000}", i); tdSysname.Controls.Add(lb); tr.Cells.Add(tdSysCode); tr.Cells.Add(tdSysname); table.Rows.Add(tr); table.ID = "tblSystemResults"; table.CellPadding = 0; table.CellSpacing = 0; TableHeaderRow thrHeader = new TableHeaderRow(); TableHeaderCell thSystem
-
I have searched, with no success, for the answer to my problem. I want to dynamically create a linkbutton and programatically set the command to fire when the button is clicked. I can create the button, but the only event that seems to fire is the Page_Load. A simple example is below: ---- File: Test1.aspx ----
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test1.aspx.cs" Inherits="ReportCard.Test1" %> Untitled Page
---- File: Test1.aspx.cs ----using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using NLog; using System.Text; namespace ReportCard { public partial class Test1 : System.Web.UI.Page { public static Logger logger = LogManager.GetCurrentClassLogger(); protected void Page_Load(object sender, EventArgs e) { logger.Info("Page_Load: Postback: {0}", Page.IsPostBack.ToString()); } protected void Button1_Click(object sender, EventArgs e) { logger.Info("Button1_Click"); Table table = new Table(); int i = 1; TableRow tr = new TableRow(); TableCell tdSysCode = new TableCell(); tdSysCode.CssClass = "tdSysCode"; tdSysCode.Text = string.Format("{0:000}", i); TableCell tdSysname = new TableCell(); tdSysname.CssClass = "tdSysname"; LinkButton lb = new LinkButton(); lb.CommandName = "lbViewSystem_Click"; lb.CommandArgument = i.ToString(); lb.Command += new CommandEventHandler(lbViewSystem_Click); lb.Text = string.Format("System {0:000}", i); tdSysname.Controls.Add(lb); tr.Cells.Add(tdSysCode); tr.Cells.Add(tdSysname); table.Rows.Add(tr); table.ID = "tblSystemResults"; table.CellPadding = 0; table.CellSpacing = 0; TableHeaderRow thrHeader = new TableHeaderRow(); TableHeaderCell thSystem
Any dynamically created button must be created before page load ( usually in loadviewstate, before viewstate is loaded ) in order for it's events to fire, because the button has to exist when viewstate is restored, in order for it's events to hook up properly.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Any dynamically created button must be created before page load ( usually in loadviewstate, before viewstate is loaded ) in order for it's events to fire, because the button has to exist when viewstate is restored, in order for it's events to hook up properly.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Christian, I must be really dense here -- I've tried overriding the OnInit and/or the LoadViewState, and recreating the object in every Page_Load, but still no success. Currently, I have a member variable, LinkButton lb. I have a method, CreateButton, that creates the button, sets the properties, and creates the CommandEventHandler. I call this method in various places, but no luck. What am I missing?
protected LinkButton lb = null; protected override void LoadViewState(object savedState) { CreateButton(); base.LoadViewState(savedState); } protected void CreateButton() { if (null == lb) { lb = new LinkButton(); } lb.ID = "lbTest001"; lb.CommandName = "lbViewSystem_Click"; lb.CommandArgument = "1"; lb.Command += new CommandEventHandler(lbViewSystem_Click); lb.Text = string.Format("System {0:000}", 1); } protected void lbViewSystem_Click(object sender, CommandEventArgs e) { logger.Info("lbViewSystem_Click"); Literal1.Text = string.Format("Argument = {0} at {1}", e.CommandArgument, DateTime.Now.ToString("MM.dd.yyyy HHmmss")); }
Again, any help is very much appreciated. --G -
Any dynamically created button must be created before page load ( usually in loadviewstate, before viewstate is loaded ) in order for it's events to fire, because the button has to exist when viewstate is restored, in order for it's events to hook up properly.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
I finally got this to work -- not only did I have to recreate the buttons in the OnInit, but I also had to add the buttons to a control on the page. Looking back, that makes some sense. Thanks for the pointer -- I don't think I would have placed it in the OnInit. --G