Hi Fabiano,
I've found that you've posted another issue on the similiar problem titiled
with
"Subject: RE: Unbounded Datagrid"
I've provide my suggestion that "use DataGrid and generate the DataSource
programatically", you may have a look there for detailed reference.
As for the problem in the thread, I think I'd like to discuss something on
dynamic created web controls on asp.net web page:
In asp.net generally, we put all the controls (Html or ASP.NET Server
controls) as well as normal html element in the page template "aspx" file.
And at runtime, when the page is requested, the page class will be
generated at runtime by parsing the aspx page template and then the actual
page class know what controls need to be render out every time. If we add
control dynamically via code at runtime in code behind, if you only add it
once and not add it when the page is posted back again, you 'll find the
control disappear. For example, if we use the following means to add a
TExtBox
in Page_load
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
TextBox tb = new TextBox();
tb.Id = "txtDynamic";
// add it into a placeholder or other
container control's Controls collection
}
}
Then, when the page is posted back( click a button ), the TextBox will
disappear because we haven't added it when page post back. The same problem
will occur when we add controls in Button's Click serverside event.
For detailed info, you can have a look at the following articles:
#Adding Controls to a Web Forms Page Programmatically
http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskaddingcontrolstowebf
ormspageprogrammatically.asp?frame=true
Also, here is a form thread maybe also helpful on understanding dynamic
added control:
http://groups.google.com/groups?hl=zh-CN&lr=&ie=UTF-8&oe=UTF-8&threadm=#n4
Lib46DHA.260%40TK2MSFTNGP11.phx.gbl&rnum=1&prev=/groups%3Fhl%3Dzh-CN%26lr%3D
%26ie%3DUTF-8%26oe%3DUTF-8%26q%3DDonald%2Bxie%2Bsteven%2BCheng
So as for your situation, I recommend that you not create dynamic controls
but use the DataGrid Template DataBound control. And modify the DataSource
dynamically and rebind it with the DataGrid when necessary., For example,
when the user hit the add button to add a new item, you can retrieve the
DataSource( maybe a DataTable stored in ViewState or Session) and add a new
record in the datatable and rebind the DataGrid.
To make this clearly ,I've created a simple demo page , here is the page's
source:
===============aspx page source=================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>demogrid</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>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp

ataGrid id="dgMain" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="name"
HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Gender">
<ItemTemplate>
<asp

ropDownList ID="lstGender" Runat="server" Enabled="False"
SelectedIndex='<%# DataBinder.Eval(Container.DataItem,"gender") %>'>
<asp:ListItem Selected="True" Value="Male">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp

ropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="email"
HeaderText="Email"></asp:BoundColumn>
<asp:ButtonColumn Text="Delete"
CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp

ataGrid>
</td>
</tr>
<tr>
<td>
Name:<asp:TextBox id="txtName" runat="server"></asp:TextBox>
Gender:<asp

ropDownList id="lstGender" runat="server">
<asp:ListItem Value="Male" Selected="True">Male</asp:ListItem>
<asp:ListItem Value="Female">Female</asp:ListItem>
</asp

ropDownList>
Email:<asp:TextBox id="txtEmail" runat="server"></asp:TextBox>
<asp:Button id="btnAdd" runat="server" Text="Add New"></asp:Button>
</td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
===========code behind page class================
public class demogrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgMain;
protected System.Web.UI.WebControls.TextBox txtName;
protected System.Web.UI.WebControls.DropDownList lstGender;
protected System.Web.UI.WebControls.Button btnAdd;
protected System.Web.UI.WebControls.TextBox txtEmail;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Init_Data();
Bind_Data();
}
}
private void Init_Data()
{
DataTable tb = new DataTable("Users");
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("gender",typeof(int)); // 0(male) or 1(female)
tb.Columns.Add("email",typeof(string));
DataRow row = tb.NewRow();
row["name"] = "Steven Cheng";
row["gender"] = 0;
row["email"] = "(e-mail address removed)";
tb.Rows.Add(row);
ViewState["DATA_SOURCE"] = tb;
}
private void Bind_Data()
{
DataTable tb = ViewState["DATA_SOURCE"] as DataTable;
if(tb != null)
{
dgMain.DataSource = tb;
dgMain.DataBind();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.dgMain.DeleteCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgMain_DeleteComm
and);
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnAdd_Click(object sender, System.EventArgs e)
{
DataTable tb = ViewState["DATA_SOURCE"] as DataTable;
if(tb != null)
{
string name = txtName.Text;
string email = txtEmail.Text;
int gender = lstGender.SelectedIndex;
DataRow row = tb.NewRow();
row["name"] = name;
row["gender"] = gender;
row["email"] = email;
tb.Rows.Add(row);
ViewState["DATA_SOURCE"] = tb;
}
Bind_Data();
}
private void dgMain_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataTable tb = ViewState["DATA_SOURCE"] as DataTable;
if(tb != null)
{
int index = e.Item.ItemIndex;
tb.Rows[index].Delete();
ViewState["DATA_SOURCE"] = tb;
}
Bind_Data();
}
}
================================================
Hope helps. Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx