Losing state on bound controls in user controls

J

Josh

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
}
}
 
A

ashish

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
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
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top