二、自定义控件之RadioButtonList

昨天下午弄了一个CheckBoxList 功能暂够用……今早 同样 整了一个RadioButtonList……
发现一个问题
如果这样写
二、自定义控件之RadioButtonList二、自定义控件之RadioButtonListView Code 1 [ToolboxData("<{0}:FBSRadioBtnList runat=server>")] 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 _disibledevent=>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 + "
Text:" + FBSRadioBtnList1.RadioText; }
取不到值…………
经调试 发现button事件 回发……按钮事件 先执行了按钮事件中方法 其次才去执行
protected override void _disibledevent=>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
但是 如果 我把方法直接写到属性中 即可
View Code 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>")] 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 }


经调试 得到我想要到结果


刚接触 自定义控件 ……还请园中 前辈 指教 ……
谢谢

×××××如果前辈您有相关开发自定义控件 是否可以 提供一份给我 [email protected] 谢谢×××××
Tags: 

延伸阅读

最新评论

发表评论