[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Loading Postback data for a composite control

Asked by xrimson77 in Programming for ASP.NET, .NET Framework 2.x

Tags: postback, composite, control, data

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:TextBox>
       <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.Specialized;

namespace MyControls
{

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:SgmTextBox runat=\"server\"></{0}:SgmTextBox>")]
    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(postedText, StringComparison.Ordinal))
            {
                Text = postedText;
                return true;
            }
            return false;
        }
        public void RaisePostDataChangedEvent()
        {
            return;
        }
    }
}

 
Related Solutions
Keywords: Loading Postback data for a composite…
 
Loading Advertisement...
 
[+][-]04/20/07 09:40 PM, ID: 18950805Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Programming for ASP.NET, .NET Framework 2.x
Tags: postback, composite, control, data
Sign Up Now!
Solution Provided By: Edwin_C
Participating Experts: 1
Solution Grade: B
 
 
Loading Advertisement...
20091021-EE-VQP-81