方案大致可以分为2种,一是使用JavaScript脚本从客户端操作RadioButton,二是使用服务器端实现单选。
一 使用JavaScript脚本从客户端操作RadioButton
我试过使用Html的Radio去实现,不用RadioButton,但是如果Radio没有加Runat = server的话,无法从服务端获取它的checked属性,当然就不能判断选择那副图片了,这种方案只适合获取value的情形。如果按照孟宪会的方法是倒是可以的。具体代码如下:
二 使用服务端解决
完全通过C#代码去操作是没有实现方案的,至少我还没有发现。但是如果是操作DataGrid是可以的,原因在于DataGridItem可以强制转化为tablecell.但是不能在DataList中实现。因为DataListItem不能转换为TableCell.具体代码如下:
public void SetFalseone(object sender, System.EventArgs e)
{
RadioButton rdo = (RadioButton)sender;
TableCell cell = (Table)rdo.Parent;
DataGridItem item = (DataGridItem)rdo.Parent;
for (int i = 0; i < this.ItemGrid.Items.Count; i++)
{
if (i != item.ItemIndex)
{
RadioButton rbt = (RadioButton)this.ItemGrid.Items[i].FindControl("rbttruck");
rbt.Checked = false;
}
}
}
<asp:DataList ID="dtgtruck" runat="server">
<ItemTemplate>
<asp:RadioButton id="rbttruck" runat="server" OnCheckedChanged="SetFalseone" AutoPostBack="True"></asp:RadioButton>
<%# Container.DataItem%>
</ItemTemplate>
</asp:DataList>