2008-10-28 35 views
4

我已经在C#中创建了一系列单选按钮控件。 (radCompany, radProperty etc.)
我已经将他们的组名设置为相同(282_Type),所以他们作为单选按钮列表。如何检索c中单选按钮的名称属性#

如何在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" 

回答

4

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

当然,另一种方法可能是使用一些其他属性(例如css等),并使用jQuery来查找它; jQuery使JavaScript远离了很多DOM痛苦。有趣的是,jQuery现在甚至被VS2008支持(包括intellisense等)。

我目前在读jQuery in Action,并且非常喜欢它。如果未选择任何

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

它返回该组中的单个检查单选按钮的值,或undefined:采取的示例直从书(上无线电设备的主题)。

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

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

+0

(删除了我的使用ID的建议,以便不从正确的答案分心。) – 2008-10-28 10:45:09

1

http://reflector.webtropy.com/default.aspx/Net/Net/[email protected]@[email protected]/DEVDIV/depot/DevDiv/releases/whidbey/netfxsp/ndp/fx/src/xsp/System/Web/UI/WebControls/[email protected]/2/[email protected]

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

你想UniqueID属性:

  • 的UniqueID - 分配给由ASP.NET页面框架控制的可分级限定唯一标识符。

  • 的ClientID - 分配给由ASP.NET页面 框架的控制和呈现为在client.The 客户端ID的HTML ID属性的唯一标识符是从所述的UniqueID不同,因为可以的UniqueID 包含冒号(:),它在HTML ID 属性中无效(并且不允许在客户端 脚本中的变量名中)。

from here