[英]How do I retrieve the name property of a radio button in c#
我在C#中创建了一系列单选按钮控件。 (radCompany, radProperty etc.)
我已将它们的组名设置为相同(282_Type),因此它们可用作单选按钮列表。
如何在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"
您需要引用控件的ClientID ; 这是最终html中的id。
当然,另一种方法可能是使用其他一些属性(如css等),并使用jQuery来查找它; jQuery从javascript中消除了很多DOM的痛苦。 有趣的是,现在甚至VS2008(使用智能感知等)支持jQuery。
我现在正在阅读jQuery in Action ,并且非常喜欢它。 从书中直接举例(关于无线电主题):
var x = $('[name=radioGroup]:checked').val();
返回组中单个选中的单选按钮的值,如果未选中,则返回undefined
。
得到这个名字; 它使用内部的UniqueGroupName
属性,它可以进行大量的修改。 一个选项(但不是一个有吸引力的选项)将使用反射来读取UniqueGroupName
。 或者,使用像文字控件这样简单的东西。 Gawd我讨厌ASP.NET表单模型......滚动MVC ...
Fianlly - 我目前正在关注公共VS2010 CTP图像; ASP.NET的一个新增功能是静态ClientID
,通过在控件上设置ClientIdMode = Static。 有点过期,但并非不受欢迎。
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;
}
}
您需要UniqueID属性:
UniqueID - ASP.NET页面框架分配给控件的分层限定的唯一标识符。
ClientID - 由ASP.NET页面框架分配给控件并在客户端上呈现为HTML ID属性的唯一标识符.ClientID与UniqueID不同,因为UniqueID可以包含冒号字符(:),该字符无效在HTML ID属性中(并且在客户端脚本中的变量名中不允许)。
( 从这里 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.