[img]http://img319.ph.126.net/9jQjsMOmmoQeyOB9pGnVnQ==/3826933783358209061.jpg[/img]
[color=red]注意:若在gridview内对radiobutton用组名groupname同名方法行不同,若加上GroupName="skytest"查看静态页面源代码便知道:两个name值的组名都不同。[/color]
[img]http://img542.ph.126.net/jhJXv7zY94mMXlnkw2k5Bg==/1329687789982224141.jpg[/img]
下面结合js实现单选.
呈现页:
<asp:GridView ID="GridView1" Width="960px" EmptyDataText="暂无标书可操作"
DataKeyNames="ID" AutoGenerateColumns="false" runat="server" onrowdatabound="GridView1_RowDataBound"
>
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
[color=red]<asp:RadioButton ID="RadioButton1" runat="server" />[/color]
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="标书编号" />
</Columns>
</asp:GridView>
代码页:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
if (rb != null)
{
[color=red]rb.Attributes.Add("onclick", "single(this)"); //single(obj)为js函数[/color]
}
}
}
需要添加的javascript函数:
<script type="text/javascript">
var last=null;
function single(obj)
{
if(last==null) //第一次选择RadioButton时赋id值给last
{
last=obj.id;
}
else //第一次以后的每一次都在这运行,把上此的RadioButton.Checked=false,记下此次的obj.name
{
var lo=document.getElementById(last);
lo.checked=false;
last=obj.id;
}
obj.checked="checked"; //添加checked属性,以便在上边赋值为false
}
</script>
[color=red]注意:若在gridview内对radiobutton用组名groupname同名方法行不同,若加上GroupName="skytest"查看静态页面源代码便知道:两个name值的组名都不同。[/color]
[img]http://img542.ph.126.net/jhJXv7zY94mMXlnkw2k5Bg==/1329687789982224141.jpg[/img]
下面结合js实现单选.
呈现页:
<asp:GridView ID="GridView1" Width="960px" EmptyDataText="暂无标书可操作"
DataKeyNames="ID" AutoGenerateColumns="false" runat="server" onrowdatabound="GridView1_RowDataBound"
>
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate>
[color=red]<asp:RadioButton ID="RadioButton1" runat="server" />[/color]
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="标书编号" />
</Columns>
</asp:GridView>
代码页:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton rb = (RadioButton)e.Row.FindControl("RadioButton1");
if (rb != null)
{
[color=red]rb.Attributes.Add("onclick", "single(this)"); //single(obj)为js函数[/color]
}
}
}
需要添加的javascript函数:
<script type="text/javascript">
var last=null;
function single(obj)
{
if(last==null) //第一次选择RadioButton时赋id值给last
{
last=obj.id;
}
else //第一次以后的每一次都在这运行,把上此的RadioButton.Checked=false,记下此次的obj.name
{
var lo=document.getElementById(last);
lo.checked=false;
last=obj.id;
}
obj.checked="checked"; //添加checked属性,以便在上边赋值为false
}
</script>