473,545 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting id in a dynmaically generated checkboxlist

I'm trying to code a reapter that for each listelement show a checkboxlist.
I'm almost there. The only thing I can't figure out is how to set the ID of
the checkboxlists.

This is my code:
<asp:CheckBoxLi st id='<%#
Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>' is not
a valid identifier.
What am I doing wrong?
--
Eirik Eldorsen
Nov 18 '05 #1
5 7891
Someone asked this recently....the control creation happens BEFORE the data
is bound to it...and you need a valid ID value...so doing it that way is no
go.

Your alternative is to you the onItemDataBound event
(http://openmymind.net/databinding/index.html#4.2)

or to use a static id, and use a hidden form field and set it's value to
Container.DataI tem ID

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Eirik Eldorsen" <ku*******@hotm ail.com> wrote in message
news:0m******** ************@ne ws.telia.no...
I'm trying to code a reapter that for each listelement show a checkboxlist. I'm almost there. The only thing I can't figure out is how to set the ID of the checkboxlists.

This is my code:
<asp:CheckBoxLi st id='<%#
Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>' is not a valid identifier.
What am I doing wrong?
--
Eirik Eldorsen

Nov 18 '05 #2
You Eirik:

Can you tell us the motivation for setting the ID programatically ?
Chances are you can just use a stirng literal here, ie:

<asp:CheckBoxLi st id="CheckList" ... />

Each row in a repeater is a naming item, and will give the check box a
unique name when rendered to the client. You can still pull a
reference to the control if you use FindControl on the repeater item,
ie:

CheckBoxList list = e.Item.FindCont rol("CheckList" ) as CheckBoxList;

The place where this causes people heartburn is when writing client
side script, because the names are "munged" by ASP.NET

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Mon, 15 Nov 2004 15:57:48 GMT, "Eirik Eldorsen"
<ku*******@hotm ail.com> wrote:
I'm trying to code a reapter that for each listelement show a checkboxlist.
I'm almost there. The only thing I can't figure out is how to set the ID of
the checkboxlists.

This is my code:
<asp:CheckBoxL ist id='<%#
Convert.ToStri ng(DataBinder.E val(Container.D ataItem, "ID")) %>'
runat="serve r" .....

This code will result in the following parse error:
'<%# Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>' is not
a valid identifier.
What am I doing wrong?


Nov 18 '05 #3
The repeater and the checkboxlists are created from two tables: Categori and
Elements
Categori(ID*, Title, Explenation)
Elements(ID*, CategoriID, ElementText)

For each listitem in the repeater I want write out Title, Explenation, and a
checkboxlist containing the elementtext's. If i use a static name on the
checkboxlistid, every checkboxlist get all the elements. This is not what I
want. I could use a hidde nfield to contain the id, but I still need a uniqe
name for eah checkboxlist.

Here is my asp.net code:

<table border="0" cellpadding="5" cellspacing="0" >

<asp:Repeater id=repCategorie s runat="server">
<ItemTemplate >
<tr>
<td>
<h2><%# Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "Title"))
%></h2><br>
<i><%# Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem,
"Explenatio n")) %></i>

<asp:CheckBoxLi st id='<%#
Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>'
runat="server" DataSource='<%# DataBinder.Eval (Container.Data Item,
"Elements") %>' DataTextField=' ElementText'>

</asp:CheckBoxLis t>

</td>
</tr>

</ItemTemplate>

</asp:Repeater>
</table>
In page_load i bind the repeater:

repKategorier.D ataSource = DBFactory.GetCa tegories();
repKategorier.D ataBind();

GetCategories return an arraylist of Categori objects. A categori objects,
contains a title, explenation and and arraylist of Elements.

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:na******** *************** *********@4ax.c om...
You Eirik:

Can you tell us the motivation for setting the ID programatically ?
Chances are you can just use a stirng literal here, ie:

<asp:CheckBoxLi st id="CheckList" ... />

Each row in a repeater is a naming item, and will give the check box a
unique name when rendered to the client. You can still pull a
reference to the control if you use FindControl on the repeater item,
ie:

CheckBoxList list = e.Item.FindCont rol("CheckList" ) as CheckBoxList;

The place where this causes people heartburn is when writing client
side script, because the names are "munged" by ASP.NET

