[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet

Grouping Multiple Radio Buttons in a Datagrid or Repeater BUG Workaround Fix

Grouping Multiple Radio Buttons in a Datagrid or Repeater BUG Workaround Fix

(Mark Kucera)

10/10/2002 5:27:00 PM

I was very dismayed when i found so many people were apparantly having
the same problem i was having when using a radio button in a template
column of a datagrid. The problem is that the datagrid overwrites the
control id giving it a mutually exclusive name, thus allowing multiple
radio buttons to be selected instead of just one. Setting the
GroupName property doesn't help becuase it is overriden by the
datagrid's name.

See http://support.microsoft.com/default.aspx?scid=kb;en-...
for detailed bug.

Here is a pretty simple workaround that i am now usuing successfully
to get around this problem. Create the template column as normal

<asp:TemplateColumn HeaderText="">
<ItemTemplate>
<input type="radio" name="rbPaymentMethods"
value="<%#DataBinder.Eval(Container.DataItem,"ID")%>" enabled >
</ItemTemplate>
</asp:TemplateColumn>

But modify your radio button to be a normal HTML radio button (not
asp:radiobutton) and make sure you leave out the runat="server"
attribute. This is very important and makes the difference between
having the datagrid rename the id of your controls or not.

If you don't need to manipulate this item on the server, then you are
set you don't need to do anything else. you will have a mutually
exclusive radio button list on your datagrid. If you need to modify
the radio button on the server like i did, then keep reading..

Modifying the radio button on the server:

first you will want to create an Event Handler for the ItemDataBound
Event
private void myDataGrid_ItemDataBound(object sender,
DataGridItemEventArgs e)
{
ListItemType item = e.Item.ItemType;
if (item == ListItemType.Item || item ==
ListItemType.AlternatingItem)
{
//our code will go here
}
}

here is some sample code that you can modify to manipulate your radio
button data. This took me a long while to figure out, but once i
figured it out, it was very quick to implement, and works like a
charm.

1. Get a reference to the radio button column. The template column
only has one control, which is a DataBoundLiteralControl
DataBoundLiteralControl lit1 =
(DataBoundLiteralControl)e.Item.Cells[0].Controls[0];

2. This control has a Text property which contains a string
representation of your databound radio button, but it is read only, so
copy the contents to a string.
string newRadioButtonText = lit1.Text;

3. Now you have a string representation of the radio button and you
can use any string functions to manipulate / parse the data. In my
case there were certain times when i wanted the radio button to be
disabled. If you notice up where i defined my radio button, i
included an attribute in the <input> tag called enabled. This isn't
actually a valid attribute (which is ok since invalid attributes are
ignored), but it gives me something to search on. i perform my
business logic, and if when i need to disable this radio button i
simply use the string's replace method to replace the invalid
attribute with a valid one.

newRadioButtonText = newRadioButtonText.Replace(" enabled "," disabled
");

4. Once i have finished modifiying my radio button string, i need to
add it into the datagrid. First we need to get rid of the read only
databound control. Do this by calling the Clear() method on the
Cell's Controls collection
e.Item.Cells[0].Controls.Clear();

5. Create a new Literal Control containing the modified radio button
LiteralControl lit2 = new LiteralControl(newRadioButtonText);

6. Finally Add the new control to the cell's control collection
e.Item.Cells[0].Controls.Add(lit2);

Thats it, your done. You have successfully circumvented this annoying
.NET bug in MS's Datagrid control.


An additional benefit to using this approach is that when you submit
the form, you will have access to your radio button in the forms
collection using the name you specified. you will not to to search
for the autogenerated name that the datagrid automatically creates for
server bound controls. This is a bonus i wasn't even looking for, but
am happy to have.

Happy Coding.
MK

Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Grouping Multiple Radio Buttons in a Datagrid or Repeater BUG Workaround Fix

Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te

Il servizio è completamente GRATUITO!