Unwanted behaviour on browser back button

J

Jeff Donkersgoed

I have a registration form that updates a province/state droplist when a
country is selected from another droplist. When the user clicks the back
button though, the province list goes back to the prior country, but the
country list remains the same. This would be ok if the country went back to
the prior country as well. Ideally, I would just like the back button to
just go back to the previous page that linked to the registration page
instead.

I tried playing around with EnableViewState a bit without success thinking
that may have something to do with it.

Below are some code snippets:
aspx
<tr>
<td>Country</td>
<td><asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList></td>
</tr>
<tr>
<td><asp:Label ID="lblProvince" runat="server"></asp:Label></td>
<td><asp:DropDownList ID="ddlProvince"
runat="server"></asp:DropDownList></td>
</tr>

aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// Look up country info
using (DataSet ds =
NNRDReports.XMLHelper.GetDataSetFromXml(@"Xml\CountryInfo.xml"))
{
DataTable dtCountries = ds.Tables["Country"];
ddlCountry.DataTextField = "Name";
ddlCountry.DataValueField = "Code";
ddlCountry.DataSource = dtCountries;
ddlCountry.DataBind();
ddlCountry.SelectedIndex = 0;
CountryChanged();
}
}
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
CountryChanged();
}

private void CountryChanged()
{
ddlProvince.Items.Clear();

// Look up what the country info and populate it into the page
using (DataSet ds =
NNRDReports.XMLHelper.GetDataSetFromXml(@"Xml\CountryInfo.xml"))
{
DataTable dtProvinces = ds.Tables["Province"];
dtProvinces.DefaultView.RowFilter = "CountryCode='" +
ddlCountry.SelectedItem.Value + "'";
lblProvince.Text = (string)dtCountries.DefaultView[0]["RegionType"];
ddlProvince.DataTextField = "Name";
ddlProvince.DataSource = dtProvinces.DefaultView;
ddlProvince.DataBind();
}
}
 
I

Ignacio Machin ( .NET/ C# MVP )

I have a registration form that updates a province/state droplist when a
country is selected from another droplist.  When the user clicks the back
button though, the province list goes back to the prior country, but the
country list remains the same.  This would be ok if the country went back to
the prior country as well.   Ideally, I would just like the back buttonto
just go back to the previous page that linked to the registration page
instead.  

I tried playing around with EnableViewState a bit without success thinking
that may have something to do with it.

Below are some code snippets:
aspx
<tr>
  <td>Country</td>
  <td><asp:DropDownList ID="ddlCountry" runat="server" AutoPostBack="true"
onselectedindexchanged="ddlCountry_SelectedIndexChanged"></asp:DropDownList­></td>
</tr>
<tr>
  <td><asp:Label ID="lblProvince" runat="server"></asp:Label></td>
  <td><asp:DropDownList ID="ddlProvince"
runat="server"></asp:DropDownList></td>
</tr>

aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    // Look up country info
    using (DataSet ds =
NNRDReports.XMLHelper.GetDataSetFromXml(@"Xml\CountryInfo.xml"))
    {
      DataTable dtCountries = ds.Tables["Country"];
      ddlCountry.DataTextField = "Name";
      ddlCountry.DataValueField = "Code";
      ddlCountry.DataSource = dtCountries;
      ddlCountry.DataBind();
      ddlCountry.SelectedIndex = 0;
      CountryChanged();
    }
  }}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{              
  CountryChanged();

}

private void CountryChanged()
{
  ddlProvince.Items.Clear();

  // Look up what the country info and populate it into the page
  using (DataSet ds =
NNRDReports.XMLHelper.GetDataSetFromXml(@"Xml\CountryInfo.xml"))
  {
    DataTable dtProvinces = ds.Tables["Province"];
    dtProvinces.DefaultView.RowFilter = "CountryCode='" +
ddlCountry.SelectedItem.Value + "'";
    lblProvince.Text = (string)dtCountries.DefaultView[0]["RegionType"];
    ddlProvince.DataTextField = "Name";
    ddlProvince.DataSource = dtProvinces.DefaultView;
    ddlProvince.DataBind();                            
  }            



}- Hide quoted text -

- Show quoted text -

Hi,

There is a directive that indicate that no cache should be applied,
expire something is called, do not remember right now. I think that
you can use it to solve your problem.

Also remember that this kind of question are better suited for the
ASPNET NG
 
P

Pavel Minaev

I have a registration form that updates a province/state droplist when a
country is selected from another droplist.  When the user clicks the back
button though, the province list goes back to the prior country, but the
country list remains the same.  This would be ok if the country went back to
the prior country as well.   Ideally, I would just like the back buttonto
just go back to the previous page that linked to the registration page
instead.  

If you want the browser Back button to behave as described, then you
need to avoid using full postback to load province list. From browser
perspective, postback is just navigaiton to another page (in this case
the same one), so Back will just undo that (however, the browser is
usually smart enough to remember values of form controls when
returning back to a filled form - in your case, it remembers the
country selection). So, you need to get rid of the postback - for
example, by using AJAX techniques to update the province list.
 
Top