By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,808 Members | 1,481 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,808 IT Pros & Developers. It's quick & easy.

Losing state on bound controls in user controls

P: n/a
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.RepeaterCommandEventHand ler(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
}
}

Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.RepeaterCommandEventHand ler(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
}
}

Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.