【问题标题】:How do I retrieve the name property of a radio button in c#如何在 C# 中检索单选按钮的名称属性
【发布时间】:2008-10-28 10:34:21
【问题描述】:

我在 C# 中创建了一系列单选按钮控件。 (radCompany, radProperty etc.)
我已将它们的组名设置为相同 (282_Type),因此它们用作单选按钮列表。

如何在 c# 中检索名称 (like: ct100$m$dfgadjkfasdghasdkjfg$282_Type),以便可以在我正在创建的 Javascript 方法中使用它?

输出的值是:

Radion Button 1  
id="ct223423432243_radCompany" name="ct100$sdfsdf$sdfsdf$282_Type"  
Radion Button 2  
id="ct223423432243_radProperty" name="ct100$sdfsdf$sdfsdf$282_Type"  

【问题讨论】:

    标签: c# javascript asp.net


    【解决方案1】:

    需要引用控件的ClientID;这是最终 html 中的 id。

    当然,另一种方法可能是使用一些其他属性(例如css等),并使用jQuery找到它; jQuery 从 javascript 中消除了很多 DOM 的痛苦。有趣的是,现在 VS2008 甚至支持 jQuery(带有智能感知等)。

    我目前正在阅读jQuery in Action,并且非常喜欢它。直接从书中举一个例子(关于收音机的主题):

    var x = $('[name=radioGroup]:checked').val();
    

    返回组中单个选中的单选按钮的值,如果没有选中,则返回undefined

    重新获得名称;它使用内部的UniqueGroupName 属性,它做了很多修改。一个选项(但不是一个有吸引力的选项)是使用反射来读取UniqueGroupName。或者,使用一些简单的东西,比如文字控件。天哪,我讨厌 ASP.NET 表单模型...在 MVC 上滚动...

    Fianlly - 我目前正在查看公众 VS2010 CTP 图像; ASP.NET 的新增功能之一是静态ClientIDs,通过在控件上设置 ClientIdMode = Static。有点过时了,但不受欢迎。

    【讨论】:

    • (删除了我使用ID的建议,以免分散正确答案的注意力。)
    • 这确实返回了控件 ID,但这不是单选按钮列表的名称,因此它返回 ID 属性而不是我想要的名称属性。
    • 你能澄清一下名字和id是什么吗?我不完全确定你看到了什么......
    • 感谢您的建议,但我几乎肯定不会为此使用 jQuery,因为这会增加不必要的复杂性。一个选项是不使用 c# 创建的单选按钮,而直接使用 HTML 单选按钮。
    • 给定 ID,你可以很容易地在 JavaScript 中找到控件,然后获取名称。
    【解决方案2】:

    http://reflector.webtropy.com/default.aspx/Net/Net/3@5@50727@3053/DEVDIV/depot/DevDiv/releases/whidbey/netfxsp/ndp/fx/src/xsp/System/Web/UI/WebControls/RadioButton@cs/2/RadioButton@cs

    internal string UniqueGroupName {  
            get { 
                if (_uniqueGroupName == null) {  
                    // For radio buttons, we must make the groupname unique, but can't just use the  
                    // UniqueID because all buttons in a group must have the same name.  So 
                    // we replace the last part of the UniqueID with the group Name.  
                    string name = GroupName; 
                    string uid = UniqueID; 
    
                    if (uid != null) {  
                        int lastColon = uid.LastIndexOf(IdSeparator); 
                        if (lastColon >= 0) {  
                            if (name.Length > 0) {  
                                name = uid.Substring(0, lastColon+1) + name; 
                            }  
                            else if (NamingContainer is RadioButtonList) { 
                                // If GroupName is not set we simply use the naming 
                                // container as the group name 
                                name = uid.Substring(0, lastColon);  
                            } 
                        }  
    
                        if (name.Length == 0) { 
                            name = uid;  
                        } 
                    } 
    
                    _uniqueGroupName = name;  
                } 
                return _uniqueGroupName;  
            }  
        }
    

    【讨论】:

      【解决方案3】:

      你想要 UniqueID 属性:

      • UniqueID — 由 ASP.NET 页面框架分配给控件的分层限定唯一标识符。

      • ClientID — 由 ASP.NET 分配给控件的唯一标识符 页面框架并呈现为客户端上的 HTML ID 属性。 ClientID 与 UniqueID 不同,因为 UniqueID 可以 包含冒号 (:),它在 HTML ID 中无效 属性(并且不允许在客户端的变量名中使用 脚本)。

      (from here)

      【讨论】:

        猜你喜欢
        • 2020-05-22
        • 2017-11-16
        • 1970-01-01
        • 2012-06-09
        • 1970-01-01
        • 2020-12-05
        • 2019-05-27
        • 1970-01-01
        • 2012-03-09
        相关资源
        最近更新 更多