xrimson77
asked on
Loading Postback data for a composite control
I am having trouble with postback data for a composite control. The control is esentially a texbox, but can be a label if the ReadOnly property is set to true. On postback, I can't seem to get the value of the textbox. It is really weird to me also because once the page is fully rendered, the textbox contains the posted value!! What is going on??
aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Namespace="MyControls" TagPrefix="sgm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
TextBox1
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBo x>
<br />
TextBox2
<sgm:SgmTextBox ID="TextBox2" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick" />
</div>
</form>
</body>
</html>
========================== ========== ========== ====
aspx.cs code
using System;
using System.Web.UI;
public partial class _Default :Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_OnClick(object sender, EventArgs e)
{
TextBox1.Text += "!";
TextBox2.Text += "!";
}
}
========================== ========== ========== ====
SgmTextBox.cs code in App_Code
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Special ized;
namespace MyControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:SgmText Box runat=\"server\"></{0}:Sgm TextBox>") ]
public sealed class SgmTextBox : CompositeControl, IPostBackDataHandler
{
public SgmTextBox()
{
}
[Bindable(false)]
[Browsable(true)]
[Category("Behavior")]
[DefaultValue(false)]
[Themeable(true)]
public bool ReadOnly
{
get
{
object readOnly = ViewState["ReadOnly"];
return (readOnly != null) ? (bool)readOnly : false;
}
set
{
ViewState["ReadOnly"] = value;
}
}
[Bindable(true)]
[Browsable(true)]
[Category("Data")]
[DefaultValue("")]
[Themeable(false)]
public string Text
{
get
{
object text = ViewState["Text"];
return (text != null) ? (string)text : string.Empty;
}
set
{
ViewState["Text"] = value;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
if (ReadOnly)
{
Label label = new Label();
label.EnableViewState = false;
Controls.Add(label);
}
else
{
TextBox textBox = new TextBox();
textBox.EnableViewState = false;
textBox.Height = Height;
textBox.Width = Width;
Controls.Add(textBox);
}
}
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string currentText = Text;
string postedText = postCollection[postDataKey ];
if (!currentText.Equals(poste dText, StringComparison.Ordinal))
{
Text = postedText;
return true;
}
return false;
}
public void RaisePostDataChangedEvent( )
{
return;
}
}
}
aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
<%@ Register Namespace="MyControls" TagPrefix="sgm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
TextBox1
<asp:TextBox runat="server" ID="TextBox1"></asp:TextBo
<br />
TextBox2
<sgm:SgmTextBox ID="TextBox2" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick" />
</div>
</form>
</body>
</html>
==========================
aspx.cs code
using System;
using System.Web.UI;
public partial class _Default :Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_OnClick(object sender, EventArgs e)
{
TextBox1.Text += "!";
TextBox2.Text += "!";
}
}
==========================
SgmTextBox.cs code in App_Code
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Special
namespace MyControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:SgmText
public sealed class SgmTextBox : CompositeControl, IPostBackDataHandler
{
public SgmTextBox()
{
}
[Bindable(false)]
[Browsable(true)]
[Category("Behavior")]
[DefaultValue(false)]
[Themeable(true)]
public bool ReadOnly
{
get
{
object readOnly = ViewState["ReadOnly"];
return (readOnly != null) ? (bool)readOnly : false;
}
set
{
ViewState["ReadOnly"] = value;
}
}
[Bindable(true)]
[Browsable(true)]
[Category("Data")]
[DefaultValue("")]
[Themeable(false)]
public string Text
{
get
{
object text = ViewState["Text"];
return (text != null) ? (string)text : string.Empty;
}
set
{
ViewState["Text"] = value;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
if (ReadOnly)
{
Label label = new Label();
label.EnableViewState = false;
Controls.Add(label);
}
else
{
TextBox textBox = new TextBox();
textBox.EnableViewState = false;
textBox.Height = Height;
textBox.Width = Width;
Controls.Add(textBox);
}
}
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string currentText = Text;
string postedText = postCollection[postDataKey
if (!currentText.Equals(poste
{
Text = postedText;
return true;
}
return false;
}
public void RaisePostDataChangedEvent(
{
return;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.