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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Programmically Adding Controls

Programmically Adding Controls

Scheduled Pinned Locked Moved ASP.NET
htmlsysadminjsonhelptutorial
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.
  • D Offline
    D Offline
    Drathmar
    wrote on last edited by
    #1

    I've come across this odd behavior, that I'm not quite sure what to make of it, or even how to determine the source of the problem. Ok, I have one page. 3 different buttons on that page. asp:Panel ID="PanelNew" runat="server" Width="97%" > asp:button runat="server" text="MostRecent" OnCommand="SelectActiveView" CommandArgument="MostRecent" CommandName="Hide" /> asp:button runat="server" text="Most Recent 2" id="testbutton" /> /asp:Panel> Then I had code in the page load that is: testbutton.Command += new CommandEventHandler(this.SelectActiveView); testbutton.CommandArgument = "MostRecent"; testbutton.CommandName = "Hide"; Button TempButton = new Button(); TempButton.Text = "Cancel"; TempButton.Command += new CommandEventHandler(this.SelectActiveView); TempButton.CommandArgument = "MostRecent"; TempButton.CommandName = "Hide"; PanelNew.Controls.Add(TempButton); So, I have on that's declared in html, one that's declared in html then modified from code, then the last is complete code made, all three buttons end up having the same command arguements, but the one made from code behaves differently from the rest. The function they call, modifies some hiden value fields then calls a page load. Ok, now this part, is hard to explain, so please stay with me. If I go straight to that page from a link, all three buttons work, first try. If I click on a button that does a post back on to the page, then I click on the tempbutton, the hidden field values are not updated, Then I click the same button a second time and it does update the hidden values like it was supposed to on the first click. Well, I hope that made sense. If it didn't then post that and I'll see if i can post some source code to duplicate this behavior. Drathmar

    M 1 Reply Last reply
    0
    • D Drathmar

      I've come across this odd behavior, that I'm not quite sure what to make of it, or even how to determine the source of the problem. Ok, I have one page. 3 different buttons on that page. asp:Panel ID="PanelNew" runat="server" Width="97%" > asp:button runat="server" text="MostRecent" OnCommand="SelectActiveView" CommandArgument="MostRecent" CommandName="Hide" /> asp:button runat="server" text="Most Recent 2" id="testbutton" /> /asp:Panel> Then I had code in the page load that is: testbutton.Command += new CommandEventHandler(this.SelectActiveView); testbutton.CommandArgument = "MostRecent"; testbutton.CommandName = "Hide"; Button TempButton = new Button(); TempButton.Text = "Cancel"; TempButton.Command += new CommandEventHandler(this.SelectActiveView); TempButton.CommandArgument = "MostRecent"; TempButton.CommandName = "Hide"; PanelNew.Controls.Add(TempButton); So, I have on that's declared in html, one that's declared in html then modified from code, then the last is complete code made, all three buttons end up having the same command arguements, but the one made from code behaves differently from the rest. The function they call, modifies some hiden value fields then calls a page load. Ok, now this part, is hard to explain, so please stay with me. If I go straight to that page from a link, all three buttons work, first try. If I click on a button that does a post back on to the page, then I click on the tempbutton, the hidden field values are not updated, Then I click the same button a second time and it does update the hidden values like it was supposed to on the first click. Well, I hope that made sense. If it didn't then post that and I'll see if i can post some source code to duplicate this behavior. Drathmar

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      A couple of things come to mind: + Try to give the control an id when you statically declare on the web page or dynamically load the control in code. + Try to add the dynamic control in the Init phase in the control life cycle. + Just curious that why you try with 3 buttons that have the same command name and argument.

      D 1 Reply Last reply
      0
      • M minhpc_bk

        A couple of things come to mind: + Try to give the control an id when you statically declare on the web page or dynamically load the control in code. + Try to add the dynamic control in the Init phase in the control life cycle. + Just curious that why you try with 3 buttons that have the same command name and argument.

        D Offline
        D Offline
        Drathmar
        wrote on last edited by
        #3

        My problem in a nutshell is the programmatically created controls aren't working 100% of the time. NOTE: Giving the Programmically made controls an id fixed the problem, and thank you minhpc_bk for the help. However I made some sample code to demonstrate the problem, what's cause the problem, is still abit puzzling, To replicate the problem: When the page first loaded the hidden value is blank, then I click on ButtonR1 or R2 to make it red. So on and so on. Now, if you start clicking on the buttons in the "Panel Created" panel, they don't work 100% of the time. You have to click the same button twice in a row for it to work. Drathmar Here is a simplified example of my problem. *****************HTML************** <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> Untitled Page

        ***************** Code ************** 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.WebControls.WebParts; using S

        M 1 Reply Last reply
        0
        • D Drathmar

          My problem in a nutshell is the programmatically created controls aren't working 100% of the time. NOTE: Giving the Programmically made controls an id fixed the problem, and thank you minhpc_bk for the help. However I made some sample code to demonstrate the problem, what's cause the problem, is still abit puzzling, To replicate the problem: When the page first loaded the hidden value is blank, then I click on ButtonR1 or R2 to make it red. So on and so on. Now, if you start clicking on the buttons in the "Panel Created" panel, they don't work 100% of the time. You have to click the same button twice in a row for it to work. Drathmar Here is a simplified example of my problem. *****************HTML************** <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> Untitled Page

          ***************** Code ************** 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.WebControls.WebParts; using S

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Hi there, Basically, the ASP.NET processes the postback event like Command based on the id of the control. When you dynamically create and add the control, the ASP.NET will generate the automatic id if you don't explicitly specify a value. And if the id of the control before and after it is clicked, the postback cannot be routed to the right control, and as a result of this the event handler does not execute. You can check this thing by doing a view source the web page. Here it's really odd that you call the Page_Load event handler again in the SetPanelColor as it leads to the fact that you recreate the dynamic control. Instead, you can seperate the code to set the backcolor of the panel in a method ,say SetPanelBackColor, then you can call it in both the Page_Load and SetPanelColor methods.

          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