ASP.NET中设置CheckBox和RadioButton的默认值不可改变,并不丢失样式!
2008-01-12 18:23
387 查看
我们制作表单的时候都会用到多选CheckBox和单选RadioButton按钮。
有的时候我们希望它们的默认值不可以让用户改变。
最普遍的方法就是加上 enable=false。但这样就有了个问题,原本定义好的样式不见了!CheckBox变的很难看。
如何让CheckBox默认值不变,又不丢失原来的样式呢?
很简单,只要在每个CheckBox上添加属性onclick="this.checked=!this.checked;"
就是在用户改变了它之后再给改回去。
foreach (Control c in Page.Controls)
foreach (Control c in Page.Controls)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (c.GetType().ToString() == "System.Web.UI.WebControls.RadioButtonList")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//根据分析代码,我们看到单个的radio有唯一的id属性而且是这一个RadioButtonList控件的id+“_”+Index,所以第一步我们很容易找到这组radio被选中的radio的id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string radioId = ((RadioButtonList)c).ClientID +"_"+ ((RadioButtonList)c).SelectedIndex.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (ListItem l in ((RadioButtonList)c).Items)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//添加onclick事件,在用户改变了默认值后,再恢复本组默认的选中值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
l.Attributes.Add("onclick","document.getElementById(""+radioId+"").checked=true");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
有的时候我们希望它们的默认值不可以让用户改变。
最普遍的方法就是加上 enable=false。但这样就有了个问题,原本定义好的样式不见了!CheckBox变的很难看。
如何让CheckBox默认值不变,又不丢失原来的样式呢?
很简单,只要在每个CheckBox上添加属性onclick="this.checked=!this.checked;"
就是在用户改变了它之后再给改回去。
foreach (Control c in Page.Controls)
foreach (Control c in Page.Controls)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (c.GetType().ToString() == "System.Web.UI.WebControls.RadioButtonList")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//根据分析代码,我们看到单个的radio有唯一的id属性而且是这一个RadioButtonList控件的id+“_”+Index,所以第一步我们很容易找到这组radio被选中的radio的id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string radioId = ((RadioButtonList)c).ClientID +"_"+ ((RadioButtonList)c).SelectedIndex.ToString();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (ListItem l in ((RadioButtonList)c).Items)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//添加onclick事件,在用户改变了默认值后,再恢复本组默认的选中值
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
l.Attributes.Add("onclick","document.getElementById(""+radioId+"").checked=true");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
相关文章推荐
- ASP.NET中设置CheckBox和RadioButton的默认值不可改变,并不丢失样式!
- ASP.NET中设置CheckBox和RadioButton的默认值不可改变
- ASP.NET中设置CheckBox和RadioButton的默认值不可改变
- ASP.NET 后台设置CheckBoxList不可选择。
- ASP.NET: TextBox控件如果设置为ReadOnly则PostBack之后可能丢失数据的问题
- asp.net 页面刷新后样式改变,解决方法
- css技巧,如何设置select、radio 、 checkbox 、file这些不可直接设置的样式 。
- 使用JavaScript脚本无法直接改变Asp.net中Checkbox控件的Enable属性的解决方法
- DataGrid,asp.net样式设置
- 关于使用RadioButton和CheckBox控件(Asp.net)生成html包含<label>控件的一点认识
- ASP.NET页面刷新样式改变解决方法
- 新增格式化单元格数据功能-dotNetFlexGrid1.24,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。(更新FieldFormatorHandle参数)
- Theme、StylesheetTheme设置ASP.NET的样式与主题
- asp.net页面刷新后样式就发生了改变
- ASP.NET: TextBox控件如果设置为ReadOnly则PostBack之后可能丢失数据的问题
- 在ASP.NET中设置文本框的默认值
- 如何给DropDownList控件设置样式(ASP.NET MVC)
- asp.net EF model中的默认值设置
- 新增格式化单元格数据功能-dotNetFlexGrid更新1.24版本,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。