473,769 Members | 7,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

datagrid rows dissapearing on postback

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="Web Form1.aspx.cs"
AutoEventWireup ="false" Inherits="WebAp plication3.WebF orm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1 </title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.1">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name="vs_defaul tClientScript" content="JavaSc ript">
<meta name="vs_target Schema"
content="http://schemas.microso ft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecol umns="False">
<columns>

<asp:boundcolum n datafield="Stri ngValue"
headertext="Str ing
Value"></asp:boundcolumn >
<asp:boundcolum n datafield="Inte gerValue"
headertext="Int eger
Value"></asp:boundcolumn >
<asp:boundcolum n datafield="Bool ean"
headertext="Boo lean"></asp:boundcolumn >
<asp:boundcolum n datafield="Curr encyValue"
headertext="Cur rency
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.W ebControls;

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

private void Page_Load(objec t sender, EventArgs e)
{
if (! IsPostBack)
{
DataGrid1.DataS ource = CreateDataSourc e();
DataGrid1.DataB ind();
}

DataGridItem head = new DataGridItem(0, 0, ListItemType.He ader);

DataGrid1.Contr ols[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 CreateDataSourc e()
{
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add( new DataColumn("Int egerValue", typeof(Int32))) ;
dt.Columns.Add( new DataColumn("Str ingValue", typeof(string)) );
dt.Columns.Add( new DataColumn("Cur rencyValue", typeof(double)) );
dt.Columns.Add( new DataColumn("Boo lean", 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(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}

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

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

}
#endregion
}
}
Jul 19 '06 #1
0 1186

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5357
by: Glenn Owens | last post by:
I have a DataGrid web control which I've dynamically populated with template columns to be used for bulk-editting. Generally, all of the columns are textbox and/or dropdownlist child controls. Currently everything is working. I can find the updated rows/columns by parsing the posted data collection against the DataGrid DataSource. However, when there is a large amount of DataGridItems (rows) the update processing can take a while. ...
1
3434
by: Michelle Stone | last post by:
Hi all. I have an empty datagrid on my web form. And I add BoundDataColumn(s) to it through code. But after each postback, the columns and rows disappear. As a workabout, I tried to rebind the datagrid to the dataset (stored in a session variable) on the PageLoad function which works fine. But my datagrid has a button-column called EDIT which is supposed to execute some code. But during the postback,
2
1261
by: abigblackman | last post by:
Hi, I have a datagrid that is programatically generated with template columns. There seems to be something happening where only the last record is rendered with any data. I start by creating the template columns like this: <code> TemplateColumn tc.HeaderText = "Name";
4
1577
by: gh | last post by:
I create the columns for the asp.net datagrid at runtime and populates them. I have a template column for checkboxs I created at runtime as well. The user selects the product they want by checking the checkbox, next to it. In the submit button I loop through the datagrids items and if checked I will save the product to the cart. The problem is the grid has no item count when I try to loop through it. I loop through the rows of the...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3565
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.