- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All TopicsI 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"
<%@ Register Namespace="MyControls" TagPrefix="sgm" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm
<html xmlns="http://www.w3.org/1
<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;
}
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: Edwin_CPosted on 2007-04-20 at 21:40:45ID: 18950805
LoadPostData only postbacks values i HTML elements such as INPUT. Since the textbox in your composite control is not persistent (not there when it is read only), it is not appropriate to use the LoadPostData to retain your value. My suggestion is that you always create a Label and a Textbox in your composite control. In the PreRender event, your hide (make Visible = false) one of them depending on the ReadOnly value. Then you don't need the ViewState to retain the Text value. Instead you can get it directly from the Textbox control. One more thing you should note is that IDs of the Label and Textbox control should be assigned to make sure they are consistent across postbacks.