473,397 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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:CheckBoxList id='<%#
Convert.ToString(DataBinder.Eval(Container.DataIte m, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToString(DataBinder.Eval(Container.DataIte m, "ID")) %>' is not
a valid identifier.
What am I doing wrong?
--
Eirik Eldorsen
Nov 18 '05 #1
5 7882
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.DataItem ID

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Eirik Eldorsen" <ku*******@hotmail.com> wrote in message
news:0m********************@news.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:CheckBoxList id='<%#
Convert.ToString(DataBinder.Eval(Container.DataIte m, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToString(DataBinder.Eval(Container.DataIte m, "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:CheckBoxList 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.FindControl("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*******@hotmail.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:CheckBoxList id='<%#
Convert.ToString(DataBinder.Eval(Container.DataIt em, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToString(DataBinder.Eval(Container.DataIte m, "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=repCategories runat="server">
<ItemTemplate>
<tr>
<td>
<h2><%# Convert.ToString(DataBinder.Eval(Container.DataIte m, "Title"))
%></h2><br>
<i><%# Convert.ToString(DataBinder.Eval(Container.DataIte m,
"Explenation")) %></i>

<asp:CheckBoxList id='<%#
Convert.ToString(DataBinder.Eval(Container.DataIte m, "ID")) %>'
runat="server" DataSource='<%# DataBinder.Eval(Container.DataItem,
"Elements") %>' DataTextField='ElementText'>

</asp:CheckBoxList>

</td>
</tr>

</ItemTemplate>

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

repKategorier.DataSource = DBFactory.GetCategories();
repKategorier.DataBind();

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.com...
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:CheckBoxList 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.FindControl("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*******@hotmail.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:CheckBoxList id='<%#
Convert.ToString(DataBinder.Eval(Container.DataI tem, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToString(DataBinder.Eval(Container.DataIte m, "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.DataItem, "Title") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Explanation")
%></td>
<td>
<asp:CheckBoxList ID="CheckBoxList1" Runat="server"
DataSource='<%#DataBinder.Eval(Container.DataItem, "Elements")
%>'
DataTextField="Name">
</asp:CheckBoxList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</form>
The code behind this form is:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace aspnet.repeaters
{
public class checkboxlist : System.Web.UI.Page
{
protected Repeater Repeater1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Category[] categories = GetCategories();
Repeater1.DataSource = categories;
Repeater1.DataBind();
}
}

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

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

return categories;
}

private Category GetCategory(int i)
{
string title = i.ToString() + "CatTitle";
string ex = i.ToString() + "Explanation";
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.ToString() + j.ToString()
+ "Element");
}

return elements;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}

class Category
{
public Category(string title, string explanation,
Element[] elements)
{
this.title = title;
this.explanation = 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.com...

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.DataItem, "Title") %></td>
<td><%# DataBinder.Eval(Container.DataItem, "Explanation")
%></td>
<td>
<asp:CheckBoxList ID="CheckBoxList1" Runat="server"
DataSource='<%#DataBinder.Eval(Container.DataItem, "Elements")
%>'
DataTextField="Name">
</asp:CheckBoxList>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</form>
The code behind this form is:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace aspnet.repeaters
{
public class checkboxlist : System.Web.UI.Page
{
protected Repeater Repeater1;

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
Category[] categories = GetCategories();
Repeater1.DataSource = categories;
Repeater1.DataBind();
}
}

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

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

return categories;
}

private Category GetCategory(int i)
{
string title = i.ToString() + "CatTitle";
string ex = i.ToString() + "Explanation";
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.ToString() + j.ToString()
+ "Element");
}

return elements;
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}

class Category
{
public Category(string title, string explanation,
Element[] elements)
{
this.title = title;
this.explanation = 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
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...
4
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...
4
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...
0
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...
0
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...
5
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...
4
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...
2
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 :-...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.