Here is my aspx page:
And here is the code behind:Code:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="GridViewDisappearing._Default" %> <%@ Register Assembly="GridViewDisappearing" Namespace="GridViewDisappearing" TagPrefix="cc1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDataSource1"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" /> <asp:BoundField DataField="Product" HeaderText="Product" SortExpression="Product" /> <asp:BoundField DataField="Warehouse" HeaderText="Warehouse" SortExpression="Warehouse" /> <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SampleDBConnectionString %>" SelectCommand="SELECT [ID], [Product], [Warehouse], [Quantity] FROM [Inventory]"> </asp:SqlDataSource> <asp:Button ID="Button1" runat="server" Text="Button" /> </div> </form> </body> </html>
The first time the page is requested, it displays the gridview with the data. But when I click on the button, the gridview disappears (and only the button shows up). When I comment out the code:Code:using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace GridViewDisappearing { public partial class _Default : MyPage1 { protected void Page_Init(object sender, EventArgs e) { int x = GridView1.Rows.Count; } protected void Page_Load(object sender, EventArgs e) { } } }
int x = GridView1.Rows.Count;
in the Page_Init(), the gridview does NOT disappear. Why is that one line of code causing it to disappear?




Reply With Quote