Results 1 to 7 of 7

Thread: dynamic web controls

  1. #1

    Thread Starter
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548

    dynamic web controls

    how can i add about 50 checkbox controls to a web page dynamically and have them with events?


    is this a possibility?

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    yes

  3. #3

    Thread Starter
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548
    Thank you for the answer to Part B of my post -

    Is there someone who can show me or point me in the direction of the answer to Part A ?


    Thanks

  4. #4
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

  5. #5

    Thread Starter
    Fanatic Member ZeBula8's Avatar
    Join Date
    Oct 2002
    Posts
    548
    based on the code below that is taken from the link -
    how would i create my 50 controls - they would all need a separate ID -

    Is the intention to put a for-loop that adds my 50 controls with a new ID in the Page_Load ?




    Code:
    namespace HowTos.Button
    {
     public class WebForm1 : System.Web.UI.Page
     {
    
      #region User Defined Code
    
      System.Web.UI.HtmlControls.HtmlForm form;
    
      private void Page_Load( System.Object sender, System.EventArgs e )
      {
        MyButton button = new MyButton();
        button.ID = "Button1";
        button.Click += new System.EventHandler( this.button_click );
        form.Controls.Add( button );
      }
    
      private void button_click( System.Object sender, System.EventArgs e )  
      {
        System.Web.UI.WebControls.Label lblMessage = new System.Web.UI.WebControls.Label();
        lblMessage.ID = "lblMessage";
        lblMessage.Text = "Button_Click Event Fired From Dynamically Created Button";
        form.Controls.Add( new System.Web.UI.LiteralControl( "<P>" ) );
        form.Controls.Add( lblMessage );
      }
    
      #endregion
    
      #region Web Form Designer generated code
    
      override protected void OnInit( System.EventArgs e )
      {
        InitializeComponent();
        base.OnInit( e );
      }
    
      private void InitializeComponent()
      {  
        this.Load += new System.EventHandler( this.Page_Load );
        form = ( System.Web.UI.HtmlControls.HtmlForm )this.FindControl( "Form1" );
      }
    
      #endregion
      }
    }

  6. #6
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    You can give them all a seperate ID. Just increment an integer, append it to the id field, and hook them all back to the same handler, where you can ctype the sender to a checkbox, and check its id field, and doing different things using a select case statement on its id.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    VB Code:
    1. Private Sub Page_Load(byval sender as object, byval e as eventargs)
    2.  _ Handles Mybase.Load
    3.  
    4. If Not IsPostBack Then
    5. For i = 1 to 50
    6.   'make new checkbox
    7.   Dim MyNewCheckBox As New System.Web.UI.CheckBox
    8.   'set whatever properties you want
    9.   'set its id
    10.   MyNewCheckbox.Id = "mycheckbox" & i.ToString
    11.   AddHandler myNewCheckBox.Click, AddressOf Mycheckboxhandler
    12. Next
    13. End If
    14.  
    15. End Sub
    16.  
    17. Private Sub myCheckBoxHandler (sender as Object, e as system.eventargs)
    18.  'narrow object to your checkbox
    19.  Dim c As mycheckbox = CType(sender,checkbox)
    20.  'do a select based on the id
    21.  Select (c.ID)
    22.   Case "mycheckbox1"
    23.     'do something
    24.  
    25.   Case "mycheckbox2"
    26.    'do something else
    27.  
    28.  
    29.   End Select
    30. End Sub
    Last edited by nemaroller; Dec 15th, 2003 at 07:48 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width