I am replacing a standard
asp:TextBox control with a custom control. Everything works except that the value entered does is not retained when the page is submitted by the server ( via a separate submit button ). I set a breakpoint in the LoadPostData method and discovered that it never gets called. The same aspx page works fine if I go back to using the
asp:TextBox control. What am I doing wrong? Any help will be appreciated.
Here is my control code:
[ToolboxData("<{0}:TextBoxEx runat=server></{0}:TextBoxEx>")]
public class TextBoxEx : System.Web.UI.WebControls.TextBox, IPostBackDataHandler
{
#region Properties
private string m_onchange;
private string m_size;
private string m_DataField;
public string onchange
{
set { m_onchange = value; }
}
public override string Text
{
get
{
return (ViewState["Text"]==null) ? base.Text : (string) ViewState["Text"];
}
set { ViewState["Text"]=value; }
}
public string Size
{
set { m_size = value; }
}
#endregion
#region Render
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The
HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
RenderInputTag(output);
}
private void RenderInputTag(HtmlTextWriter htw)
{
htw.AddAttribute(HtmlTextWriterAttribute.Value,Tex t);
if (m_onchange != null) htw.AddAttribute(HtmlTextWriterAttribute.Onchange, m_onchange);
if (this.CssClass != "") htw.AddAttribute(HtmlTextWriterAttribute.Class,
Css Class);
if (this.MaxLength != 0) htw.AddAttribute(HtmlTextWriterAttribute.Maxlength ,MaxLength.ToString());
htw.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString(NumberFormatInfo.InvariantInfo)) ;
htw.AddAttribute(HtmlTextWriterAttribute.Size,m_si ze);
// if (this.ReadOnly) htw.AddAttribute(HtmlTextWriterAttribute.ReadOnly, "readonly");
htw.RenderBeginTag(HtmlTextWriterTag.Input);
htw.RenderEndTag();
}
#endregion Render
#region IPostBackDataHandler *** This code is never called
bool IPostBackDataHandler.LoadPostData(
string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
string presentValue = Text;
string postedValue = postCollection[this.UniqueID];
if (!presentValue.Equals(postedValue))
{
Text = postedValue;
return true;
}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
}