473,382 Members | 1,647 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,382 software developers and data experts.

User Control in Repeater fires multiple events.

I have a problem that I have boiled down to a very simple example.

I have a user control that displays a some data from a business object. On one screen I have a collection of these business objects and wish to display the user control multiple times. On this web page I simply bind the repeater to the data source and in the ItemDataBound event dynamically load the user control via "LoadControl()".

The user control contains an auto post back textbox. When the text in the textbox changes it is to update the database. (It is also to update the web page with the results of a calculation and thus the reason for the update on the _TextChange rather than updating on a submit button.)

The problem occurs when there are multiple user controls on the screen. The user can edit the textbox in a user control, the auto post back fires, the _TextChanged event is called and the database is updated. If the user were to then edit another textbox in another user control then not only does its _TextChange event get called but the first _TextChange event is raised again.

This means that if the user were to edit 9 user controls, then when the 10th is edited, 10 _TextChange events would be raised.

The _TextChange event sets of sophisticated chain of events. It should be raised only when required.

I have no idea why this is happening and no idea about how to solve it.

The code can be downloaded from www.tcpiq.com/temp/UserControlEvents.zip

The web page's source is

<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>
</div>
</form>
</body>

and the code behind is

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);

List<stringtheData = new List<string>();
theData.Add("A");
theData.Add("B");
theData.Add("C");
theData.Add("D");

Repeater1.DataSource = theData;
Repeater1.DataBind();
}

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
WebUserControl webUserControl = (WebUserControl)LoadControl(@"~\WebUserControl.asc x");
e.Item.Controls.Add(webUserControl);
}
}

The user control's source is

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

and the code behind is:

using System;

public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

public void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString() + " " + ((System.Web.UI.WebControls.TextBox) sender).ClientID.ToString() + " Text changed<br>");
}
}
Thanks in advance.

Dave A

Jan 4 '07 #1
1 4318
I am not sure whether anyone looked at this post - there were no replies. But for the record the answer is to setup the databinding in the web page in the OnInit

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);

// Create an abritary collection to bind to.
List<stringtheData = new List<string>();
theData.Add("A");
theData.Add("B");
theData.Add("C");
theData.Add("D");

// We need to rebind to the datasource on the post back since we are dynamically
// loading a user control.
Repeater1.DataSource = theData;
Repeater1.DataBind();

base.OnInit(e);
}
protected void Page_Load(object sender, EventArgs e)
{
}

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
WebUserControl webUserControl = (WebUserControl)LoadControl(@"~\WebUserControl.asc x");
e.Item.Controls.Add(webUserControl);
}
protected void Button1_Click(object sender, EventArgs e)
{

}
}

"Dave A" <da**@sigmasolutionsdonotspamme.com.auwrote in message news:OQ**************@TK2MSFTNGP02.phx.gbl...
I have a problem that I have boiled down to a very simple example.

I have a user control that displays a some data from a business object. On one screen I have a collection of these business objects and wish to display the user control multiple times. On this web page I simply bind the repeater to the data source and in the ItemDataBound event dynamically load the user control via "LoadControl()".

The user control contains an auto post back textbox. When the text in the textbox changes it is to update the database. (It is also to update the web page with the results of a calculation and thus the reason for the update on the _TextChange rather than updating on a submit button.)

The problem occurs when there are multiple user controls on the screen. The user can edit the textbox in a user control, the auto post back fires, the _TextChanged event is called and the database is updated. If the user were to then edit another textbox in another user control then not only does its _TextChange event get called but the first _TextChange event is raised again.

This means that if the user were to edit 9 user controls, then when the 10th is edited, 10 _TextChange events would be raised.

The _TextChange event sets of sophisticated chain of events. It should be raised only when required.

I have no idea why this is happening and no idea about how to solve it.

The code can be downloaded from www.tcpiq.com/temp/UserControlEvents.zip

The web page's source is

<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>
</div>
</form>
</body>

and the code behind is

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);

List<stringtheData = new List<string>();
theData.Add("A");
theData.Add("B");
theData.Add("C");
theData.Add("D");

Repeater1.DataSource = theData;
Repeater1.DataBind();
}

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
WebUserControl webUserControl = (WebUserControl)LoadControl(@"~\WebUserControl.asc x");
e.Item.Controls.Add(webUserControl);
}
}

The user control's source is

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

and the code behind is:

using System;

public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}

public void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write(DateTime.Now.ToString() + " " + ((System.Web.UI.WebControls.TextBox) sender).ClientID.ToString() + " Text changed<br>");
}
}
Thanks in advance.

Dave A



Jan 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David J. Berman | last post by:
Thanks for any help...! My error is: Object reference not set to an instance of an object. > public int DisplayOrder { > get { >>>>>> return (int) ViewState; > }
3
by: Sam Kuehn | last post by:
I have been stuck on this for some time and am desperate for a solution. I run into this all of the time! I will give a over simplified example to illustrate the problem. Say you have 2 user...
0
by: mike | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
0
by: news_server.nc.rr.com | last post by:
How do i perform a databind on a web user control within a repeater or rather how can I access the datasource that is already bound? I have a web user control that displays a table of values (the...
9
by: Jaybuffet | last post by:
my aspx has something like this <asp:Repeater id="Repeater1" runat="server"> <ItemTemplate> <mycontrol:ctl id="ctlId" obj='<%# Container.DataItem %>' showItem="true"/> </ItemTemplate>...
1
by: Jonathan Wood | last post by:
Okay, as evidenced by other questions, I am an experienced programmer very new to ASP.NET. I have a vertical navigation bar. It is all one color with several panels inside it with a different...
8
by: fernandezr | last post by:
I would like to use a user control as a template inside a repeater. Some of the fields in the control should be hidden depending on whether or not there is data. I'm still a ASP .Net newbie so the...
3
by: Webmills | last post by:
Hi all I have a repeater control containing a web user control within its' item template field. Is it possible to pass through a data field into the web user control, such that this is used...
7
by: | last post by:
I have what's probably a simple page lifecycle question related to dynamically evaluating values that are placed by a repeater and dynmically placing user controls that use those values. I'm...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.