onTextChanged won't fire in UserControl

D

DanG

Howdy,

In a UserControl, I have a DataGrid containing textboxes. The
textboxes have AutoCallBack=true, and onTextChanged="TextChanged". The
parent control does a LoadControl("Child.ascx") in its Page_Load. The
first time (only) that the UserControl appears, the onTextChanged event
won't fire.

When a textbox first displays with "aaa", the user changes the value to
"bbb", and clicks tab. First, the parent Page_Load runs, and then the
UserControl Page_Load runs, and the textbox changes back to "aaa". :-o

Repeating the steps a second time, the user enters "ccc" and clicks
tab. This time, the parent Page_Load runs, the UserControl Page_Load
runs, and then the TextChanged function is called, which successfully
processes the "ccc". All subsequent updates work fine.

Why doesn't the onTextChanged event fire the first time??? I've stared
at this for three days already. I could use a clue. I read somewhere
to try setting the DataGrid to EnableViewState=false, but that didn't
help.

Cheers
Dan
 
A

Alessandro Zifiglio

hi, seems to work well and i dont experience what you described. Try the
following test i used. What are you doing differently :eek:

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net



page_load of container page :

protected void Page_Load(object sender, EventArgs e)
{
WebUserControl wuc =
(WebUserControl)LoadControl("WebUserControl.ascx");
this.PlaceHolder1.Controls.Add(wuc);
}



Usercontrols declarative code in WebUserControl.ascx :

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>
<h1>
user control1</h1>
<asp:DataGrid ID="DataGrid1" BorderColor="black" BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false" runat="server">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox1" Tex='<%#
DataBinder.Eval(Container.DataItem, "StringValue") %>' AutoPostBack="true"
OnTextChanged="TextBox1_TextChanged" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>




usercontrols code-behind WebUserControl.ascx.cs :



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Load this data only once.
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}

}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}


protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("TextBox1 changed Fired");
}
 
D

DanG

Thanks for all the work, Alessandro.

Btw, I do have this working fine in another project, which I wrote
myself. This current project was written by another developer, but he
quit, and I've been working for the past week on a few unresolved
issues. This is the last one.

I've studied your code, and basically, that's what I'm doing. There
are some minor differences:

I am adding the UserControl to a panel in the container page. The Add
statement is what causes the UserControl Page_Load to run. I have
tried NOT adding the control programmatically, but just having it
defined in the aspx panel. The UserControl Page_Load seems to run
right after the container page's Page_Load, but the results were the
same.

I don't check isPostBack because, since it's always loading the
UserControl, the isPostBack is always true. I DO have a DataBind in
the Page_Load, which I had thought was causing the text field to reset
the value. But even skipping the DataBind didn't change anything. The
value resets even without it. But even if the value didn't change, I'd
still need to get that onTextChaned event to fire.

You know, I think it's also strange that in the UserControl's
Page_Load, the value of the textbox in the grid is always the previous
value (eg, "aaa"). It's not until the TextChanged function finally
runs that I see the "ccc". If the new value would come in at the page
load, I could handle the processing then.

I noticed that you had AutoEventWireup=true, where mine was false. I
changed it, but that didn't fix it.

Any other ideas?

Regards
Dan

Alessandro said:
hi, seems to work well and i dont experience what you described. Try the
following test i used. What are you doing differently :eek:

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net



page_load of container page :

protected void Page_Load(object sender, EventArgs e)
{
WebUserControl wuc =
(WebUserControl)LoadControl("WebUserControl.ascx");
this.PlaceHolder1.Controls.Add(wuc);
}



Usercontrols declarative code in WebUserControl.ascx :

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>
<h1>
user control1</h1>
<asp:DataGrid ID="DataGrid1" BorderColor="black" BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false" runat="server">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox1" Tex='<%#
DataBinder.Eval(Container.DataItem, "StringValue") %>' AutoPostBack="true"
OnTextChanged="TextBox1_TextChanged" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>




usercontrols code-behind WebUserControl.ascx.cs :



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Load this data only once.
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}

}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}


protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("TextBox1 changed Fired");
}



DanG said:
Howdy,

In a UserControl, I have a DataGrid containing textboxes. The
textboxes have AutoCallBack=true, and onTextChanged="TextChanged". The
parent control does a LoadControl("Child.ascx") in its Page_Load. The
first time (only) that the UserControl appears, the onTextChanged event
won't fire.

When a textbox first displays with "aaa", the user changes the value to
"bbb", and clicks tab. First, the parent Page_Load runs, and then the
UserControl Page_Load runs, and the textbox changes back to "aaa". :-o

Repeating the steps a second time, the user enters "ccc" and clicks
tab. This time, the parent Page_Load runs, the UserControl Page_Load
runs, and then the TextChanged function is called, which successfully
processes the "ccc". All subsequent updates work fine.

