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

时间:2021-05-26 02:54:47

I've created a series of radio button controls in 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.

我在C#中创建了一系列单选按钮控件。 (radCompany,radProperty等)我已将它们的组名设置为相同(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#中检索名称(如: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"  

3 个解决方案

#1


4  

You need to reference the ClientID of the control; this is the id in the final html.

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

Of course, another approach might be to use some other attribute (such as the css etc), and use jQuery to find it; jQuery takes a lot of DOM pain away from javascript. It is interesting that jQuery is now even supported by VS2008 (with intellisense etc).

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

I'm currently reading jQuery in Action, and liking it much. To take an example straight from the book (on the subject of radios):

我现在正在阅读jQuery in Action,并且非常喜欢它。从书中直接举一个例子(关于无线电的主题):

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. An option (but not an attractive one) would be to use reflection to read UniqueGroupName. Alternatively, use something simple like a literal control. Gawd I hate the ASP.NET forms model... roll on MVC...

得到这个名字;它使用内部的UniqueGroupName属性,它可以进行大量的修改。一个选项(但不是一个有吸引力的选项)将使用反射来读取UniqueGroupName。或者,使用像文字控件这样简单的东西。 Gawd我讨厌ASP.NET表单模型......滚动MVC ...

Fianlly - I'm currently looking at the public VS2010 CTP image; one of the new additions for ASP.NET is static ClientIDs, by setting ClientIdMode = Static on the control. A bit overdue, but not unwelcome.

Fianlly - 我目前正在关注公共VS2010 CTP图像; ASP.NET的一个新增功能是静态ClientID,通过在控件上设置ClientIdMode = Static。有点过期,但并非不受欢迎。

#2


1  

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;  
        }  
    }

#3


0  

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)

(从这里)

#1


4  

You need to reference the ClientID of the control; this is the id in the final html.

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

Of course, another approach might be to use some other attribute (such as the css etc), and use jQuery to find it; jQuery takes a lot of DOM pain away from javascript. It is interesting that jQuery is now even supported by VS2008 (with intellisense etc).

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

I'm currently reading jQuery in Action, and liking it much. To take an example straight from the book (on the subject of radios):

我现在正在阅读jQuery in Action,并且非常喜欢它。从书中直接举一个例子(关于无线电的主题):

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. An option (but not an attractive one) would be to use reflection to read UniqueGroupName. Alternatively, use something simple like a literal control. Gawd I hate the ASP.NET forms model... roll on MVC...

得到这个名字;它使用内部的UniqueGroupName属性,它可以进行大量的修改。一个选项(但不是一个有吸引力的选项)将使用反射来读取UniqueGroupName。或者,使用像文字控件这样简单的东西。 Gawd我讨厌ASP.NET表单模型......滚动MVC ...

Fianlly - I'm currently looking at the public VS2010 CTP image; one of the new additions for ASP.NET is static ClientIDs, by setting ClientIdMode = Static on the control. A bit overdue, but not unwelcome.

Fianlly - 我目前正在关注公共VS2010 CTP图像; ASP.NET的一个新增功能是静态ClientID,通过在控件上设置ClientIdMode = Static。有点过期,但并非不受欢迎。

#2


1  

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;  
        }  
    }

#3


0  

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)

(从这里)