<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rbStudent" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SName" HeaderText="用户名" />
<asp:BoundField DataField="SSex" HeaderText="性别" />
</Columns>
</asp:GridView>
public class Student
{
public string SID { get; set; }
public string SName { get; set; }
public string SSex { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
}
}
public void BindGridView()
{
List<Student> sList = new List<Student>()
{
new Student(){ SID = "s001", SName="张三", SSex="男"},
new Student(){ SID = "s002", SName="李四", SSex="女"},
new Student(){ SID = "s003", SName="王五", SSex="男"}
};
GridView1.DataSource = sList;
GridView1.DataBind();
}
<table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col"> </th><th scope="col">用户名</th><th scope="col">性别</th>
</tr><tr>
<td>
<input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" />
</td><td>张三</td><td>男</td>
</tr><tr>
<td>
<input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" />
</td><td>李四</td><td>女</td>
</tr><tr>
<td>
<input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" />
</td><td>王五</td><td>男</td>
</tr>
</table>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
RadioButton RadioButtionRB = (RadioButton)e.Row.FindControl("rbStudent");
RadioButtionRB.Attributes["name"] = "student";
}
}
<span name="student"><input id="GridView1_rbStudent_0" type="radio" name="GridView1$ctl02$rbStudent" value="rbStudent" /></span>
</td><td>张三</td><td>男</td>
</tr><tr>
<td>
<span name="student"><input id="GridView1_rbStudent_1" type="radio" name="GridView1$ctl03$rbStudent" value="rbStudent" /></span>
</td><td>李四</td><td>女</td>
</tr><tr>
<td>
<span name="student"><input id="GridView1_rbStudent_2" type="radio" name="GridView1$ctl04$rbStudent" value="rbStudent" /></span>
</td><td>王五</td><td>男</td>
<script type="text/javascript">
function SetRadioName() {
var gv = document.getElementById("GridView1"); //获取GridView的客户端ID
var myradio = gv.getElementsByTagName("input"); //获取GridView的Inputhtml
for (var i = 0; i < myradio.length; i++) {
if (myradio[i].type == 'radio')//hidden
{
myradio[i].setAttribute("name", "myradio");
}
}
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGridView();
ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),
"SetRadioName()", true);
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有