Setting id in a dynmaically generated checkboxlist

E

Eirik Eldorsen

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.DataItem, "ID")) %>'
runat="server" .....

This code will result in the following parse error:
'<%# Convert.ToString(DataBinder.Eval(Container.DataItem, "ID")) %>' is not
a valid identifier.


What am I doing wrong?
 
K

Karl Seguin

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
 
S

Scott Allen

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
 
E

Eirik Eldorsen

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.DataItem, "Title"))
%></h2><br>
<i><%# Convert.ToString(DataBinder.Eval(Container.DataItem,
"Explenation")) %></i>

<asp:CheckBoxList id='<%#
Convert.ToString(DataBinder.Eval(Container.DataItem, "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.
 
S

Scott Allen

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 = 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).
 
E

Eirik Eldorsen

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 said:
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 = 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).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top