日期:2012-08-25  浏览次数:20370 次

碰到的问题是如果用的是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