--
Scott
http://www.OdeToCode.com/blogs/scott/
On Mon, 15 Nov 2004 15:57:48 GMT, "Eirik Eldorsen"
<ku*******@hotm ail.com> wrote:
I'm trying to code a reapter that for each listelement show a
checkboxlis t.
I'm almost there. The only thing I can't figure out is how to set the ID
of
the checkboxlists.

This is my code:
<asp:CheckBox List id='<%#
Convert.ToStr ing(DataBinder. Eval(Container. DataItem, "ID")) %>'
runat="server " .....

This code will result in the following parse error:
'<%# Convert.ToStrin g(DataBinder.Ev al(Container.Da taItem, "ID")) %>' is
not
a valid identifier.
What am I doing wrong?

Nov 18 '05 #4
>
For each listitem in the repeater I want write out Title, Explenation, and a
checkboxlist containing the elementtext's. If i use a static name on the
checkboxlistid , every checkboxlist get all the elements. This is not what I
want. I could use a hidde nfield to contain the id, but I still need a uniqe
name for eah checkboxlist.


That's interesting - the ID of the control should not change the
databinding behavior in any way. Here is an example (apologies for any
wierd line breaks):

<form id="Form1" method="post" runat="server">
<table>
<asp:Repeater ID="Repeater1" Runat="server">
<ItemTemplate >
<tr>
<td><%# DataBinder.Eval (Container.Data Item, "Title") %></td>
<td><%# DataBinder.Eval (Container.Data Item, "Explanatio n")
%></td>
<td>
<asp:CheckBoxLi st ID="CheckBoxLis t1" Runat="server"
DataSource='<%# DataBinder.Eval (Container.Data Item, "Elements")
%>'
DataTextField=" Name">
</asp:CheckBoxLis t>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</form>
The code behind this form is:

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace aspnet.repeater s
{
public class checkboxlist : System.Web.UI.P age
{
protected Repeater Repeater1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!Page.IsPost Back)
{
Category[] categories = GetCategories() ;
Repeater1.DataS ource = categories;
Repeater1.DataB ind();
}
}

private Category[] GetCategories()
{
Category[] categories = new Category[3];

for(int i = 0; i < categories.Leng th; i++)
{
categories[i] = GetCategory(i);
}

return categories;
}

private Category GetCategory(int i)
{
string title = i.ToString() + "CatTitle";
string ex = i.ToString() + "Explanatio n";
Element[] elements = GetElements(i);

return new Category(title, ex, elements);
}

private Element[] GetElements(int i)
{
Element[] elements = new Element[i + 2];

for(int j = 0; j < elements.Length ; j++)
{
elements[j] = new Element(i.ToStr ing() + j.ToString()
+ "Element");
}

return elements;
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}

class Category
{
public Category(string title, string explanation,
Element[] elements)
{
this.title = title;
this.explanatio n = explanation;
this.elements = elements;
}

public string Title
{
get { return title; }
}

public string Explanation
{
get { return explanation; }
}

public Element[] Elements
{
get { return elements; }
}

private string title;
private string explanation;
private Element[] elements;
}

class Element
{
public Element(string name)
{
this.name = name;
}

public string Name
{
get { return name; }
}

private string name;
}
}
What I'll get is a Repeater with three rows. Each row has a
CheckBoxList with different elements displayed (and they all have
unique client IDs).

--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 18 '05 #5
Thank you so much for the help :-D

It was a stupid bug. I forgot the where clause in my select statment :-)

"SELECT * " +
"FROM Elementes " +
"WHERE CategoriID = " + id +

"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:p9******** *************** *********@4ax.c om...

For each listitem in the repeater I want write out Title, Explenation, and
a
checkboxlis t containing the elementtext's. If i use a static name on the
checkboxlisti d, every checkboxlist get all the elements. This is not what
I
want. I could use a hidde nfield to contain the id, but I still need a
uniqe
name for eah checkboxlist.


That's interesting - the ID of the control should not change the
databinding behavior in any way. Here is an example (apologies for any
wierd line breaks):

<form id="Form1" method="post" runat="server">
<table>
<asp:Repeater ID="Repeater1" Runat="server">
<ItemTemplate >
<tr>
<td><%# DataBinder.Eval (Container.Data Item, "Title") %></td>
<td><%# DataBinder.Eval (Container.Data Item, "Explanatio n")
%></td>
<td>
<asp:CheckBoxLi st ID="CheckBoxLis t1" Runat="server"
DataSource='<%# DataBinder.Eval (Container.Data Item, "Elements")
%>'
DataTextField=" Name">
</asp:CheckBoxLis t>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</form>
The code behind this form is:

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;

