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. Making dynamic controls and catching their events

Making dynamic controls and catching their events

Scheduled Pinned Locked Moved ASP.NET
csharphelpc++asp-netdebugging
6 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.
  • R Offline
    R Offline
    Roger Jane
    wrote on last edited by
    #1

    Hi All, Any help would be cool - there are 3 questions in here, an answer to any one would be great! I'm new at this (ASP.NET and C#) and I feel that I'm making a meal out of something more simple. Perhaps one of you fellow could give me a pointer or two... I have loads of experience with C++ and so forth so there's no need to be too gentle. I need to pop up a general 'Picker' for the user to pick one from a number of options. I didn't want to use a new browser window as I have no control over it so I'm using an absolutely positioned 'DIV'. I'm adding this to a placeholder on the page when I need it using code like: HtmlGenericControl div1 = new HtmlGenericControl(); div1.ID = "Picker"; div1.Attributes.Add("class", "box"); div1.Style.Add("left", "700px"); ... ListBox lb1 = new ListBox(); lb1.ID = "PickerBox"; lb1.AutoPostBack = true; * lb1.SelectedIndexChanged += new System.EventHandler(HavePicked); div1.Controls.Add(lb1); ... myPlaceHolder.Controls.Add(div1); So that I can catch the effect of the user picking one of these elements, I have the line above that is marked with a '*'. (The code has other controls in the structure (there's a table below the 'div' and above the 'listbox' for example) but this is just illustrative. Now for the questions/problem: 1. "HavePicked()" is never called. The picker appears on the screen lovely and when I click on an option it disappears too. However, my code doesn't seem to notice. A breakpoint in 'HavePicked()' never gets seen. 2. It's very laborious adding in each element and attribute 'by hand' as I've done above. I feel there must be a way that I can do something like: pickerCode = @"<div ID=""picker"" class=""box"" style=""left:700px;top:150px""><asp:ListBox ... </div>"; and using that. I haven't spotted it though. 3. SelectedIndexChanged is not really what I want. I'd like 'UserHasPickedSomething' because even in static, non-dynamically generated ListBoxes I only seem to get a postback when they chose a different option. I'd like to know if they hit return on the currently selected option or double clicked it. So, if anyone can give me some hints here then I'd be very grateful! Thanks Rog -- modified at 8:50 Thursday 19th January, 2006

    I 1 Reply Last reply
    0
    • R Roger Jane

      Hi All, Any help would be cool - there are 3 questions in here, an answer to any one would be great! I'm new at this (ASP.NET and C#) and I feel that I'm making a meal out of something more simple. Perhaps one of you fellow could give me a pointer or two... I have loads of experience with C++ and so forth so there's no need to be too gentle. I need to pop up a general 'Picker' for the user to pick one from a number of options. I didn't want to use a new browser window as I have no control over it so I'm using an absolutely positioned 'DIV'. I'm adding this to a placeholder on the page when I need it using code like: HtmlGenericControl div1 = new HtmlGenericControl(); div1.ID = "Picker"; div1.Attributes.Add("class", "box"); div1.Style.Add("left", "700px"); ... ListBox lb1 = new ListBox(); lb1.ID = "PickerBox"; lb1.AutoPostBack = true; * lb1.SelectedIndexChanged += new System.EventHandler(HavePicked); div1.Controls.Add(lb1); ... myPlaceHolder.Controls.Add(div1); So that I can catch the effect of the user picking one of these elements, I have the line above that is marked with a '*'. (The code has other controls in the structure (there's a table below the 'div' and above the 'listbox' for example) but this is just illustrative. Now for the questions/problem: 1. "HavePicked()" is never called. The picker appears on the screen lovely and when I click on an option it disappears too. However, my code doesn't seem to notice. A breakpoint in 'HavePicked()' never gets seen. 2. It's very laborious adding in each element and attribute 'by hand' as I've done above. I feel there must be a way that I can do something like: pickerCode = @"<div ID=""picker"" class=""box"" style=""left:700px;top:150px""><asp:ListBox ... </div>"; and using that. I haven't spotted it though. 3. SelectedIndexChanged is not really what I want. I'd like 'UserHasPickedSomething' because even in static, non-dynamically generated ListBoxes I only seem to get a postback when they chose a different option. I'd like to know if they hit return on the currently selected option or double clicked it. So, if anyone can give me some hints here then I'd be very grateful! Thanks Rog -- modified at 8:50 Thursday 19th January, 2006

      I Offline
      I Offline
      Ista
      wrote on last edited by
      #2

      I'll be honest. I didnt read the whole thing. But a label doesnt create a postback. You need to add an AutoPostBack property. This property is actually found in the List and TextBox controls. Java has a method called '__doPostBack' that forces a post back. Check out this article: http://www.dotnetspider.com/Technology/KBPages/195.aspx[^] 1 line of code equals many bugs. So don't write any!!

      R 1 Reply Last reply
      0
      • I Ista

        I'll be honest. I didnt read the whole thing. But a label doesnt create a postback. You need to add an AutoPostBack property. This property is actually found in the List and TextBox controls. Java has a method called '__doPostBack' that forces a post back. Check out this article: http://www.dotnetspider.com/Technology/KBPages/195.aspx[^] 1 line of code equals many bugs. So don't write any!!

        R Offline
        R Offline
        Roger Jane
        wrote on last edited by
        #3

        Thanks for reading some of it anyway! The problem isn't whether it generates a callback (it does), but it doesn't generate the event. Or, if it does, I'm not catching it. I'm sure there's a simple answer... Rog

        I 1 Reply Last reply
        0
        • R Roger Jane

          Thanks for reading some of it anyway! The problem isn't whether it generates a callback (it does), but it doesn't generate the event. Or, if it does, I'm not catching it. I'm sure there's a simple answer... Rog

          I Offline
          I Offline
          Ista
          wrote on last edited by
          #4

          Add the handler for selected index changed and set the AutoPostBack to true. That will solve it 1 line of code equals many bugs. So don't write any!!

          R 1 Reply Last reply
          0
          • I Ista

            Add the handler for selected index changed and set the AutoPostBack to true. That will solve it 1 line of code equals many bugs. So don't write any!!

            R Offline
            R Offline
            Roger Jane
            wrote on last edited by
            #5

            No, got that... ListBox lb1 = new ListBox(); lb1.Style.Add("background-color", "InfoBackground"); lb1.Style.Add("color", "InfoText"); lb1.ID = "PickerBox"; **lb1.AutoPostBack = true;** lb1.Attributes.Add("height", "200px"); **lb1.SelectedIndexChanged += new System.EventHandler(HavePicked);** cell1.Controls.Add(lb1); Think of anything else I'm doing that's stupid - there must be something! Rog

            I 1 Reply Last reply
            0
            • R Roger Jane

              No, got that... ListBox lb1 = new ListBox(); lb1.Style.Add("background-color", "InfoBackground"); lb1.Style.Add("color", "InfoText"); lb1.ID = "PickerBox"; **lb1.AutoPostBack = true;** lb1.Attributes.Add("height", "200px"); **lb1.SelectedIndexChanged += new System.EventHandler(HavePicked);** cell1.Controls.Add(lb1); Think of anything else I'm doing that's stupid - there must be something! Rog

              I Offline
              I Offline
              Ista
              wrote on last edited by
              #6

              The reason is more than likely its not in a form tag. Paste this code. private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here Table t = new Table(); TableRow row = new TableRow(); TableCell cell = new TableCell(); ListBox lst = new ListBox(); lst.Style.Add("background-color", "InfoBackground"); lst.Style.Add("color", "InfoText"); lst.ID = "PickerBox"; lst.AutoPostBack = true; lst.Attributes.Add("height", "200px"); lst.SelectedIndexChanged += new System.EventHandler(HavePicked); lst.Items.Add(new ListItem("A")); lst.Items.Add(new ListItem("B")); System.Web.UI.HtmlControls.HtmlForm f = (System.Web.UI.HtmlControls.HtmlForm)FindControl( "Form1" ); cell.Controls.Add( lst ); row.Cells.Add( cell ); t.Rows.Add( row ); f.Controls.Add( t ); } private void HavePicked( object sender, System.EventArgs e ) { int i=0; } Also if you dont have any items in the list box the event wont fire because you cant change the index. Maybe this hepls. Nick 1 line of code equals many bugs. So don't write any!!

              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