views:

456

answers:

1

I have a gridview that is being populated from a sql database. When the results are displayed I am linking the district #'s to a new contact page for each one. Ex. d1.aspx, d2.aspx, d3.aspx etc. On each of those pages there is a link to return back to the orginal page. When this happens the original page is refrreshed and the previous gridview results are gone. Is it possible to stop this from happening or is there a better way to do something like this?

<Columns>
<asp:BoundField HeaderText="Name" DataField="school" /><asp:BoundField />

<asp:TemplateField HeaderText="District" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("district", "cdms/d{0}.aspx") %>' Text='<%# Eval("district") %>'>


</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>

<EmptyDataTemplate>
<span style="color: rgb(255, 0, 0);">No records found!</span>
</EmptyDataTemplate>
</asp:GridView>

Codebehind:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
            BuildGridView1();
            GridView1.DataBind();
            GridView1.Visible = true;
    }

    private void BuildGridView1()
    {
        GridView1.DataSource = new Select(District.Schema.TableName + ".*")
          .From(District.Schema)
          .Where(District.Columns.Zip).IsEqualTo(this.txtZip.Text)
          .OrderAsc(District.Columns.Zip)
          .ExecuteDataSet();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        BuildGridView1();
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
A: 

There are a lot of different ways to handle this situation. A possible solution for you is to store the user's query results in a session variable. On page load, check to see if query results exist. If so, populated the GridView from the session variable. If not, hit the database for the results and add the result to the session variable.

I haven't tested it but something like this should work. :

private void BuildGridView1()
            {
                DataSet ds;
                if (Session["myDataset"] == null)
                {
                    ds = new Select(District.Schema.TableName + ".*")
                         .From(District.Schema)
                         .Where(District.Columns.Zip).IsEqualTo(this.txtZip.Text)
                         .OrderAsc(District.Columns.Zip)
                         .ExecuteDataSet();

                    Session["myDataset"] = ds;
                }
                else
                {
                    ds = (DataSet)Session["myDataSet"];
                }
                GridView1.DataSource = ds;
            }
Alison
Can you provide an example?
Brett
Ok, I see what your saying. I will give it a try. Thanks!
Brett
I'd recommend against using session variables just because most people today use multiple tabs in their browsers. If an end-user has two tabs opened, then one tab will overwrite the session variable set by the other tab. Although, this might happen to very few end-users, it is very confusing to them when it does happen. I'd recommend looking at a way to maybe pass the ZIP value via URL parameterto the page and load it that way.
Jim W
IE8 maintains sessions across tabs (http://blogs.msdn.com/ie/archive/2008/07/28/ie8-and-reliability.aspx). I think IE7 does as well as recent Firefox releases.If you're still concerned about using sessions, you can always store the dataset inside a cache variable and play with the expiry times. The code is similar with some small changes. It's not ideal but it would likely meet your needs.
Alison
Yes, but that's not what I'm talking about. If user has 2 tabs with the query page open and queries two different things, the dataset querying in tab 1 will get overwritten by the dataset in tab 2. There's only 1 session var that's shared between the two tabs, so if you set it in tab 2, then the old value from tab 1 is overwritten. So if user clicks 'next page' in tab 1, it will show page 2 of tab 2's criteria.
Jim W