代码之家  ›  专栏  ›  技术社区  ›  Bravax

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

  •  4
  • Bravax  · 技术社区  · 16 年前

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

    如何检索名称 (like: ct100$m$dfgadjkfasdghasdkjfg$282_Type) 在c#中,那么我可以在我创建的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"  
    
    3 回复  |  直到 11 年前
        1
  •  4
  •   Marc Gravell    16 年前

    你需要参考 ClientID 控制力;这是最终html中的id。

    当然,另一种方法可能是使用其他属性(如css等),并使用 jQuery 找到它;jQuery消除了javascript带来的许多DOM痛苦。有趣的是,jQuery现在甚至得到了VS2008的支持(包括intellisense等)。

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

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

    返回组中单个选中单选按钮的值,或 undefined 如果未选择任何选项。

    我们得到了名字;它使用内部 UniqueGroupName 房地产,这会造成很多破坏。一个选择(但不是一个有吸引力的选择)是使用反射来阅读 唯一组名 .或者,使用文字控件之类的简单控件。我讨厌ASP。网络表单模型。。。开始MVC。。。

    最后——我目前正在关注公众 VS2010 CTP 形象ASP的新增功能之一。网络是静态的 ClientID s、 通过在控件上设置ClientIdMode=Static。有点晚了,但不是不受欢迎。

        2
  •  1
  •   Dallas Morrison    13 年前

    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
  •  0
  •   Community CDub    4 年前

    您需要UniqueID属性:

    • 独一无二的 ASP分配给控件的按层次结构限定的唯一标识符。NET页面框架。

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

    ( from here )