This is a migrated thread and some comments may be shown as answers.

RadioButton in Radgrid with single selection

5 Answers 598 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RB
Top achievements
Rank 1
RB asked on 09 May 2014, 09:26 PM
I am adding a radio button in each row of my radgrid as follows on OnInit:
              string templateColumnName = "Select One";
           GridTemplateColumn templateColumn = new GridTemplateColumn();
           templateColumn.ItemTemplate = new RadioButtonTemplate(templateColumnName);
           templateColumn.HeaderText = templateColumnName;
           this._RadGrid1.MasterTableView.Columns.Add(templateColumn);


public class RadioButtonTemplate : ITemplate
{
    RadioButton radioButton;
    String columnName;
    public RadioButtonTemplate(String colName)
    {
        columnName = colName;
    }
 
    public void InstantiateIn(System.Web.UI.Control container)
    {
        radioButton = new RadioButton();
        radioButton.ID = columnName;       
        container.Controls.Add(radioButton);       
    }
}


How can I enable only single selection at a time and capture the row selected?

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 12 May 2014, 06:44 AM
Hi,

You can take a look at the following code library which uses radio button residing in ItemTemplate of RadGrid template column for selecting only single row at a time. Try and let me know if any concern.
Single RadioButton check at a time with row selection

Thanks,
Princy
0
RB
Top achievements
Rank 1
answered on 12 May 2014, 10:13 PM
Thanks for the reply Princy. I implemented the example you provided. But still it allows multiple selection.The radgrid is set as follows:
           protected override void OnInit(EventArgs e)
        {
            setGrdRadioButtonOnClick();
            base.OnInit(e);
 
            this._RadLoadingPanel.ID = "_RadLoadingPanel";
            this._RadLoadingPanel.Transparency = 30;
            this._RadLoadingPanel.Skin = "WebBlue";
            this._RadLoadingPanel.BackgroundPosition = AjaxLoadingPanelBackgroundPosition.Center;
             
            this._RadGrid1.Skin = "WebBlue";
            this._RadGrid1.Width = Unit.Percentage(98);
            this._RadGrid1.GridLines = GridLines.None;
            this._RadGrid1.PageSize = 100;
            this._RadGrid1.AllowPaging = true;
            this._RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            this._RadGrid1.AllowSorting = true;
            this._RadGrid1.AllowFilteringByColumn = false;
            this._RadGrid1.AutoGenerateColumns = false;
            this._RadGrid1.EnableLinqExpressions = false;
            this._RadGrid1.MasterTableView.NoMasterRecordsText = "There are no records";
            this._RadGrid1.ShowGroupPanel = false;
            this._RadGrid1.ShowStatusBar = false;
            this._RadGrid1.GroupingEnabled = false;
        
            this._RadGrid1.ClientSettings.EnableRowHoverStyle = true;
            this._RadGrid1.ClientSettings.EnablePostBackOnRowClick = true;
            this._RadGrid1.ClientSettings.ClientEvents.OnRowSelected = "RowSelecting";
            this._RadGrid1.MasterTableView.Width = Unit.Percentage(100);
            this._RadGrid1.MasterTableView.DataKeyNames = new string[] { this._PriceDealProductSumTable.PriceDealIdColumn.ColumnName };
            this._RadGrid1.MasterTableView.EnableHeaderContextMenu = true;
            this._RadGrid1.NeedDataSource += RadGrid1_NeedDataSource;
 
            this._RadGrid1.ItemDataBound += RadGrid1_ItemDataBound;
            this._RadGrid1.SelectedIndexChanged += RadGrid1_SelectedIndexChanged;
 
            string templateColumnName = "Select One";
            GridTemplateColumn templateColumn = new GridTemplateColumn();
            templateColumn.ItemTemplate = new RadioButtonTemplate(templateColumnName);
            templateColumn.HeaderText = templateColumnName;
            this._RadGrid1.MasterTableView.Columns.Add(templateColumn);
}
public void setGrdRadioButtonOnClick()
        {
            int i;
            RadioButton radioButton;
            for (i = 0; i < this._RadGrid1.Items.Count; i++)
            {
                radioButton = (RadioButton)this._RadGrid1.Items[i].FindControl("Select One");
                radioButton.Attributes.Add("OnClick", "SelectMeOnly(" + radioButton.ClientID + ", " + "'_RadGrid1'" + ")");
            }
        }
It doesnt enter the for loop since items.count is 0. The template is as follows:
public class RadioButtonTemplate : ITemplate
{
    RadioButton radioButton;
    String columnName;
    public RadioButtonTemplate(String colName)
    {
        columnName = colName;
    }
 
    public void InstantiateIn(System.Web.UI.Control container)
    {
        radioButton = new RadioButton();
        radioButton.ID = columnName;
        radioButton.AutoPostBack = true;
        radioButton.CheckedChanged += rdSelect_CheckedChanged;
        container.Controls.Add(radioButton);       
        
    }
    protected void rdSelect_CheckedChanged(object sender, EventArgs e)
    {
        ((sender as RadioButton).Parent.Parent as GridItem).Selected = (sender as RadioButton).Checked;
    }
      
}

0
RB
Top achievements
Rank 1
answered on 12 May 2014, 10:27 PM
On selection of a radio button, another template with some buttons opens. Depending on the item selected, the buttons will be displayed.How can I achieve this?
0
Radoslav
Telerik team
answered on 14 May 2014, 01:37 PM
Hi Richi,

To achieve the desired functionality you can try handling the RadGrid client side selection by attaching event handler to the OnRowSelected event:
<telerik:RadGrid runat="server" ... >
...
        <ClientSettings>
            <ClientEvents OnRowSelected="OnRowSelected" />
        </ClientSettings>
    </telerik:RadGrid>
<telerik:RadCodeBlock runat="server">
        <script type="text/javascript">
            function OnRowSelected(sender, eventArgs)
            {
                // Show template with buttons
            }
        </script>
    </telerik:RadCodeBlock>

Please give it try and let me know if it helps you. 

Regards,
Radoslav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Princy
Top achievements
Rank 2
answered on 15 May 2014, 09:08 AM
Hi,

I checked your code and found that you haven't set the ID property of the Grid. Please set it and the radio button single selection works fine at my end.

C#:
this._RadGrid1.ID = "_RadGrid1";

Thanks,
Princy
Tags
Grid
Asked by
RB
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
RB
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or