Dynamic Datagrid with TextBoxes Textchanged events wont fire

J

Jean-Pierre Fouche

Hi,

I have the following code (using the VS IDE), which creates a datagrid
with template columns (TextBoxes). The grid is bound to a dataview.
As yet, I have not bound the TextBoxes. I intend to bind the TextBoxes
in the ItemDataBound event of the grid.

My question is, why won't the TextBoxChanged event fire?
I have added the handler for the TextChanged event to the TextBox
column, but the event won't fire.

Please could somebody help. I've been on this problem for about 3
months!

jp


code follows:--->


<!-- WebForm1.aspx --->

<%@ Page language="c#" Trace="true" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="tst.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<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">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 95px; POSITION:
absolute; TOP: 10px" runat="server" Text="Button" Width="252px"
Height="30px"></asp:Button>
</form>
</body>
</HTML>


//WebForm1.aspx.cs

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;
using System.Diagnostics;


namespace tst
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected tst.DataSet1 dataSet11;
protected System.Data.DataView dataView1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Web.UI.WebControls.Button Button1;
public Control Form1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#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);
int intCnt = 0;
string strID;

DataGrid mygrid = new DataGrid();
mygrid.EnableViewState = false;
mygrid.AutoGenerateColumns = false;
mygrid.DataSource = dataView1;

//mygrid.ItemDataBound += new
DataGridItemEventHandler(mygrid_ItemDataBound);
TemplateColumn tm = new TemplateColumn();
strID = intCnt.ToString();
TextBoxTemplate myTextBoxItem = new TextBoxTemplate("CaseID");
intCnt += 1;
TextBox tb = myTextBoxItem.myTextBox;
tb.Text = "Hello";
Trace.Write("TB IS NULL" + (tb==null).ToString());
Trace.Write("tb.ID : " + tb.ID);
tb.TextChanged += new EventHandler(this.TextBoxChanged);

tm.ItemTemplate = myTextBoxItem;
mygrid.Columns.AddAt(0, tm);
Form1.Controls.Add(mygrid);


sqlDataAdapter1.Fill(dataSet11, "vwht_Cases");
mygrid.DataBind();



}


private void TextBoxChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
Trace.Write("TEXTCHANGED IN: " + tb.Text);
}
protected void DoNothing(object o, EventArgs e)
{
Trace.Write("*Do Nothing");
return;
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dataSet11 = new tst.DataSet1();
this.dataView1 = new System.Data.DataView();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginIni
t();
((System.ComponentModel.ISupportInitialize)(this.dataView1)).BeginIni
t();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new
System.Data.Common.DataTableMapping("Table", "vwht_Cases", new
System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("CaseID", "CaseID"),
new
System.Data.Common.DataColumnMapping("CaseRef", "CaseRef"),
new
System.Data.Common.DataColumnMapping("DEP_ORGID", "DEP_ORGID")})});
this.sqlDataAdapter1.RowUpdated += new
System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter1_Row
Updated);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data
source=DEV2\\DEV2_2000;initial catalog=testdb;persist security
info=False;use" +
"r id=sa;password=!secret!;workstation id=DEV2;packet size=4096";
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new
System.Globalization.CultureInfo("en-ZA");
this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
//
// dataView1
//
this.dataView1.Table = this.dataSet11.vwht_Cases;
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT CaseID, CaseRef,
DEP_ORGID FROM vwht_Cases";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.Button1.Click += new System.EventHandler(this.DoNothing);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(
);
((System.ComponentModel.ISupportInitialize)(this.dataView1)).EndInit(
);

}
#endregion

private void sqlDataAdapter1_RowUpdated(object sender,
System.Data.SqlClient.SqlRowUpdatedEventArgs e)
{

}
}

public class TextBoxTemplate : ITemplate
{
public string m_strCtlID;
public TextBox myTextBox = new TextBox();
public TextBoxTemplate(string ID)
{
m_strCtlID = ID;
}
public void InstantiateIn(Control container)
{
myTextBox = new TextBox();
myTextBox.ID = m_strCtlID;
myTextBox.Text = m_strCtlID;
//myTextBox.TextMode="MultiLine";
myTextBox.Wrap=true;

//myTextBox.Height=500;
container.Controls.Add(myTextBox);

}

}



#region Other Classes
public class DDLTemplate : ITemplate
{
public string m_strCtlID;
//public DropDownList myDDL = new DropDownList();
public DDLTemplate(string ID)
{
m_strCtlID = ID;
}
public void InstantiateIn(Control container)
{
Trace.Write("*InstantiateIn - DDL");
DropDownList myDDL = new DropDownList();
myDDL.ID = m_strCtlID;
container.Controls.Add(myDDL);
}

}

#endregion






}


http://www.psam.ru.ac.za
Monitoring Transparency and Accountability in the Public Service, South
Africa
 
C

Cristian Suazo

I had the same problem and that time it was that I made a bind of the
datagrid every time in the page load event. This cause an overwriting of the
viewstate => the events were not detected. I needed the rebind to the
datagrid every time I loaded the page so my solution was to move the
databind of the datagrid to the Page Init event.

