473,385 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Losing state on bound controls in user controls

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
1 1313
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

32
by: Neil Ginsberg | last post by:
We're using SQL Server 7 with an Access 2000 MDB as a front end with ODBC linked tables. I recently created a new set of tables for the app, and users are complaining that unsaved data is being...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
4
by: Souri Challa | last post by:
Hello All, When a datagrid in a web form is getting re populated from view state on post back, it is firing the datagrid Item Created event but the DataGridItem(e.Item.DataItem is null) in the...
5
by: fbwhite | last post by:
I know this issue has been brought up many times, but I have tried many of the solutions to no avail. I wanted to give my specific case to see if someone could be of any help. We are using the...
0
by: bryanp10 | last post by:
I have a DataList on my page which contains multiple DropDownLists. My page defaults to showing six rows in the DataList. I want the ability to dynamically add rows if needed. Right now I'm just...
8
by: mark.norgate | last post by:
I've asked this question before, but still haven't solved it, so am asking again. I am programmatically adding a user control to the page in response to a button click. The user control consists...
0
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
12
by: Michael Lang | last post by:
I'm adding checkbox controls to a panel in a post back, I then have a second post back in which I attempt to process the checkbox controls however they seem to have disappeared off the panel. The...
5
by: LabGeek | last post by:
I have an Access application that I have created that collects data from a scanner and from a weighting scale. The data from the scanner are unique identifiers (barcodes) and the scanner is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.