简体   繁体   English

如何在c#中检索单选按钮的name属性

[英]How do I retrieve the name property of a radio button in c#

I've created a series of radio button controls in C#. 我在C#中创建了一系列单选按钮控件。 (radCompany, radProperty etc.)
I've set their group name to be the same (282_Type) so they function as a list of radio buttons. 我已将它们的组名设置为相同(282_Type),因此它们可用作单选按钮列表。

How do I retrieve the name (like: ct100$m$dfgadjkfasdghasdkjfg$282_Type) in c# so I can use this in a Javascript method i'm creating? 如何在c#中检索名称(like: ct100$m$dfgadjkfasdghasdkjfg$282_Type) ,以便我可以在我正在创建的Javascript方法中使用它?

The values output are: 输出值为:

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

You need to reference the ClientID of the control; 您需要引用控件的ClientID ; this is the id in the final html. 这是最终html中的id。

Of course, another approach might be to use some other attribute (such as the css etc), and use jQuery to find it; 当然,另一种方法可能是使用其他一些属性(如css等),并使用jQuery来查找它; jQuery takes a lot of DOM pain away from javascript. jQuery从javascript中消除了很多DOM的痛苦。 It is interesting that jQuery is now even supported by VS2008 (with intellisense etc). 有趣的是,现在甚至VS2008(使用智能感知等)支持jQuery。

I'm currently reading jQuery in Action , and liking it much. 我现在正在阅读jQuery in Action ,并且非常喜欢它。 To take an example straight from the book (on the subject of radios): 从书中直接举例(关于无线电主题):

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

which returns the value of the single checked radio button in the group, or undefined if none is selected. 返回组中单个选中的单选按钮的值,如果未选中,则返回undefined

Re getting the name; 得到这个名字; it uses the internal UniqueGroupName property, which does a lot of mangling. 它使用内部的UniqueGroupName属性,它可以进行大量的修改。 An option (but not an attractive one) would be to use reflection to read UniqueGroupName . 一个选项(但不是一个有吸引力的选项)将使用反射来读取UniqueGroupName Alternatively, use something simple like a literal control. 或者,使用像文字控件这样简单的东西。 Gawd I hate the ASP.NET forms model... roll on MVC... Gawd我讨厌ASP.NET表单模型......滚动MVC ...

Fianlly - I'm currently looking at the public VS2010 CTP image; Fianlly - 我目前正在关注公共VS2010 CTP图像; one of the new additions for ASP.NET is static ClientID s, by setting ClientIdMode = Static on the control. ASP.NET的一个新增功能是静态ClientID ,通过在控件上设置ClientIdMode = Static。 A bit overdue, but not unwelcome. 有点过期,但并非不受欢迎。

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 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 /单选@ CS / 2 /单选@ 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;  
        }  
    }

You want the UniqueID attribute: 您需要UniqueID属性:

  • UniqueID — The hierarchically-qualified unique identifier assigned to a control by the ASP.NET page framework. UniqueID - ASP.NET页面框架分配给控件的分层限定的唯一标识符。

  • ClientID — A unique identifier assigned to a control by the ASP.NET page framework and rendered as the HTML ID attribute on the client.The ClientID is different from the UniqueID because the UniqueID can contain the colon character (:), which is not valid in the HTML ID attribute (and is not allowed in variable names in client-side script). ClientID - 由ASP.NET页面框架分配给控件并在客户端上呈现为HTML ID属性的唯一标识符.ClientID与UniqueID不同,因为UniqueID可以包含冒号字符(:),该字符无效在HTML ID属性中(并且在客户端脚本中的变量名中不允许)。

( from here ) 从这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM