Solved

ASP.NET UpdatePanel - Variable Dropdownlist Causes Invalid Postback Error

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now