[thelist] [ASP.NET] Repeater Controls and Radio Buttons
David Travis
dwork at macam.ac.il
Thu Sep 11 06:45:31 CDT 2003
Hi Joshua,
It is not so intuitive, and I recommend that you find some articles about
data binding, but basically it requires that you will write a class which
implements ITemplate interface, and add to this template a data binding
event handler.
Here is a code snippet I wrote, which does the above in a datagird, the
mothod is the same, but with different objects:
private class CategoryColumnTemplate : ITemplate {
public CategoryColumnTemplate() { }
public void InstantiateIn(Control container) {
PlaceHolder span = new PlaceHolder();
span.DataBinding +=new EventHandler(span_DataBinding);
container.Controls.Add(span);
}
private void span_DataBinding(object sender, EventArgs e) {
PlaceHolder span = (PlaceHolder)sender;
DataGridItem container = (DataGridItem)span.NamingContainer;
DataRowView rowView = (DataRowView)container.DataItem;
string content = rowView["categoryName"].ToString());
/*
at this point I can do anything I want, I have the content
from the database ('rowView'),
and I have a reference to the control which will contain this
content ('span')
what YOU need to do in this example is to create an instance
of a radio button, provide a unique ID
to it (radio_XX - XX can be id of the record in the DB), a
radio group, and add it to Controls collection
of the container ('span' in my case).
*/
//add my content to the container
span.Controls.Add(new LiteralControl(content);
}
}
What's missing in here is the line which sets this template to the datagrid:
TemplateColumn column = new TemplateColumn();
column.ItemTemplate = new CategoryColumnTemplate();
column.HeaderTemplate = new GridHeaderTemplate(ImagesDir,
HeaderNames.CategoryId, this);
column.SortExpression = "category";
grid.Columns.Add(column);
That's it, I know it looks bad, and I hope it is clear, but once you do it
properly, it becomes rather easy...
Anyways, here is an article with additional info:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingwebservercontroltemplatesdynamically.asp
HTH!
David Travis.
-----Original Message-----
From: thelist-bounces at lists.evolt.org
[mailto:thelist-bounces at lists.evolt.org] On Behalf Of Joshua Olson
Sent: 10 September 2003 20:48
To: thelist at lists.evolt.org
Subject: [thelist] [ASP.NET] Repeater Controls and Radio Buttons
> Hi ASP.NET gurus,
>
> I want to use a Repeater control to display a bunch of records. Got
> that part figured out. Now I want each record to have next to it a
> radio button so I can select exactly one record and then click "next".
> I'm using <asp:RadioButton> to create the radio button, but when the
> page is rendered each button has a different name and therefore the
> browser doesn't group them together as it should and I can select as
> many as I'd like. I've already tried setting GroupName. What else am I
> missing.
>
> TIA,
>
> <><><><><><><><><><>
> Joshua Olson
> Web Application Engineer
> WAE Tech Inc.
> http://www.waetech.com
> 706.210.0168
>
> --
> * * Please support the community that supports you. * *
> http://evolt.org/help_support_evolt/
>
> For unsubscribe and other options, including the Tip Harvester
> and archives of thelist go to: http://lists.evolt.org
> Workers of the Web, evolt !
More information about the thelist
mailing list