关于Web自定义控件的问题,在线等
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
namespace MyWebControls
{
[DefaultProperty( "Text ")]
[ToolboxData( " <{0}:LabelTextBox runat=server> </{0}:LabelTextBox> ")]
public class LabelTextBox : WebControl, IPostBackDataHandler, INamingContainer
{
public LabelTextBox()
: base( "input ")
{
Context.Trace.Write( "LabelTextBox ", "Created ");
}
private static readonly object _textChanged = new object();
public event EndEventHandler TextChanged
{
add { Events.AddHandler(_textChanged, value); }
remove { Events.RemoveHandler(_textChanged, value); }
}
[Bindable(true)]
[Category( "Appearance ")]
[DefaultValue( " ")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState[ "Text "];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState[ "Text "] = value;
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
protected override void CreateChildControls()
{
base.CreateChildControls();
Label label1 = new Label();
Controls.Add(label1);
TextBox textBox1 = new TextBox();
Controls.Add(textBox1);
}
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text ");
if (ViewState[ "Text "] != null)
writer.AddAttribute( "value ", (String)ViewState[ "Text "]);
}
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
bool raiseEvent = false;
Context.Trace.Write( "LabelTextBox ", "Previous value: " + Text);
if (Text != postCollection[postDataKey])
raiseEvent = true;
Text = postCollection[postDataKey];
return raiseEvent;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
EventHandler handler = (EventHandler)Events[_textChanged];
if (handler != null)
handler(this, EventArgs.Empty);
}
}
}
生成后将LabelTextBox控件拖到Default.aspx窗体时出现以下错误:
创建控件时出错-LabelTextBox1
未将对象引用设置到对象的实例.
请问谁能解决?????
[解决办法]
帮顶 没写过复合控件
但根据经验 可能是呈现 问题 我不知道问题在哪 呵呵