Why doesn't the onTextChanged event fire the first time??? I've stared
at this for three days already. I could use a clue. I read somewhere
to try setting the DataGrid to EnableViewState=false, but that didn't
help.

Cheers
Dan
 
A

Alessandro Zifiglio

Dan, I wish i could help you more, but i have little to work on, some code
of your non working scenario will be helpful =P
Somethings to avoid are : make sure your datagrid is not rebinding after
postback. So surround the code that binds your datagrid in an ispostback
check as i have done in the code i posted. Another thing also is if you are
calling Page.DataBind in the container page, then this will cause data
binding to occur on the invoked control and all of its child
controls(meaning your datagrid also).

Do not disable viewstate at the control level on your textboxes, if you do
then the textchanged event will fire regardless if the value changed or not,
which is not part of your problem but a common pitfall.
Though, i'll be frank with you, none of what i just mentioned will help
resolve your problem. Try to make a small test situation where what you are
experiencing is happening and postback that code.

Good luck.
Alessandro Zifiglio
http://www.AsyncUI.net

DanG said:
Thanks for all the work, Alessandro.

Btw, I do have this working fine in another project, which I wrote
myself. This current project was written by another developer, but he
quit, and I've been working for the past week on a few unresolved
issues. This is the last one.

I've studied your code, and basically, that's what I'm doing. There
are some minor differences:

I am adding the UserControl to a panel in the container page. The Add
statement is what causes the UserControl Page_Load to run. I have
tried NOT adding the control programmatically, but just having it
defined in the aspx panel. The UserControl Page_Load seems to run
right after the container page's Page_Load, but the results were the
same.

I don't check isPostBack because, since it's always loading the
UserControl, the isPostBack is always true. I DO have a DataBind in
the Page_Load, which I had thought was causing the text field to reset
the value. But even skipping the DataBind didn't change anything. The
value resets even without it. But even if the value didn't change, I'd
still need to get that onTextChaned event to fire.

You know, I think it's also strange that in the UserControl's
Page_Load, the value of the textbox in the grid is always the previous
value (eg, "aaa"). It's not until the TextChanged function finally
runs that I see the "ccc". If the new value would come in at the page
load, I could handle the processing then.

I noticed that you had AutoEventWireup=true, where mine was false. I
changed it, but that didn't fix it.

Any other ideas?

Regards
Dan

Alessandro said:
hi, seems to work well and i dont experience what you described. Try the
following test i used. What are you doing differently :eek:

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net



page_load of container page :

protected void Page_Load(object sender, EventArgs e)
{
WebUserControl wuc =
(WebUserControl)LoadControl("WebUserControl.ascx");
this.PlaceHolder1.Controls.Add(wuc);
}



Usercontrols declarative code in WebUserControl.ascx :

<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs"
Inherits="WebUserControl" %>
<h1>
user control1</h1>
<asp:DataGrid ID="DataGrid1" BorderColor="black" BorderWidth="1"
CellPadding="3"
AutoGenerateColumns="false" runat="server">
<HeaderStyle BackColor="#00aaaa"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox1" Tex='<%#
DataBinder.Eval(Container.DataItem, "StringValue") %>'
AutoPostBack="true"
OnTextChanged="TextBox1_TextChanged" runat="server">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>




usercontrols code-behind WebUserControl.ascx.cs :



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Load this data only once.
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}

}
ICollection CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
for (int i = 0; i < 9; i++)
{
dr = dt.NewRow();

dr[0] = "Item " + i.ToString();

dt.Rows.Add(dr);
}

DataView dv = new DataView(dt);
return dv;
}


protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Response.Write("TextBox1 changed Fired");
}



DanG said:
Howdy,

In a UserControl, I have a DataGrid containing textboxes. The
textboxes have AutoCallBack=true, and onTextChanged="TextChanged". The
parent control does a LoadControl("Child.ascx") in its Page_Load. The
first time (only) that the UserControl appears, the onTextChanged event
won't fire.

When a textbox first displays with "aaa", the user changes the value to
"bbb", and clicks tab. First, the parent Page_Load runs, and then the
UserControl Page_Load runs, and the textbox changes back to "aaa". :-o

Repeating the steps a second time, the user enters "ccc" and clicks
tab. This time, the parent Page_Load runs, the UserControl Page_Load
runs, and then the TextChanged function is called, which successfully
processes the "ccc". All subsequent updates work fine.

Why doesn't the onTextChanged event fire the first time??? I've stared
at this for three days already. I could use a clue. I read somewhere
to try setting the DataGrid to EnableViewState=false, but that didn't
help.

Cheers
Dan
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top