Solved

ASP.NET UpdatePanel - Variable Dropdownlist Causes Invalid Postback Error

Posted on 2009-05-14
2
914 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

726 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