Using data to name a radioButton control

J

John Holmes

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
(e-mail address removed)
 
S

Steven Cheng[MSFT]

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.)
 
J

John Holmes

Thanks, this helped me accomplish what I was trying to do and I learned more
about asp.net.

John

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

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top