Blue Dream

记录成长的每一个脚印,写下漫长的程序人生
  首页  :: 订阅 订阅  :: 管理

.net中Web自定义控件编写的几个注意事项

Posted on 2006-07-08 16:50  Dennis  阅读(222)  评论(0编辑  收藏  举报

.net中Web自定义控件编写的几个注意事项

如果重载本身父类提供的
enable属性,将导致无法将子控件中的值用viewstate回传,即无法保持状态。所以最好自己定义该类属性并实现。

定义属性时,如果是子控件本身属性的反映,可以直接取其值,值将自动保留,如果属于自己定义的属性,用viewState保留状态

例如:

 

/// <summary>

         /// 文本框值 txtMD是一个Web TextBox

         /// </summary>

         [Bindable(true),

         Category("Appearance"),

         DefaultValue("")]

         public string Text

         {

              get

              {

                 

                   if(txtMD.Text!="")

                       return txtMD.Text;

                   else

                       return "";

              }

 

              set

              {

                  

                   txtMD.Text=value;

              }

         }

 

自定义属性:

/// <summary>

         /// 参考录入窗口宽度

         /// </summary>

         [Bindable(true),

         Category("Appearance"),

         DefaultValue("50")]

         public int TextBoxMaxLength

         {

              get

              {

                   return ViewState[this.UniqueID+"TextBoxMaxLength"]==null?50:(int)ViewState[this.UniqueID+"TextBoxMaxLength"];

              }

              set

              {

                   ViewState[this.UniqueID+"TextBoxMaxLength"]=value;

              }

 

         }


注意:加上this.UniqueID是区分多个相同控件在同一页面上时的相同属性。