Add control using C# (newbie)

I

Ivan Demkovitch

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#?
 
R

Ravikanth[MVP]

Hi

Modified code in C#:

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



HTH

Ravikanth
 
I

Ivan Demkovitch

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..
 
K

Kevin Spencer

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
http://www.takempis.com
Big things are made up of
lots of little things.
 
I

Ivan Demkovitch

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?
 
K

Kevin Spencer

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
http://www.takempis.com
Big things are made up of
lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,095
Messages
2,570,616
Members
47,232
Latest member
helpplease!

Latest Threads

Top