namespace aspnet.repeater s
{
public class checkboxlist : System.Web.UI.P age
{
protected Repeater Repeater1;

private void Page_Load(objec t sender, System.EventArg s e)
{
if(!Page.IsPost Back)
{
Category[] categories = GetCategories() ;
Repeater1.DataS ource = categories;
Repeater1.DataB ind();
}
}

private Category[] GetCategories()
{
Category[] categories = new Category[3];

for(int i = 0; i < categories.Leng th; i++)
{
categories[i] = GetCategory(i);
}

return categories;
}

private Category GetCategory(int i)
{
string title = i.ToString() + "CatTitle";
string ex = i.ToString() + "Explanatio n";
Element[] elements = GetElements(i);

return new Category(title, ex, elements);
}

private Element[] GetElements(int i)
{
Element[] elements = new Element[i + 2];

for(int j = 0; j < elements.Length ; j++)
{
elements[j] = new Element(i.ToStr ing() + j.ToString()
+ "Element");
}

return elements;
}

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
InitializeCompo nent();
base.OnInit(e);
}

private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}

class Category
{
public Category(string title, string explanation,
Element[] elements)
{
this.title = title;
this.explanatio n = explanation;
this.elements = elements;
}

public string Title
{
get { return title; }
}

public string Explanation
{
get { return explanation; }
}

public Element[] Elements
{
get { return elements; }
}

private string title;
private string explanation;
private Element[] elements;
}

class Element
{
public Element(string name)
{
this.name = name;
}

public string Name
{
get { return name; }
}

private string name;
}
}
What I'll get is a Repeater with three rows. Each row has a
CheckBoxList with different elements displayed (and they all have
unique client IDs).

--
Scott
http://www.OdeToCode.com/blogs/scott/

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
7934
by: Bryce Budd | last post by:
Hello All, I've been a taker of information from newsgroups for a long time and thought I'd finally make a contribution back to the community whose supported me when I've needed it. After all before commercialization took over that was the beauty of the Internet! I've create a checkboxlist validator control...something MS should have...
4
8063
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected status. David
4
6505
by: Shaul Feldman | last post by:
Hello, I have something really awkward at work - fighting with CheckBoxList... How can I define CSS for ListItem in CheckBoxList programmatically. I add CheckBoxList's Items on the fly, something like dim li as ListItem li = new ListItem("title","value"); '' how to define here the CSS for List Item, not CheckBoxList?!?!?!...
0
1723
by: Rik | last post by:
I am trying to find out if it is possible to set the Checked value of a checkboxlist when databinding. Code below works except for the setting of the checked value.... cbServer.DataTextField = "SrvName"; cbServer.DataValueField = "SrvNo"; // = "SrvChked"; cbServer.DataSource = ObjSQL.QueryDataSet("select SrvNo,SrvName,
0
2525
by: Boris | last post by:
When I dynamically create CheckButtonList, I add ListItem(s) to my CheckButtonList object chkList chkList.Items.Add(new ListItem("My Text", "My Value")); The resulting HTML doesn't contain value="My Value". Therefore, I can't access "My Value" with Request.Form on post back. I do get the desired outcome with my RadioButtonList object...
5
13375
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able to get a Boolean value TRUE or FALSE when a checkBox is selected. When the checkBoxList was out of the DataList i used "OnSelectedIndexChanged"...
4
4031
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to work:( Thanks My CheckBoxList in the DataList Below
2
5224
by: Patrick.O.Ige | last post by:
I have some boolean value(1 or 0 ) in a table and i want a databinded CheckBoxList to present the selected values on the page.. With CheckBox i know i can se the Checked property like so :- <asp:CheckBox id="chk" checked=<% Databind.Eval("This returns 1 or 0")%> runat="server"%> Any help how i can do it using CheckBoxList?
1
7917
by: terminul | last post by:
Hi I have a datalist which loops through the categories and within the datalist I have a CheckBoxList which are bound on the DataList's OnItemDataBound. The only problem I have is retrieving the values that have been checked when the user submits the form. This code works as far as presentation goes but I can't seem to retrieve the...
0
7470
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7659
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7760
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5334
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.