2008-10-28 99 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

相關問題