Add control using C# (newbie)

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Add control using C# (newbie)

    Hi!

    I'm VB developer learning ASP.NET and C#

    Unfortunately, my book has only few examples in C#, all in VB.NET

    Here is a question:

    I'm trying to add control dinamically. In VB it looks like:

    Dim CheckBoxList1 As New CheckBoxList()

    Controls.Add(CheckBoxList1)

    CheckBoxList1.Items.Add("Check1")



    How do I write this code in C#?






    Ivan Demkovitch Guest

  2. Similar Questions and Discussions

    1. Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback
      Newbie:Access custom Itemplate(not datagrid/repeater/datalist) control values on postback Hi, I have simple Itemplate implementation with 2...
    2. User Control Populate Form after Log On - newbie question
      Hi, I have a user control that contains a login element, textboxes for username and password, and a button to submit, which appears in the header...
    3. Page_Load called more than once for user control - newbie question
      Hi, I have a page with a user control as header, which in turn has a user control drop down list. When I run the page in debug with a break on...
    4. User control newbie question - pls help
      I have a user control with a label and a method. The webform should call the method on the user control and populate the label. I am getting object...
    5. Using Table control in a custom composite control. Control does not render properly in design time.
      All, I have written a very simple custom composite control that includes a control of type System.Web.UI.WebControls.Table. The control...
  3. #2

    Default Add control using C# (newbie)

    Hi

    Modified code in C#:

    CheckBoxList1 = new System.Web.UI.WebControls.CheckBoxList
    ();
    Controls.Add(CheckBoxList1);
    CheckBoxList1.Items.Add("Check1");



    HTH

    Ravikanth

    >-----Original Message-----
    >Hi!
    >
    >I'm VB developer learning ASP.NET and C#
    >
    >Unfortunately, my book has only few examples in C#, all
    in VB.NET
    >
    >Here is a question:
    >
    >I'm trying to add control dinamically. In VB it looks
    like:
    >
    >Dim CheckBoxList1 As New CheckBoxList()
    >
    >Controls.Add(CheckBoxList1)
    >
    >CheckBoxList1.Items.Add("Check1")
    >
    >
    >
    >How do I write this code in C#?
    >
    >
    >
    >
    >
    >
    >.
    >
    Ravikanth[MVP] Guest

  4. #3

    Default Re: Add control using C# (newbie)

    Thanks!

    I'm using WebMatrix for development (I think it's good to spend time and
    understand files and syntax)
    and steel have problem(CS0103: The name 'chkBox' does not exist in the class
    or namespace 'ASP.frmLogin_aspx')...

    Here is all page:

    --------------------------------
    <%@ Page Language="C#" %>
    <script runat="server">

    private void Page_Load(object sender, System.EventArgs e)
    {


    chkBox = new System.Web.UI.WebControls.CheckBoxList();
    Controls.Add(chkBox);
    chkBox.Items.Add("Check1");


    }

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">
    <asp:Button id="cmdOk" runat="server" Text="Button"></asp:Button>
    <!-- Insert content here -->
    </form>
    </body>
    </html>


    -------------------------------


    What is wrong here?

    .... Sorry, I know it's all stupid questions..




    "Ravikanth[MVP]" <dvravikanth@hotmail.com> wrote in message
    news:0e6301c34d48$8f2d98a0$a501280a@phx.gbl...
    > Hi
    >
    > Modified code in C#:
    >
    > CheckBoxList1 = new System.Web.UI.WebControls.CheckBoxList
    > ();
    > Controls.Add(CheckBoxList1);
    > CheckBoxList1.Items.Add("Check1");
    >
    >
    >
    > HTH
    >
    > Ravikanth
    >
    >
    > >-----Original Message-----
    > >Hi!
    > >
    > >I'm VB developer learning ASP.NET and C#
    > >
    > >Unfortunately, my book has only few examples in C#, all
    > in VB.NET
    > >
    > >Here is a question:
    > >
    > >I'm trying to add control dinamically. In VB it looks
    > like:
    > >
    > >Dim CheckBoxList1 As New CheckBoxList()
    > >
    > >Controls.Add(CheckBoxList1)
    > >
    > >CheckBoxList1.Items.Add("Check1")
    > >
    > >
    > >
    > >How do I write this code in C#?
    > >
    > >
    > >
    > >
    > >
    > >
    > >.
    > >

    Ivan Demkovitch Guest

  5. #4

    Default Re: Add control using C# (newbie)

    Yes, you were given some wrong code there. The first thing you need to do
    before assigning a value to a variable is to DECLARE it! See below:

    CheckBoxList CheckBox1 = new CheckBoxList();
    // We declare the CheckBoxList control with the type first
    // This is similar to adding "As CheckBoxList" to the definition in VB.Net -
    // but you didn't do that because
    // You had option strict turned off - C# ALWAYS requires strong typing
    Controls.Add(CheckBoxList1);
    CheckBoxList1.Items.Add("Check1");

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Big things are made up of
    lots of little things.

    "Ivan Demkovitch" <i@d> wrote in message
    news:ORgnXuUTDHA.1992@TK2MSFTNGP12.phx.gbl...
    > Thanks!
    >
    > I'm using WebMatrix for development (I think it's good to spend time and
    > understand files and syntax)
    > and steel have problem(CS0103: The name 'chkBox' does not exist in the
    class
    > or namespace 'ASP.frmLogin_aspx')...
    >
    > Here is all page:
    >
    > --------------------------------
    > <%@ Page Language="C#" %>
    > <script runat="server">
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    >
    >
    > chkBox = new System.Web.UI.WebControls.CheckBoxList();
    > Controls.Add(chkBox);
    > chkBox.Items.Add("Check1");
    >
    >
    > }
    >
    > </script>
    > <html>
    > <head>
    > </head>
    > <body>
    > <form runat="server">
    > <asp:Button id="cmdOk" runat="server" Text="Button"></asp:Button>
    > <!-- Insert content here -->
    > </form>
    > </body>
    > </html>
    >
    >
    > -------------------------------
    >
    >
    > What is wrong here?
    >
    > ... Sorry, I know it's all stupid questions..
    >
    >
    >
    >
    > "Ravikanth[MVP]" <dvravikanth@hotmail.com> wrote in message
    > news:0e6301c34d48$8f2d98a0$a501280a@phx.gbl...
    > > Hi
    > >
    > > Modified code in C#:
    > >
    > > CheckBoxList1 = new System.Web.UI.WebControls.CheckBoxList
    > > ();
    > > Controls.Add(CheckBoxList1);
    > > CheckBoxList1.Items.Add("Check1");
    > >
    > >
    > >
    > > HTH
    > >
    > > Ravikanth
    > >
    > >
    > > >-----Original Message-----
    > > >Hi!
    > > >
    > > >I'm VB developer learning ASP.NET and C#
    > > >
    > > >Unfortunately, my book has only few examples in C#, all
    > > in VB.NET
    > > >
    > > >Here is a question:
    > > >
    > > >I'm trying to add control dinamically. In VB it looks
    > > like:
    > > >
    > > >Dim CheckBoxList1 As New CheckBoxList()
    > > >
    > > >Controls.Add(CheckBoxList1)
    > > >
    > > >CheckBoxList1.Items.Add("Check1")
    > > >
    > > >
    > > >
    > > >How do I write this code in C#?
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >.
    > > >
    >
    >

    Kevin Spencer Guest

  6. #5

    Default Re: Add control using C# (newbie)

    Thanks Kevin, looks like it closer, but steel error:
    System.Web.HttpException: Control '_ctl1_0' of type 'CheckBox' must be
    placed inside a form tag with runat=server.


    And my page code is:

    -------------------------------------------
    <%@ Page Language="C#" %>
    <script runat="server">

    private void Page_Load(object sender, System.EventArgs e)
    {


    CheckBoxList chkBox = new CheckBoxList();
    // We declare the CheckBoxList control with the type first
    // This is similar to adding "As CheckBoxList" to the definition in
    VB.Net -
    // but you didn't do that because
    // You had option strict turned off - C# ALWAYS requires strong typing
    Controls.Add(chkBox);
    chkBox.Items.Add("Check1");


    }

    </script>
    <html>
    <head>
    </head>
    <body>
    <form runat="server">
    <asp:Button id="cmdOk" runat="server" Text="Button"></asp:Button>
    <!-- Insert content here -->
    </form>
    </body>
    </html>
    ------------------------------------------



    I'm just playing with little code snippets. he idea is to ad this control on
    the page.

    How it suppose to be done?









    Ivan Demkovitch Guest

  7. #6

    Default Re: Add control using C# (newbie)

    You need to add the Control to the Controls Collection of a Control in the
    Page. For example, try putting a Panel inthe Page, and add the CheckBox to
    the Panel's Controls Collection.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    [url]http://www.takempis.com[/url]
    Big things are made up of
    lots of little things.

    "Ivan Demkovitch" <i@d> wrote in message
    news:ew2U1mVTDHA.1552@TK2MSFTNGP12.phx.gbl...
    > Thanks Kevin, looks like it closer, but steel error:
    > System.Web.HttpException: Control '_ctl1_0' of type 'CheckBox' must be
    > placed inside a form tag with runat=server.
    >
    >
    > And my page code is:
    >
    > -------------------------------------------
    > <%@ Page Language="C#" %>
    > <script runat="server">
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    >
    >
    > CheckBoxList chkBox = new CheckBoxList();
    > // We declare the CheckBoxList control with the type first
    > // This is similar to adding "As CheckBoxList" to the definition in
    > VB.Net -
    > // but you didn't do that because
    > // You had option strict turned off - C# ALWAYS requires strong typing
    > Controls.Add(chkBox);
    > chkBox.Items.Add("Check1");
    >
    >
    > }
    >
    > </script>
    > <html>
    > <head>
    > </head>
    > <body>
    > <form runat="server">
    > <asp:Button id="cmdOk" runat="server" Text="Button"></asp:Button>
    > <!-- Insert content here -->
    > </form>
    > </body>
    > </html>
    > ------------------------------------------
    >
    >
    >
    > I'm just playing with little code snippets. he idea is to ad this control
    on
    > the page.
    >
    > How it suppose to be done?
    >
    >
    >
    >
    >
    >
    >
    >
    >

    Kevin Spencer Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139