datagrid rows dissapearing on postback

F

Freddie

hi,

i need a datagrid w/ 2 headers, the datagrid is bound if( ! IsPostBack),
Ken Cox kindly shared some code that addes a new header in the
ItemDataBound method, so i moved it to Page_Load, and now the last row
of datagrid disappears on postback - i.e. all item rows disappera after
a few postacks

so far i found only this:

"Problems can arise if, prior to the view state being loaded, you insert
a control into the Controls collection in a position other than the tail
end because the view state information for each child control is tied to
a specific index in the Controls collection."

but i insert the control after viewstate is loaded

here is the code

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false" Inherits="WebApplication3.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</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 MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecolumns="False">
<columns>



<asp:boundcolumn datafield="StringValue"
headertext="String
Value"></asp:boundcolumn>


<asp:boundcolumn datafield="IntegerValue"
headertext="Integer
Value"></asp:boundcolumn>
<asp:boundcolumn datafield="Boolean"
headertext="Boolean"></asp:boundcolumn>
<asp:boundcolumn datafield="CurrencyValue"
headertext="Currency
Value"></asp:boundcolumn>
</columns>
</asp:datagrid>
<asp:Button Runat=server Text="qwe" />
</form>
</body>
</HTML>



using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : Page
{
protected DataGrid DataGrid1;

private void Page_Load(object sender, EventArgs e)
{
if (! IsPostBack)
{
DataGrid1.DataSource = CreateDataSource();
DataGrid1.DataBind();
}

DataGridItem head = new DataGridItem(0, 0, ListItemType.Header);

DataGrid1.Controls[0].Controls.AddAt(0, head);

int intCount;
TableCell fcell;
for(intCount = 0; intCount < 4; intCount++)
{
fcell = new TableCell();
fcell.Text = "qqqqqqqqq";
head.Cells.Add(fcell);
}
}

DataTable CreateDataSource()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
dt.Columns.Add(new DataColumn("Boolean", typeof(bool)));

for (int i = 0; i <= 8; i++)
{
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i + 1);
dr[3] = (i == 4);
dt.Rows.Add(dr);
}
return dt;
}

#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);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{

this.Load += new EventHandler(this.Page_Load);

}
#endregion
}
}
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top