GridView中的单选按钮选择

时间:2013-07-27 06:39:24

标签: asp.net gridview radio-button datalist

这是我的要求。

我有3组,并且每组都有一组员工。

因此,我在DataList中放置了3个标签(显示组名)3个网格视图(来自每个组的不同员工组)。 3个GridView中的所有行都有RadioButtons。

现在问题是RadioButton表现得像一个复选框,它允许多个选择。

但是,我需要在所有3个Gridview中只选择一个RADIOBUTTON。如何实现这一目标。有人可以帮帮我吗?

/*********  Displaying Group Name and Its set of employes  **********/

/ * ** 此循环将执行3次,因为我有3个组。的 * *** /

protected void dlGraphItemDataBound(object sender, DataListItemEventArgs e)
{
    MyList dataitem = (MyList)e.Item.DataItem;

        if (dataitem != null)
        {
            Label lbl = (Label)e.Item.FindControl("lblMyLabel");
            lbl.Text = dataitem.GroupName;

            GridView dg = (GridView)e.Item.FindControl("gvList");

            if (dg != null)
            {
                List< MyList > groupItem = new List< MyList >(); // List of Employees

                foreach (MyList item in _empList)
                {
                    if (item.GroupName.Equals(dataitem.GroupName))
                 groupItem.Add(item); // Grouping all the emps who had same groupname
                }

             SetupReportGrid(dg); // Method to add controls to gridview dynamically
             dg.SetDataSource(groupItem); //Setting datasource for gridview
            }
        }
    }


protected void onRadioButtonClicked(object sender, EventArgs e)
{           

        foreach (DataListItem dlItem in dlMyDataList.Items)
        {
            GridView grid = (GridView) dlItem.FindControl("gvList");
            if (grid != null)
            {
                RadioButton selectButton = (RadioButton) sender;
                GridViewRow row = (GridViewRow) selectButton.NamingContainer;

                int a = row.RowIndex;

                foreach (GridViewRow gridRow in grid.Rows)
                {
                    RadioButton rd = gridRow.FindControl("rdoSelect") as RadioButton;
                    if (rd.Checked)
                    {
                        if (gridRow.RowIndex == a)
                        {

                            rd.Checked = true;
                        }
                        else
                        {
                            rd.Checked = false;
                        }

                    }
                }
            }
        }

这就是我尝试的方式....当我在第一个gridview中选择第一个和第二个radiobutton时,在第二个radiobutton事件之后,第一个和第二个radiobutton都未经检查。当循环对所有网格视图执行时,最后一个网格没有检查无线电按钮。最后我的代码结果没有检查无线电按钮

网格视图标记:

<asp:DataList ID="dlMyDataList" runat="server" OnItemDataBound="dlGraphItemDataBound">
                <ItemTemplate>                    
                    <table cellspacing="0" width="100%" >   

                        <tr>                                
                            <td nowrap="nowrap" width="100%" align="left" valign="middle">
                                <asp:Label ID="lblGroupName" runat="server" CssClass="ssrptsublabel" > </asp:Label>                                   
                            </td>
                         </tr>

                        <tr>
                            <td width="100%" nowrap="nowrap" align="left" valign="middle">
                                 <asp:Panel id="pnlReport" runat="server" BorderWidth="0" Width="100%" Wrap="False">
                                    <commoncontrols:MyGridView id="gvList" runat="server" autogeneratecolumns="false" EnableViewState="True">
                                    </commoncontrols:MyGridViewview>
                                 </asp:Panel>                                     
                            </td>                                                        
                        </tr>  
                        </table>                                                      

                </ItemTemplate>
            </asp:DataList>


**// Assigning controls dynamically to grid view
// Having Seperate customized class for gridview, based on that we are adding controls
//

GridViewColumnControl(ControlTypes, Control_ID, CSS Style, 0, databind,"" );**

private void SetupGrid(GridView grid)
    {

        IList<GridViewColumn> columns = new List<GridViewColumn>();

        GridViewColumn gridColumn = new GridViewColumn(ColumnTypes.TemplateColumn, "", 500, HorizontalAlign.Left,null);




       GridViewColumnControl control = new GridViewColumnControl(ControlTypes.RadioButton, "rdoSelect", "labelstyle", 0, null, "");
        control.Visible = false;
        control.AutoPostBack = true;
        control.OnChanged += onRadioButtonClicked;
        gridColumn.AddControl(control);



        control = new GridViewColumnControl(ControlTypes.DropDown, "", "style", 0, null, "");
        control.Visible = false;
        gridColumn.AddControl(control);




        grid.SetColumns(columns);
    }

1 个答案:

答案 0 :(得分:0)

RadioButton控件有一个名为GroupName的属性。具有相同GroupName的所有单选按钮属于同一组。在每个单选按钮组中,只能选择一个单选按钮。所以我相信如果你为网格视图中的所有单选按钮设置了这个属性,你就会满足你的要求。