Asp.net 如何解决自定义服务器控件中的回发问题?
我正在创建自定义服务器控件。我在此中创建了一个textbox,并在我的页面中使用了此控件。但是,当页面回发(回发)时,textbox的值会被忽略。我曾想过在此处使用viewstate,但我没有获取textbox的值。可能是我使用了错误的方法。请帮助我Asp.net 如何解决自定义服务器控件中的回发问题?,asp.net,Asp.net,我正在创建自定义服务器控件。我在此中创建了一个textbox,并在我的页面中使用了此控件。但是,当页面回发(回发)时,textbox的值会被忽略。我曾想过在此处使用viewstate,但我没有获取textbox的值。可能是我使用了错误的方法。请帮助我 using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using Sys
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
namespace DNWebComponent {
[DefaultProperty("Text")]
[ToolboxData("<{0}:DNTextBox runat=server></{0}:DNTextBox>")]
public class DNTextBox : WebControl, IDNComponent {
public String _ConnectedField;
private string _label;
private TextBox _txtBox;
private string _connectedField;
private string _MultiSeekField;
private string _InputTable;
public string ControlClientID;
public DNTextBox() {
_txtBox = new TextBox();
}
[PersistToViewState]
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text {
get {
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + AspTextBox.Text + "]" : s);
}
set {
ViewState["Text"] = value;
}
}
public DNTextBox(string label)
: this() {
this._label = label;
}
public String Label {
get { return _label; }
set { _label = value; }
}
public String ConnectedField {
get { return _connectedField; }
set { _connectedField = value; }
}
public String MultiSeekField {
get { return _MultiSeekField; }
set { _MultiSeekField = value; }
}
public String InputTable {
get { return _InputTable; }
set { _InputTable = value; }
}
public TextBox AspTextBox {
get { return _txtBox; }
set { _txtBox = value; }
}
public string DivCss { get; set; }
protected override void RenderContents(HtmlTextWriter output) {
output.Write("<div class=\"" + DivCss + "\" >");
output.Write(Text);
output.Write(_label + ": ");
_txtBox.RenderControl(output);
output.Write("</div>");
}
public bool FillControl() {
return false;
}
protected override void LoadViewState(object savedState) {
base.LoadViewState(savedState);
PropertyInfo[] properties = GetType().GetProperties();
foreach (PropertyInfo property in properties) {
object[] attributes = property.GetCustomAttributes(typeof(PersistToViewState), true);
if (attributes.Length > 0) {
if (ViewState[property.Name] != null)
property.SetValue(this, ViewState[property.Name], null);
}
}
}
protected override object SaveViewState() {
ViewState["Text"] = AspTextBox.Text;
//PropertyInfo[] properties = GetType().GetProperties();
//foreach (PropertyInfo property in properties) {
// object[] attributes = property.GetCustomAttributes(typeof(PersistToViewState), true);
// if (attributes.Length > 0)
// ViewState[property.Name] = property.GetValue(this, null);
//}
return base.SaveViewState();
}
[AttributeUsage(AttributeTargets.Property)]
public class PersistToViewState : Attribute {
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.HTMLControl;
运用系统反思;
名称空间DNWebComponent{
[默认属性(“文本”)]
[ToolboxData(“”)
公共类DNTextBox:WebControl,IDNComponent{
公共字符串_ConnectedField;
私有字符串标签;
私人文本框(txtBox),;
私有字符串_connectedField;
私有字符串_MultiSeekField;
私有字符串_可输入;
公共字符串ControlClientID;
公共DNTextBox(){
_txtBox=新文本框();
}
[持久化视图状态]
[可装订(真实)]
[类别(“外观”)]
[默认值(“”)
[可本地化(正确)]
公共字符串文本{
得到{
字符串s=(字符串)视图状态[“文本”];
返回((s==null)“[”+AspTextBox.Text+“]”:s);
}
设置{
ViewState[“文本”]=值;
}
}
公共DNTextBox(字符串标签)
:此(){
这个._label=标签;
}
公共字符串标签{
获取{return\u label;}
设置{u label=value;}
}
公共字符串连接字段{
获取{return\u connectedField;}
设置{u connectedField=value;}
}
公共字符串多搜索字段{
获取{return\u MultiSeekField;}
设置{u MultiSeekField=value;}
}
公共字符串可输入{
获取{return\u inputable;}
设置{u inputable=value;}
}
公共文本框AspTextBox{
获取{return\u txtBox;}
设置{u txtBox=value;}
}
公共字符串DivCss{get;set;}
受保护的覆盖无效渲染内容(HtmlTextWriter输出){
输出。写入(“”);
输出。写入(文本);
output.Write(_label+“:”);
_txtBox.RenderControl(输出);
输出。写入(“”);
}
公共布尔控制(){
返回false;
}
受保护的覆盖无效LoadViewState(对象保存状态){
base.LoadViewState(savedState);
PropertyInfo[]properties=GetType().GetProperties();
foreach(属性中的PropertyInfo属性){
object[]attributes=property.GetCustomAttributes(typeof(PersistToViewState),true);
如果(attributes.Length>0){
if(ViewState[property.Name]!=null)
SetValue(这个,ViewState[property.Name],null);
}
}
}
受保护的覆盖对象SaveViewState(){
ViewState[“Text”]=AspTextBox.Text;
//PropertyInfo[]properties=GetType().GetProperties();
//foreach(属性中的PropertyInfo属性){
//object[]attributes=property.GetCustomAttributes(typeof(PersistToViewState),true);
//如果(attributes.Length>0)
//ViewState[property.Name]=property.GetValue(this,null);
//}
返回base.SaveViewState();
}
[AttributeUsage(AttributeTargets.Property)]
公共类PersistToViewState:属性{
}
}
}
发生这种情况是因为您没有将子文本框控件添加到控件中-因为文本框不是控件树的一部分,它将不会保留其值(或其他属性)
您应该从CompositeControl(或开发UserControl)派生。比如说,
...
public class DNTextBox : CompositeControl, IDNComponent {
private TextBox _txtBox;
protected override void CreateChildControls()
{
_txtBox = new TextBox();
_txtBox.ID = "T";
this.Controls.Add(_textBox);
}
public TextBox AspTextBox
{
get { EnsureChildControls(); return _txtBox; }
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write("<div class=\"" + DivCss + "\" >");
output.Write(Text);
output.Write(_label + ": ");
base.RenderContents(output); // this will create html for child controls
output.Write("</div>");
}
}
。。。
公共类DNTextBox:CompositeControl,IDNComponent{
私人文本框(txtBox),;
受保护的覆盖无效CreateChildControls()
{
_txtBox=新文本框();
_txtBox.ID=“T”;
this.Controls.Add(_textBox);
}
公共文本框AspTextBox
{
获取{EnsureChildControls();返回_txtBox;}
}
受保护的覆盖无效渲染内容(HtmlTextWriter输出)
{
输出。写入(“”);
输出。写入(文本);
output.Write(_label+“:”);
base.RenderContents(output);//这将为子控件创建html
输出。写入(“”);
}
}
免责声明:说明性代码,未经测试
每当需要引用文本框时,请确保调用AspTextBox属性中所示的
EnsureChildControl
。此代码适用于我。我添加了一个方法add属性
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DNWebComponent {
[DefaultProperty("Text")]
[ToolboxData("<{0}:DNTextBox runat=server></{0}:DNTextBox>")]
public class DNTextBox : TextBox, IDNComponent {
public String _ConnectedField;
private string _label;
private TextBox _txtBox;
private string _connectedField;
private string _MultiSeekField;
private string _InputTable;
public string ControlClientID;
public DNTextBox() {
_txtBox = this;
}
public DNTextBox(string label)
: this() {
this._label = label;
}
public String Label {
get { return _label; }
set { _label = value; }
}
public String ConnectedField {
get { return _connectedField; }
set { _connectedField = value; }
}
public String MultiSeekField {
get { return _MultiSeekField; }
set { _MultiSeekField = value; }
}
public String InputTable {
get { return _InputTable; }
set { _InputTable = value; }
}
public TextBox AspTextBox {
get { return this; }
set { _txtBox = value; }
}
public string DivCss { get; set; }
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name = "FullTrust")]
protected override void AddAttributesToRender(HtmlTextWriter writer) {
writer.AddAttribute("Label", Label);
writer.AddAttribute("Text", Text);
base.AddAttributesToRender(writer);
}
protected override void RenderContents(HtmlTextWriter output) {
output.Write("<div class=\"" + DivCss + "\" >");
output.Write(_label + ": ");
output.Write("</div>");
}
public bool FillControl() {
return false;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.HTMLControl;
名称空间DNWebComponent{
[默认属性(“文本”)]
[ToolboxData(“”)
公共类DNTextBox:TextBox,IDNComponent{
公共字符串_ConnectedField;
私有字符串标签;
私人文本框(txtBox),;
私有字符串_connectedField;
私有字符串_MultiSeekField;
私有字符串_可输入;
公共字符串ControlClientID;
公共DNTextBox(){
_txtBox=这个;
}
公共DNTextBox(字符串标签)
:此(){
这个._label=标签;
}
公共字符串标签{
获取{return\u label;}
设置{u label=value;}
}
公共字符串连接字段{
获取{return\u connectedField;}
设置{u connectedField=value;}
}
公共字符串多搜索字段{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Reflection;
namespace DNWebComponent {
[DefaultProperty("Text")]
[ToolboxData("<{0}:DNTextBox runat=server></{0}:DNTextBox>")]
public class DNTextBox : WebControl, IDNComponent {
public String _ConnectedField;
private string _label;
private TextBox _txtBox;
private string _connectedField;
private string _MultiSeekField;
private string _InputTable;
public string ControlClientID;
public DNTextBox() {
_txtBox = new TextBox();
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text {
get {
String s = (String)ViewState["Text"];
return ((s == null) ? "[" + AspTextBox.Text + "]" : s);
}
set {
ViewState["Text"] = value;
}
}
public DNTextBox(string label)
: this() {
this._label = label;
}
public String Label {
get { return _label; }
set { _label = value; }
}
public String ConnectedField {
get { return _connectedField; }
set { _connectedField = value; }
}
public String MultiSeekField {
get { return _MultiSeekField; }
set { _MultiSeekField = value; }
}
public String InputTable {
get { return _InputTable; }
set { _InputTable = value; }
}
public TextBox AspTextBox {
get { return _txtBox; }
set { _txtBox = value; }
}
public string DivCss { get; set; }
protected override void OnInit(EventArgs e) {
var div = new Panel{ CssClass = DivCss };
div.Controls.Add(new Literal{ Text = Text });
div.Controls.Add(new Literal{ Text = _label + ":" });
div.Controls.Add(_txtBox);
Controls.Add(div);
}
public bool FillControl() {
return false;
}
}
}