I hope this information is helpful to u
Cheers
Cristian

Jean-Pierre Fouche said:
Hi,

I have the following code (using the VS IDE), which creates a datagrid
with template columns (TextBoxes). The grid is bound to a dataview.
As yet, I have not bound the TextBoxes. I intend to bind the TextBoxes
in the ItemDataBound event of the grid.

My question is, why won't the TextBoxChanged event fire?
I have added the handler for the TextChanged event to the TextBox
column, but the event won't fire.

Please could somebody help. I've been on this problem for about 3
months!

jp


code follows:--->


<!-- WebForm1.aspx --->

<%@ Page language="c#" Trace="true" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="tst.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<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">
<asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 95px; POSITION:
absolute; TOP: 10px" runat="server" Text="Button" Width="252px"
Height="30px"></asp:Button>
</form>
</body>
</HTML>


//WebForm1.aspx.cs

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;
using System.Diagnostics;


namespace tst
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
///

public class WebForm1 : System.Web.UI.Page
{
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected tst.DataSet1 dataSet11;
protected System.Data.DataView dataView1;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Web.UI.WebControls.Button Button1;
public Control Form1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#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);
int intCnt = 0;
string strID;

DataGrid mygrid = new DataGrid();
mygrid.EnableViewState = false;
mygrid.AutoGenerateColumns = false;
mygrid.DataSource = dataView1;

//mygrid.ItemDataBound += new
DataGridItemEventHandler(mygrid_ItemDataBound);
TemplateColumn tm = new TemplateColumn();
strID = intCnt.ToString();
TextBoxTemplate myTextBoxItem = new TextBoxTemplate("CaseID");
intCnt += 1;
TextBox tb = myTextBoxItem.myTextBox;
tb.Text = "Hello";
Trace.Write("TB IS NULL" + (tb==null).ToString());
Trace.Write("tb.ID : " + tb.ID);
tb.TextChanged += new EventHandler(this.TextBoxChanged);

tm.ItemTemplate = myTextBoxItem;
mygrid.Columns.AddAt(0, tm);
Form1.Controls.Add(mygrid);


sqlDataAdapter1.Fill(dataSet11, "vwht_Cases");
mygrid.DataBind();



}


private void TextBoxChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
Trace.Write("TEXTCHANGED IN: " + tb.Text);
}
protected void DoNothing(object o, EventArgs e)
{
Trace.Write("*Do Nothing");
return;
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dataSet11 = new tst.DataSet1();
this.dataView1 = new System.Data.DataView();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).BeginIni
t();
((System.ComponentModel.ISupportInitialize)(this.dataView1)).BeginIni
t();
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new
System.Data.Common.DataTableMapping("Table", "vwht_Cases", new
System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("CaseID", "CaseID"),
new
System.Data.Common.DataColumnMapping("CaseRef", "CaseRef"),
new
System.Data.Common.DataColumnMapping("DEP_ORGID", "DEP_ORGID")})});
this.sqlDataAdapter1.RowUpdated += new
System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter1_Row
Updated);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data
source=DEV2\\DEV2_2000;initial catalog=testdb;persist security
info=False;use" +
"r id=sa;password=!secret!;workstation id=DEV2;packet size=4096";
//
// dataSet11
//
this.dataSet11.DataSetName = "DataSet1";
this.dataSet11.Locale = new
System.Globalization.CultureInfo("en-ZA");
this.dataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd";
//
// dataView1
//
this.dataView1.Table = this.dataSet11.vwht_Cases;
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT CaseID, CaseRef,
DEP_ORGID FROM vwht_Cases";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
this.Button1.Click += new System.EventHandler(this.DoNothing);
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet11)).EndInit(
);
((System.ComponentModel.ISupportInitialize)(this.dataView1)).EndInit(
);

}
#endregion

private void sqlDataAdapter1_RowUpdated(object sender,
System.Data.SqlClient.SqlRowUpdatedEventArgs e)
{

}
}

public class TextBoxTemplate : ITemplate
{
public string m_strCtlID;
public TextBox myTextBox = new TextBox();
public TextBoxTemplate(string ID)
{
m_strCtlID = ID;
}
public void InstantiateIn(Control container)
{
myTextBox = new TextBox();
myTextBox.ID = m_strCtlID;
myTextBox.Text = m_strCtlID;
//myTextBox.TextMode="MultiLine";
myTextBox.Wrap=true;

//myTextBox.Height=500;
container.Controls.Add(myTextBox);

}

}



#region Other Classes
public class DDLTemplate : ITemplate
{
public string m_strCtlID;
//public DropDownList myDDL = new DropDownList();
public DDLTemplate(string ID)
{
m_strCtlID = ID;
}
public void InstantiateIn(Control container)
{
Trace.Write("*InstantiateIn - DDL");
DropDownList myDDL = new DropDownList();
myDDL.ID = m_strCtlID;
container.Controls.Add(myDDL);
}

}

#endregion






}


http://www.psam.ru.ac.za
Monitoring Transparency and Accountability in the Public Service, South
Africa
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top