Ask a Question related to ASP.NET General, Design and Development.
-
Ivan Demkovitch #1
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
-
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... -
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... -
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... -
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... -
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... -
Ravikanth[MVP] #2
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
in VB.NET>-----Original Message-----
>Hi!
>
>I'm VB developer learning ASP.NET and C#
>
>Unfortunately, my book has only few examples in C#, alllike:>
>Here is a question:
>
>I'm trying to add control dinamically. In VB it looks>
>Dim CheckBoxList1 As New CheckBoxList()
>
>Controls.Add(CheckBoxList1)
>
>CheckBoxList1.Items.Add("Check1")
>
>
>
>How do I write this code in C#?
>
>
>
>
>
>
>.
>Ravikanth[MVP] Guest
-
Ivan Demkovitch #3
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
>
>> in VB.NET> >-----Original Message-----
> >Hi!
> >
> >I'm VB developer learning ASP.NET and C#
> >
> >Unfortunately, my book has only few examples in C#, all> like:> >
> >Here is a question:
> >
> >I'm trying to add control dinamically. In VB it looks> >
> >Dim CheckBoxList1 As New CheckBoxList()
> >
> >Controls.Add(CheckBoxList1)
> >
> >CheckBoxList1.Items.Add("Check1")
> >
> >
> >
> >How do I write this code in C#?
> >
> >
> >
> >
> >
> >
> >.
> >
Ivan Demkovitch Guest
-
Kevin Spencer #4
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...class> 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> 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
> >
> >> > in VB.NET> > >-----Original Message-----
> > >Hi!
> > >
> > >I'm VB developer learning ASP.NET and C#
> > >
> > >Unfortunately, my book has only few examples in C#, all> > like:> > >
> > >Here is a question:
> > >
> > >I'm trying to add control dinamically. In VB it looks> > >
> > >Dim CheckBoxList1 As New CheckBoxList()
> > >
> > >Controls.Add(CheckBoxList1)
> > >
> > >CheckBoxList1.Items.Add("Check1")
> > >
> > >
> > >
> > >How do I write this code in C#?
> > >
> > >
> > >
> > >
> > >
> > >
> > >.
> > >
>
Kevin Spencer Guest
-
Ivan Demkovitch #5
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
-
Kevin Spencer #6
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...on> 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> the page.
>
> How it suppose to be done?
>
>
>
>
>
>
>
>
>
Kevin Spencer Guest



Reply With Quote

