Solved

ASP.NET UpdatePanel - Variable Dropdownlist Causes Invalid Postback Error

Posted on 2009-05-14
2
910 Views
Last Modified: 2013-11-07
I'm going mad trying to solve a problem with my web user control. I have several controls on it (Dropdownlists, Radiobuttonlists), some of which have SelectedIndexChanged events that in turn call methods that re-populate some of the other controls. If I set autopostback to true on the controls that cause these changes and don't have an updatepanel on the control, just allowing full postbacks, then everything is fine (except for the ugly postbacks which won't be acceptable for this control).

However, once I surround these controls (everything but the submit button) in an UpdatePanel, all the population etc works fine, and I lose the full postbacks and it all looks pretty. However, my page's submit button posts to another (external) page. When users submit, but then click the back button on the browser (which they need to be able to do) though, there is an issue:

If, before they had clicked the submit button, they had selected a radiobutton on my control that had updated the values of a particular dropdownlist, then when they submit and then hit the back button they will not be able to do anything on the control without getting an invalid postback error. I know (think I know!) that this is because the values in the dropdownlist are unexpected according to event validation, but don't know what to do about it (I can't/won't disable event validation)!

I've included just the code I think should be relevant. I need to get this project out for testing yesterday. :( Help? Do I have any option apart from disabling event validation (I can't do this, if I have to I'll have to start all over again and think of a new solution)?
<%@ Control Language="C#" AutoEventWireup="false" CodeFile="SearchForm.ascx.cs" Inherits="SearchForm" %>
   <div class="searchform">
       <asp:UpdatePanel runat="server" ID="udpMain">
           <ContentTemplate>
               <div class="sectionhead">
                   <asp:LinkButton runat="server" ID="lkbAvail" CssClass="m" OnClick="lkbToggleAll_Click">
                       By availability
                   </asp:LinkButton>
               </div>
               <asp:Panel ID="pnlAvail" runat="server" CssClass="panel">
                   <asp:DropDownList runat="server" ID="ddlRegions" CssClass="fullddl" OnLoad="ddlRegions_Load">
                   </asp:DropDownList>
                   
                   <asp:RadioButtonList runat="server" ID="rblAccom" RepeatLayout="Flow" RepeatDirection="Horizontal"
                       CssClass="rbl" AutoPostBack="true" OnSelectedIndexChanged="rblAccom_SelectedIndexChanged">
                       <asp:ListItem Value="cs" Selected="True">Option 1</asp:ListItem>
                       <asp:ListItem Value="ap">Option 2</asp:ListItem>
                   </asp:RadioButtonList>
               </asp:Panel>
           </ContentTemplate>
       </asp:UpdatePanel>
   
       <div class="searchbut">
           <asp:LinkButton runat="server" ID="lkbSearch" PostBackUrl="EXTERNAL URL HERE">
               SEARCH
           </asp:LinkButton>
       </div>
   </div>
 
   public partial class SearchForm : System.Web.UI.UserControl {
       protected void ddlRegions_Load(object sender, EventArgs e) {
           if (!IsPostBack) {
               PopulateRegions();
           }
       }
    
       protected void rblAccom_SelectedIndexChanged(object sender, EventArgs e) {
            PopulateRegions();
      }
   
       protected void PopulateRegions() {
           ListItem lsiOption = new ListItem();
           ddlRegions.Items.Clear();
   
           XmlDocument xmdRegions = new XmlDocument();
           xmdRegions.Load(XML URL HERE);
   
           XmlNodeList nodeList;
   
           string xmlns = xmdRegions.DocumentElement.Attributes["xmlns"].Value;
           XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmdRegions.NameTable);
           nsmgr.AddNamespace("pfix", xmlns);
   
           nodeList = xmdRegions.SelectNodes("//pfix:Region", nsmgr);
   
           foreach (XmlNode node in nodeList) {
               if (rblAccom.SelectedValue == "cs") { // Option 1
                   if (!node.SelectSingleNode("pfix:RegionName", nsmgr).InnerText.Contains("/option2")) {
                       lsiOption = new ListItem(HttpUtility.HtmlDecode(node.SelectSingleNode("pfix:RegionName", nsmgr).InnerText),
                           node.SelectSingleNode("pfix:RegionCode", nsmgr).InnerText);
                       ddlRegions.Items.Add(lsiOption);
                   }
               } else { // Option 2
                   if (node.SelectSingleNode("pfix:RegionName", nsmgr).InnerText.Contains("/option2")) {
                       lsiOption = new ListItem(HttpUtility.HtmlDecode(node.SelectSingleNode("pfix:RegionName", nsmgr).InnerText.Split('/')[0]),
                           node.SelectSingleNode("pfix:RegionCode", nsmgr).InnerText);
                       ddlRegions.Items.Add(lsiOption);
                   }
               }
           }
       }
   }

Open in new window

0
Comment
Question by:hbrjgibsonuk
2 Comments
 
LVL 10

Accepted Solution

by:
MaxOvrdrv2 earned 500 total points
ID: 24384753
here's the problem... and here are your options...

Problem: you are submitting to a different page, and thus, all the controls are reset when the user pushes the back button... this is completely normal and it is how it should be as well.

Solutions:

1) you can set the PostBackURL on ALL of the controls to the one where your submit button submits... that way when you submit, the events and controls are reset with the proper information since it goes through both sections of code.

2) you can persist the information from the controls into a session variable, and then check that session on page load... if there is something in the session, load the controls accordingly/set their values one by one based on dependencies, and then fire their relevant functions to populate the dependants.

3) you can move the external URL back into your back-end...

i know it's not much, but it's truly all that you can do for the very simple reason that you are posting to another URL... it's an option they put in so that you wouldn't have to declare two forms, one ASP, the other HTML, to achieve this effect, and/or redirect in the back and and pass the information from the form within a session... it's a "let's make their lives a LITTLE easier", and not a "Let's define new classes and functions that handle postbacks on different pages seemlessly"... sucks but that's how it is...
0
 

Author Comment

by:hbrjgibsonuk
ID: 24393624
Well, I'm a fool, because I'd previously been pondering the fact that I'd probably need to use the session to persist the values of the form anyway, to improve the user experience - but I didn't twig that this would in fact solve my event validation issue too! So, you've put two and two together for me basically, and it works perfectly. You're a star, thank you. :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question