二、自定义控件之RadioButtonList
昨天下午弄了一个CheckBoxList 功能暂够用……今早 同样 整了一个RadioButtonList……
发现一个问题
如果这样写

1 [ToolboxData("<{0}:FBSRadioBtnList runat=server></{0}:FBSRadioBtnList>")]
2 public class FBSRadioBtnList : System.Web.UI.WebControls.RadioButtonList
3 {
4 #region Field
5 private string radioText;
6 private string radioValue;
7 #endregion
8 #region Properties
9 [Description("Text值"), Browsable(true), DefaultValue(""), Category("Appearance")]
10 public string RadioText
11 {
12 get
13 {
14 return radioText;
15 }
16 set
17 {
18 radioText = value;
19 }
20 }
21 [Description("Value值"), Browsable(true), DefaultValue(""), Category("Appearance")]
22 public string RadioValue
23 {
24 get
25 {
26 return radioValue;
27 }
28 set
29 {
30 radioValue = value;
31 }
32 }
33 #endregion
34 #region Control
35 protected override void Render(HtmlTextWriter writer)
36 {
37 base.Render(writer);
38 }
39
40 protected override void OnPreRender(EventArgs e)
41 {
42 base.OnPreRender(e);
43 //取值
44 for (int i = 0; i < this.Items.Count; i++)
45 {
46 if (this.Items[i].Selected == true)
47 RadioText = Items[i].Text;
48 }
49 if (string.IsNullOrEmpty(RadioText))
50 RadioText = "请选择";
51 //设置 默认选中
52 if (!string.IsNullOrEmpty(RadioText))
53 {
54 this.SelectedIndex = -1;
55 for (int j = 0; j < this.Items.Count; j++)
56 {
57 if (string.Equals(Items[j].Text, RadioText, StringComparison.InvariantCultureIgnoreCase))
58 {
59 this.Items[j].Selected = true;
60 }
61 }
62 }
63 }
64 #endregion
65 }
前台调用时
protected void FBSRadioBtnList_Click(object sender, EventArgs e)
{
Label2.Text = "取值:Value:" + FBSRadioBtnList1.RadioValue + " Text:" + FBSRadioBtnList1.RadioText;
}
protected void FBSRadioBtnListSet_Click(object sender, EventArgs e)
{
FBSRadioBtnList1.RadioValue = "1";
Label2.Text = "设置默认选择:Value:" + FBSRadioBtnList1.RadioValue + "</br> Text:" + FBSRadioBtnList1.RadioText;
}
取不到值…………
经调试 发现button事件 回发……按钮事件 先执行了按钮事件中方法 其次才去执行
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
//取值
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Selected == true)
RadioText = Items[i].Text;
}
if (string.IsNullOrEmpty(RadioText))
RadioText = "请选择";
//设置 默认选中
if (!string.IsNullOrEmpty(RadioText))
{
this.SelectedIndex = -1;
for (int j = 0; j < this.Items.Count; j++)
{
if (string.Equals(Items[j].Text, RadioText, StringComparison.InvariantCultureIgnoreCase))
{
this.Items[j].Selected = true;
}
}
}
}
所以 每次得到的值都是Null
但是 如果 我把方法直接写到属性中 即可

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Web.UI;
6 using System.ComponentModel;
7
8 namespace FumaCRM_BS.WebControls
9 {
10 [ToolboxData("<{0}:FBSRadioBtnList runat=server></{0}:FBSRadioBtnList>")]
11 public class FBSRadioBtnList : System.Web.UI.WebControls.RadioButtonList
12 {
13 #region Properties
14 [Description("Text值"), Browsable(true), DefaultValue(""), Category("Appearance")]
15 public string RadioText
16 {
17 get
18 {
19 string strText = string.Empty;
20 for (int i = 0; i < this.Items.Count; i++)
21 {
22 if (this.Items[i].Selected == true)
23 return Items[i].Text;
24 }
25 if (string.IsNullOrEmpty(strText))
26 strText = "请选择";
27 return strText;
28 }
29 set
30 {
31 if (value != null && value != string.Empty && value != "")
32 {
33 this.SelectedIndex = -1;
34 string strText = value;
35 for (int j = 0; j < this.Items.Count; j++)
36 {
37 if (string.Equals(Items[j].Text, strText, StringComparison.InvariantCultureIgnoreCase))
38 {
39 this.Items[j].Selected = true;
40 }
41 }
42 }
43 }
44 }
45 [Description("Value值"), Browsable(true), DefaultValue(""), Category("Appearance")]
46 public string RadioValue
47 {
48 get
49 {
50 string strValue = string.Empty;
51 for (int i = 0; i < this.Items.Count; i++)
52 {
53 if (this.Items[i].Selected == true)
54 return Items[i].Value;
55 }
56 if (string.IsNullOrEmpty(strValue))
57 strValue = "请选择";
58 return strValue;
59 }
60 set
61 {
62 if (value != null && value != string.Empty && value != "")
63 {
64 this.SelectedIndex = -1;
65 string strText = value;
66 for (int j = 0; j < this.Items.Count; j++)
67 {
68 if (string.Equals(Items[j].Value, strText, StringComparison.InvariantCultureIgnoreCase))
69 {
70 this.Items[j].Selected = true;
71 }
72 }
73 }
74 }
75 }
76 #endregion
77 #region Control
78 protected override void Render(HtmlTextWriter writer)
79 {
80 base.Render(writer);
81 }
82
83 #endregion
84 }
85 }
经调试 得到我想要到结果
刚接触 自定义控件 ……还请园中 前辈 指教 ……
谢谢
×××××如果前辈您有相关开发自定义控件 是否可以 提供一份给我 pepe_anhwei@126.com 谢谢×××××
作者:PEPE
出处:http://pepe.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述