page display when reloading in ASP.NET

83 views Asked by At

I'm a newbie to ASP.NET, so my question might be a little dumb. Let's say we have a web form with two buttons and a gridview, here is some code

protected void button1_Click(object sender, EventArgs e)
{
   ...retrieve data from database and bind Dataset to gridview.
}

protected void button2_Click(object sender, EventArgs e)
{
   // do nothing
} 

So when I click button1, it displays the gridview with data, fair enough, that's what it should be. But it still displays the gridview with data when I click button2 which does nothing, from my understanding, when I click button2, there is a new postback request to reload and send a new page back, since button2 does nothing, there shouldn't be anything binded with the gridview, so the page should display nothing? But it still displays the gridview with data...

1

There are 1 answers

0
Bryan Dellinger On BEST ANSWER

here is a quick example demonstrating what VDWWD pointed out.

make a new webform. copy paste the code below. hit the first button the gridview loads, hit the second button you get a postback but the gridview remains.

now go into the aspx page and change EnableViewState="true" to EnableViewState="false" and run it again this time when you click the second button the gridview data is not persisted.

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" EnableViewState="true" %>

<!DOCTYPE html>

<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"></asp:GridView>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />

    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication3
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            TestObject test1 = new TestObject()
            {
                firstName = "Fred",
                lastName = "Smith",
                phone = "334-456-7698"
            };
            TestObject test2 = new TestObject()
            {
                firstName = "Mary",
                lastName = "Jones",
                phone = "344-556-7558"
            };
            List<TestObject> list = new List<TestObject>();
            list.Add(test1);
            list.Add(test2);

            GridView1.DataSource = list;
            GridView1.DataBind();

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //do nothing
        }
    }

    internal class TestObject
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string phone { get; set; }
    }
}