469,955 Members | 2,544 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,955 developers. It's quick & easy.

Using data to name a radioButton control

I am using radioButton controls in a data repeater and would like to
incorporate the 'key' field into the 'id' attribute of the radioButton
controls and name them something like:

'rad' + '<%# (string)DataBinder.Eval(Container.DataItem, "ParcelID") %>' +
PropType1

I think part of the problem is that intially there is no data bound to this
repeater section. The data doesn't get built until the user clicks a button
on the form and then the repeater gets populated. But I'm not sure of the
syntax for concatenating strings for an attribute in HTML. A code snippet is
below that just has the 'key' field without any concatenation as the 'id'
attribute. This doesn't work and gives the following error message:

Parser Error Message: '<%# (string)DataBinder.Eval(Container.DataItem,
"ParcelID") %>' is not a valid identifier.
<tr>
<td bgcolor="#eeeeee" colspan="5">Classified or designated as forest land?
Chapter 84.33 RCW</td>
<td style="WIDTH: 75px" align="center">
<asp:CheckBox id='<%# (string)DataBinder.Eval(Container.DataItem,
"ParcelID") %>' runat='server' Checked='<%#
(bool)DataBinder.Eval(Container.DataItem, "ForestLand") %>' Enabled=false>
</asp:CheckBox></td>
</tr>

NOTE that all the other bound references work fine. Please help.

Thanks,

John Holmes
jo****@co.skagit.wa.us


Nov 18 '05 #1
2 1782
Hi John,

Thanks for posting in the community!
From your description, you'd like to specify the sub control(checkbox) 's
ID at runtime using the data retrieving from its container(repeater) 's
DataSource. And you found that if you used the below style code in page
source file:
<asp:CheckBox id='<%# (string)DataBinder.Eval(Container.DataItem,
You got a Parser Error , yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, the Parser Error "..is not a valid identifier" is
because in the ASPX html source file, we must specify a constant name for a
certain servercontrol's ID property, since the runtime will create the
control in memory via this ID, we can't set it as a dynmic value. However,
we can through other means to change the control's ID, use the Repeater
control's "ItemDataBound" event. The "ItemDataBound" will fire when a
certain item of the repeater has been binded with data. Thus, we can
retireve the checkBox control in the repeater's "ItemDataBound" event and
change its ID. For example:

private void rptMain_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox chk = (CheckBox)e.Item.FindControl("chkSelected");

chk.ID = .......// set the certain value
}

}
To make this clearly, I've made a sample page to show the above means, here
is the page's code:
--------------------------------aspx page
--------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Repeater</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<TBODY>
<tr>
<td><asp:repeater id="rptMain" runat="server">
<HeaderTemplate>
<table width="100%" align="center" border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"index") %></td>
<td><asp:CheckBox ID="chkSelected" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"name") %>' ></asp:CheckBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate> </asp:Repeater></TD></TR>
<tr>
<td></td>
</tr>
</TBODY></TABLE></form>
</body>
</HTML>

---------------------------code behind page class------------------------
public class Repeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater rptMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindRepeater();
}
}

private void BindRepeater()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("selected");

for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();

newrow["index"] = index.ToString();
newrow["name"] = "Name" + index.ToString();

if(i%2 ==0)
{
newrow["selected"] = true;
}
else
{
newrow["selected"] = false;
}

tb.Rows.Add(newrow);
}
rptMain.DataSource = tb;
rptMain.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.rptMain.ItemDataBound += new
System.Web.UI.WebControls.RepeaterItemEventHandler (this.rptMain_ItemDataBoun
d);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void rptMain_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
CheckBox chk = (CheckBox)e.Item.FindControl("chkSelected");

chk.ID = drv["name"].ToString();

if(drv["selected"].ToString().Equals("True"))
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}

}
}
----------------------------------------------------

Please check out the suggestion. If you have any questions on it, please
feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #2
Thanks, this helped me accomplish what I was trying to do and I learned more
about asp.net.

John

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:OL*************@cpmsftngxa07.phx.gbl...
Hi John,

Thanks for posting in the community!
From your description, you'd like to specify the sub control(checkbox) 's
ID at runtime using the data retrieving from its container(repeater) 's
DataSource. And you found that if you used the below style code in page
source file:
<asp:CheckBox id='<%# (string)DataBinder.Eval(Container.DataItem,
You got a Parser Error , yes?
If there is anything I misunderstood, please feel free to let me know.

As for this problem, the Parser Error "..is not a valid identifier" is
because in the ASPX html source file, we must specify a constant name for a certain servercontrol's ID property, since the runtime will create the
control in memory via this ID, we can't set it as a dynmic value. However,
we can through other means to change the control's ID, use the Repeater
control's "ItemDataBound" event. The "ItemDataBound" will fire when a
certain item of the repeater has been binded with data. Thus, we can
retireve the checkBox control in the repeater's "ItemDataBound" event and
change its ID. For example:

private void rptMain_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox chk = (CheckBox)e.Item.FindControl("chkSelected");

chk.ID = .......// set the certain value
}

}
To make this clearly, I've made a sample page to show the above means, here is the page's code:
--------------------------------aspx page
--------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>Repeater</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center">
<TBODY>
<tr>
<td><asp:repeater id="rptMain" runat="server">
<HeaderTemplate>
<table width="100%" align="center" border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# DataBinder.Eval(Container.DataItem,"index") %></td>
<td><asp:CheckBox ID="chkSelected" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"name") %>' ></asp:CheckBox></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate> </asp:Repeater></TD></TR>
<tr>
<td></td>
</tr>
</TBODY></TABLE></form>
</body>
</HTML>

---------------------------code behind page class------------------------
public class Repeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater rptMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindRepeater();
}
}

private void BindRepeater()
{
DataTable tb = new DataTable();
tb.Columns.Add("index");
tb.Columns.Add("name");
tb.Columns.Add("selected");

for(int i=0;i<15;i++)
{
int index = i+1;
DataRow newrow = tb.NewRow();

newrow["index"] = index.ToString();
newrow["name"] = "Name" + index.ToString();

if(i%2 ==0)
{
newrow["selected"] = true;
}
else
{
newrow["selected"] = false;
}

tb.Rows.Add(newrow);
}
rptMain.DataSource = tb;
rptMain.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.rptMain.ItemDataBound += new
System.Web.UI.WebControls.RepeaterItemEventHandler (this.rptMain_ItemDataBoun d);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void rptMain_ItemDataBound(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
CheckBox chk = (CheckBox)e.Item.FindControl("chkSelected");

chk.ID = drv["name"].ToString();

if(drv["selected"].ToString().Equals("True"))
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}

}
}
----------------------------------------------------

Please check out the suggestion. If you have any questions on it, please
feel free to let me know.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by William Gill | last post: by
3 posts views Thread by B-Dog | last post: by
3 posts views Thread by LemonSeven | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.