Click here to Skip to main content
15,921,028 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi!
Does anyone know how to add a radiobutton to a dynamic gridview that will be mutually exclusive?
"In the GridView, the radiobuttons cannot be grouped and therefore are not mutually exclusive. That is, an end user is able to select multiple radiobuttons simultaneously from the GridView."
One of the solutions I found searching is to add a template field with a HtmlInputRadioButton witch works as mutually exclusive but only in normal gridviews, in the dynamic ones, the mutually exclusive doesn't work. When I select one of the radiobuttons and select the next the first one is not deselected. I read somewhere it's because all radiobuttons have different name. How can I give all the radiobuttons same name in InstantiateIn methode look in my class below.
Also, once I have the radiobutton set up correctly...how can I identify whether that row has the radiobutton checked or not?

I asked the same question yesterday. I ask for your help again. Thanks in advance.

Here is the code I use:

TemplateField rbtnColumn = new TemplateField();            
rbtnColumn.HeaderTemplate = new GridViewTemplate(ListItemType.Header, "Select");   
rbtnColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data"); 
myGridView.Columns.Add(rbtnColumn);
Here is the GridViewTemplate class I use:
public class GridViewTemplate : System.Web.UI.Page, ITemplate
{
    ListItemType templateType;
    string columnName;
    public GridViewTemplate(ListItemType type, string colname)
    {
        templateType = type;
        columnName = colname;
    }
 public void InstantiateIn(System.Web.UI.Control container)
    {
        HtmlInputRadioButton ckh = new HtmlInputRadioButton();     
        switch (templateType)
        {
          case ListItemType.Item:
                container.Controls.Add(rbt);
                break;
         }
    }
}
Posted
Updated 31-Mar-11 9:30am
v2
Comments
Smithers-Jones 31-Mar-11 15:34pm    
Instead of posting the same question again, you should have gone back to yesterday's post and edit that one to maybe give some more information, if possible. Do not post the same question again, if you don't get a satisfying answer immediately but have some patience.
Also, I edited your question a bit (added code block)
Majid khosravi 31-Mar-11 16:06pm    
Thanks!

1 solution

Hi Majid,

If you want the same name for all those radio then you can use your custom radio button. You just need to know how to form little html markup.

Also note your template control no need to inherit from page class. Here is the sample code.

C#
public class GridViewTemplate :  ITemplate
{
    ListItemType templateType;
    string columnName;
    public GridViewTemplate(ListItemType type, string colname)
    {
        templateType = type;
        columnName = colname;
    }
    public void InstantiateIn(System.Web.UI.Control container)
    {
        MyRadioButton button = new MyRadioButton();
        button.ClientIDMode = ClientIDMode.Static;
        button.ID = "Radio1";
        button.Name = "Radio1";
        button.Value = "Test";
        switch (templateType)
        {
            case ListItemType.Item:
                container.Controls.Add(button);
                break;
        }
    }
}


C#
public class MyRadioButton : RadioButton
{
    public string Name { get; set; }
    public string Value { get; set; }
    protected override void Render(HtmlTextWriter writer)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("<input type='radio' id='" + Name + "' name='" + Name + "' value='" + Value + "'/>");
        writer.Write(sb.ToString());
    }
}


And add it in the same way as you did. For the header you use the default header template available in asp.net
C#
TemplateField rbtnColumn = new TemplateField();
 rbtnColumn.ItemTemplate = new GridViewTemplate(ListItemType.Item, "some data");
 GridView1.Columns.Add(rbtnColumn);


good luck.

Improved part as per your comment
-------------------------------------------

What you can do is handle the row data bound event of the gridview and assign the value for the radio buttons. Sample code as below.

In the gridview markup assign a handler like this OnRowDataBound="Gridview1_RowDatabound" or else in the page load GridView1.RowDataBound += new GridViewRowEventHandler(Gridview1_RowDatabound);. Once the handler is assigned then inside the handler

C#
protected void Gridview1_RowDatabound(object sender,  GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //Assumed here the column index where the radio button present is 3
        // Change the index as per your column index.
        MyRadioButton btn = (MyRadioButton)e.Row.Cells[3].FindControl("Radio1");
        //Assumed here the customer id is the 0th column
        //change accordingly in your data.
        btn.Value = e.Row.Cells[0].Text;
    }
}


The cell indices you need to change as per your data. Now the radio button's value are assigned for the customer id. Hope that helps.
 
Share this answer
 
v2
Comments
Majid khosravi 1-Apr-11 12:39pm    
Thank you. I wiil try this and let you know.
Majid khosravi 7-Apr-11 12:21pm    
Thanks I tried this. It works. Now I have another problem. I want toread which of the row from GridView has been selected by selectning one of the Radio Button. I know I can read it by this way:
string selectedValue = Request.Form["MyRadioButton"];

But before that I shall set value of the each Radio Buttons to something like:
Value = '<%#Eval("CustomerID")%>' />
How can I set the value to this in MyRadioButton class?
I tried this but it didn't work:
sb.Append("<input type='radio' id='" + Name + "' name='" + Name + "' value='<%#Eval(" + "\"" + Value + "\"" + ")%>'/>");
Albin Abel 7-Apr-11 13:49pm    
Hi Majid look at my answer at the improved part
Majid khosravi 7-Apr-11 13:58pm    
Thanks for your help. I will try this.
Majid khosravi 11-Apr-11 9:53am    
I tried this but that, I want, is to find out which row has been selected. After the user selects one of row by chosing of the Radio Button and when the app comes back to server the Gridview1_RowDatabound event will not not be raised.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900