Losing state on bound controls in user controls

Discussion in 'ASP .Net' started by Josh, Mar 22, 2005.

  1. Josh

    Josh Guest

    I am trying to move some of my more common controls into user controls.
    However, I seem to have problems maintaining state of databound
    controls that are contained within user controls. On postback the
    controls still display correctly. However, trying to access the
    controls programatically gets me into their default (unbound) values.
    I feel like I am missing something basic about creating user controls.
    Any help is appreciated.

    Below is a sample of what I'm running into. When clicking a button in
    the repeater (which is within a user control), the value of the label
    comes up as "" even though it still displays correctly as
    "A","B","C","D" or "F".

    Thanks in advance,
    Josh

    User Control Design
    -----------------
    <%@ Control Language="c#" AutoEventWireup="false"
    Codebehind="TestControl.ascx.cs"
    Inherits="WebTools.Documents.TestControl"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <asp:Repeater ID="rptTest" Runat=server>
    <ItemTemplate>
    <asp:Label ID="lblTest" Runat=server><%# Container.DataItem
    %></asp:Label>
    <asp:LinkButton ID="lnkTest" Runat=server
    CommandName="Test">Test</asp:LinkButton>
    <br>
    </ItemTemplate>
    </asp:Repeater>
    <asp:Label ID="lblResult" Runat=server>
    </asp:Label>


    User Control CodeBehind
    -----------------------


    namespace WebTools.Documents
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    public class TestControl : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.Repeater rptTest;
    protected System.Web.UI.WebControls.Label lblResult;

    private void Page_Load(object sender, System.EventArgs e)
    {

    }

    public void TestBind()
    {
    string[] strTest = {"A","B","C","D","F"};
    rptTest.DataSource = strTest;
    rptTest.DataBind();

    }
    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.rptTest.ItemCommand += new
    System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rptTest_ItemCommand);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion

    private void rptTest_ItemCommand(object source,
    System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    {
    if (e.CommandName == "Test")
    {
    lblResult.Text = "Test Selected:" +
    ((Label)e.Item.FindControl("lblTest")).Text;
    }
    }
    }
    }

    Page Design
    ---------------

    <%@ Register TagPrefix="uc1" TagName="TestControl"
    Src="TestControl.ascx" %>
    <%@ Page language="c#" Codebehind="TestControlPage.aspx.cs"
    AutoEventWireup="false" Inherits="WebTools.Documents.TestControlPage"
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>TestControlPage</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema
    content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">

    <form id="Form1" method="post" runat="server"><uc1:TestControl
    id=uclTest runat="server"></uc1:TestControl>

    </form>

    </body>
    </HTML>

    Page CodeBehind
    ----------------
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace WebTools.Documents
    {
    public class TestControlPage : System.Web.UI.Page
    {
    protected TestControl uclTest;

    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    if (! IsPostBack)
    {
    uclTest.TestBind();
    }
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion
    }
    }
    Josh, Mar 22, 2005
    #1
    1. Advertising

  2. Josh

    ashish Guest

    because you are not setting Text property :)

    you should set your label to <asp:label id="label1" runat="server"
    text='<% Container.dataitem %>' ></asp:label>


    and it should work

    hth
    -ashish

    Josh wrote:
    > I am trying to move some of my more common controls into user controls.
    > However, I seem to have problems maintaining state of databound
    > controls that are contained within user controls. On postback the
    > controls still display correctly. However, trying to access the
    > controls programatically gets me into their default (unbound) values.
    > I feel like I am missing something basic about creating user controls.
    > Any help is appreciated.
    >
    > Below is a sample of what I'm running into. When clicking a button in
    > the repeater (which is within a user control), the value of the label
    > comes up as "" even though it still displays correctly as
    > "A","B","C","D" or "F".
    >
    > Thanks in advance,
    > Josh
    >
    > User Control Design
    > -----------------
    > <%@ Control Language="c#" AutoEventWireup="false"
    > Codebehind="TestControl.ascx.cs"
    > Inherits="WebTools.Documents.TestControl"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    > <asp:Repeater ID="rptTest" Runat=server>
    > <ItemTemplate>
    > <asp:Label ID="lblTest" Runat=server><%# Container.DataItem
    > %></asp:Label>
    > <asp:LinkButton ID="lnkTest" Runat=server
    > CommandName="Test">Test</asp:LinkButton>
    > <br>
    > </ItemTemplate>
    > </asp:Repeater>
    > <asp:Label ID="lblResult" Runat=server>
    > </asp:Label>
    >
    >
    > User Control CodeBehind
    > -----------------------
    >
    >
    > namespace WebTools.Documents
    > {
    > using System;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    > public class TestControl : System.Web.UI.UserControl
    > {
    > protected System.Web.UI.WebControls.Repeater rptTest;
    > protected System.Web.UI.WebControls.Label lblResult;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    >
    > }
    >
    > public void TestBind()
    > {
    > string[] strTest = {"A","B","C","D","F"};
    > rptTest.DataSource = strTest;
    > rptTest.DataBind();
    >
    > }
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.rptTest.ItemCommand += new
    > System.Web.UI.WebControls.RepeaterCommandEventHandler(this.rptTest_ItemCommand);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    > private void rptTest_ItemCommand(object source,
    > System.Web.UI.WebControls.RepeaterCommandEventArgs e)
    > {
    > if (e.CommandName == "Test")
    > {
    > lblResult.Text = "Test Selected:" +
    > ((Label)e.Item.FindControl("lblTest")).Text;
    > }
    > }
    > }
    > }
    >
    > Page Design
    > ---------------
    >
    > <%@ Register TagPrefix="uc1" TagName="TestControl"
    > Src="TestControl.ascx" %>
    > <%@ Page language="c#" Codebehind="TestControlPage.aspx.cs"
    > AutoEventWireup="false" Inherits="WebTools.Documents.TestControlPage"
    > %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > <HTML>
    > <HEAD>
    > <title>TestControlPage</title>
    > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    > <meta name="CODE_LANGUAGE" Content="C#">
    > <meta name=vs_defaultClientScript content="JavaScript">
    > <meta name=vs_targetSchema
    > content="http://schemas.microsoft.com/intellisense/ie5">
    > </HEAD>
    > <body MS_POSITIONING="GridLayout">
    >
    > <form id="Form1" method="post" runat="server"><uc1:TestControl
    > id=uclTest runat="server"></uc1:TestControl>
    >
    > </form>
    >
    > </body>
    > </HTML>
    >
    > Page CodeBehind
    > ----------------
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    > namespace WebTools.Documents
    > {
    > public class TestControlPage : System.Web.UI.Page
    > {
    > protected TestControl uclTest;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > if (! IsPostBack)
    > {
    > uclTest.TestBind();
    > }
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    > }
    > }
    >
    ashish, Mar 23, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinay Joseph Mathew

    Losing Session State on a State Server

    Vinay Joseph Mathew, Aug 10, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    591
    =?Utf-8?B?c29ubnk=?=
    Sep 16, 2004
  2. Replies:
    0
    Views:
    3,935
  3. Replies:
    8
    Views:
    479
  4. Ian

    Losing Postback state with composite controls

    Ian, Jun 9, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    126
  5. Jason C
    Replies:
    4
    Views:
    673
    Morty Abzug
    Jun 26, 2012
Loading...

Share This Page