zoukankan      html  css  js  c++  java
  • 二、自定义控件之RadioButtonList

    昨天下午弄了一个CheckBoxList 功能暂够用……今早 同样 整了一个RadioButtonList……

    发现一个问题

    如果这样写

    View Code
     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

    但是 如果 我把方法直接写到属性中 即可

    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></{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/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    将make的输出重定向到文件
    ubuntu mount u盘以及cp拷贝文件夹
    Emacs Tutorial摘录
    c#实现每隔一段时间执行代码(多线程)
    socket.BeginReceiveFrom异步接收信息时,回调函数无法正常进入
    23个C#实用技巧
    C#中实现Form的透明属性变化即渐隐效果
    C#键位定制:设置一个文本框只能输入数字键
    byte 与 bit 的转换
    C# Socket UDP 案例 2
  • 原文地址:https://www.cnblogs.com/PEPE/p/2182163.html
Copyright © 2011-2022 走看看