Solved

ASP.NET UpdatePanel - Variable Dropdownlist Causes Invalid Postback Error

Posted on 2009-05-14
2
904 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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

10 Experts available now in Live!

Get 1:1 Help Now