Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamic linkbuttons and event wiring

Dynamic linkbuttons and event wiring

Scheduled Pinned Locked Moved ASP.NET
csharpdesignsecurityhelptutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Glenn E Lanier II
    wrote on last edited by
    #1

    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

    C 1 Reply Last reply
    0
    • G Glenn E Lanier II

      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

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      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 )

      G 2 Replies Last reply
      0
      • C Christian Graus

        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 )

        G Offline
        G Offline
        Glenn E Lanier II
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • C Christian Graus

          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 )

          G Offline
          G Offline
          Glenn E Lanier II
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups