473,396 Members | 1,966 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,396 software developers and data experts.

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 1881
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bala | last post by:
Iam using python....to invoke VBApplication...in the VBApplication contains lot controls...for Example 1.TextBox 2)CheckBox 3)Button 4)RadioButton 5)ComboBox 6)ListBox Outoff this six...
11
by: William Gill | last post by:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep references to them in a 2 dimensional list ( rBtns ). It works fine, and I can even make it so only one button per column can be...
1
by: David Whitney | last post by:
Hi, all. I'm trying to work with some client-side scripting issues with an ASP.NET application. I realize I've probably done something wrong, but at the moment it looks to me like an...
3
by: Nathan Sokalski | last post by:
I am using the AddHandler statement to add a CheckedChanged event handler to a series of RadioButtons that I create in a loop. However, the handler is not being called for a reason I cannot...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
3
by: LemonSeven | last post by:
Hi All, newbie here- When I use an example from the MSDN Library, say, to programmically create a radio button, all I get is an empty form. What am I missing? Here's the code (I just add...
2
by: Savas Ates | last post by:
I want to change my radiobutton's name ,id and value properties programmatically. RadioButton1.Attributes.Add("value", "Pele") RadioButton1.Attributes.Add("name", "R1") ...
1
by: sheenaa | last post by:
Hello Members, I m creating my application forms in ASP.Net 2005 C# using the backend SQL Server 2005. What i have used on forms :: ? On my first form i have used some...
8
by: =?Utf-8?B?UmljaA==?= | last post by:
If you enclose a group of radiobuttons (option buttons in MS Access) in an option group control (a frame control) in Access -- the frame control will return the index of the option button that is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.