468,247 Members | 1,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

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 4080
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by David J. Berman | last post: by
3 posts views Thread by Sam Kuehn | last post: by
reply views Thread by news_server.nc.rr.com | last post: by
1 post views Thread by Jonathan Wood | last post: by
3 posts views Thread by Webmills | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.