Solved

ASP.NET UpdatePanel - Variable Dropdownlist Causes Invalid Postback Error

Posted on 2009-05-14
2
912 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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