碰到的问题是如果用的是Server端的Radio控件的话
系统会自动的给它分配Name以相互区分
这样就破坏了我们单选的目的
但如果用一般的HtmlControl,又不能保存状态
所以我自己写了一个用户控件
给你参考一下,代码如下:
UserRadio.ascx.cs
namespace ExamWebUI
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;
/// <summary>
///WebUserControl1 的摘要说明。
/// </summary>
public abstract class UserRadio : System.Web.UI.UserControl
{
private string rValue;
private string rName;
private string fValue;
private bool PostCheck;
private void Page_Load(object sender, System.EventArgs e)
{
fValue=this.Page.Request.Form[this.Name];
if(this.rValue==this.fValue)
PostCheck=true;
}
protected override void Render(HtmlTextWriter output)
{
string outStr;
if(PostCheck)
outStr="<h3>Value: <input name=" + this.Name + " type=radio value=" + this.Value + " checked> </h3>";
else
outStr="<h3>Value: <input name=" + this.Name + " type=radio value=" + this.Value + " > </h3>";
output.Write(outStr);
}
public string Value
{
get
{
return this.rValue;
}
set
{
this.rValue = value;
}
}
public string Name
{
get
{
return this.rName;
}
set
{
this.rName = value;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///设计器支持所需的方法 - 不要使用
///代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
UserRadio.ascx
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="UserRadio.ascx.cs" Inherits="ExamWebUI.UserRadio" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
使用该控件的例子
test1.aspx
<%@ Page language="c#" Codebehind="test1.aspx.cs" AutoEventWireup="false" Inherits="ExamWebUI.test1" %>
<%@ Register TagPrefix="dxuc" TagName="radio" Src="UserRadio.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>test1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="test1" method="post" runat="server">
<FONT face="宋体">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 230px; POSITION: absolute; TOP: 136px" runat="server" Width="221px" Height="189px">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<dxuc:radio id="rd1" runat=server value=<%#Container.DataItem%> name="hahah"></dxuc:radio>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button ID=s1 Runat=server></asp:Button>
</FONT>
</form>
</body>
</HTML>
test1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ExamWebUI
{
/// <summary>
/// test1 的摘要说明。
/// </summary>
public class test1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
System.Collections.ArrayList a=new System.Collections.ArrayList();
a.Add("a");
a.Add("b");
a.Add("c");
this.DataGrid1.DataSource=a;
this.DataGrid1.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
这种方法最大问题是必须每次Bind,暂时还没
……