【问题标题】:radio button grouping asp.net单选按钮分组 asp.net
【发布时间】:2012-05-19 16:37:13
【问题描述】:

我正在尝试将单选按钮与 groupname 属性组合在一起。 由于在渲染时 asp.net 中的命名约定,它不起作用..

所以我尝试继承单选按钮控件并覆盖单选按钮并创建自己的控件..

它解决了分组问题,但由于某种原因它没有处理视图状态...... 所以我所有的单选按钮都是无视图的..

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace CustomControl
{
    public class GroupRadioButton : RadioButton
    {
        public GroupRadioButton()
            : base()
        { }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(new GroupedRadioButtonTextWriter(writer,this.GroupName));
        }
    }

    public class GroupedRadioButtonTextWriter : HtmlTextWriter
    {
        private string groupName;

        public GroupedRadioButtonTextWriter(HtmlTextWriter baseWriter, string groupName) : base(baseWriter)
        {
            this.groupName = groupName;
        }

        public override void AddAttribute(HtmlTextWriterAttribute key, string value)
        {
            if (key == HtmlTextWriterAttribute.Name)
            {
                base.AddAttribute(key, this.groupName);
            }
            else
            {
                base.AddAttribute(key, value);
            }
        }
    }
}

谁能帮忙?

【问题讨论】:

  • 您能解释一下 RadioButton 上的 GroupName 属性对您不起作用的原因吗?对我来说就像一个魅力(至少在我过去使用 WebForms 时)。你用的是什么版本的 ASP.NET WebForms(我刚用 4.0 重新测试过)
  • 你试过 RadioButtonList 吗?这将创建一组单选按钮。您需要做的就是将数据源绑定到它。 msdn.microsoft.com/en-us/library/…

标签: c# asp.net radio-button overriding viewstate


【解决方案1】:

考虑到RadioButtonList 是您应该使用的组件,我认为您通过实现自己的单选按钮来滥用您的时间。

【讨论】:

  • 单选按钮列表无法解决问题,因为按钮在表单中的不同位置.. :( 单选按钮分组由于某种原因仍然无法正常工作..
猜你喜欢
  • 2010-11-23
  • 2011-05-18
  • 2015-03-19
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
相关资源
最近更新 更多