这是我的要求。
我有 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;
}
}
}
}
}
这就是我尝试的方式....当我在第一个网格视图中选择第一个和第二个单选按钮时,并且在第二个单选按钮的事件之后,第一个和第二个单选按钮都未选中。由于循环对所有网格视图和最后一个没有选中单选按钮的网格执行。最后我的代码结果是没有检查单选按钮
网格视图标记:
<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);
}