DarrylGodden
DarrylGodden

Reputation: 1534

Response.Redirect Same Page RadioButtonList SelectedItem

I'm trying to do a job filter for the list of jobs on our website. The filter for the job type is wrapped in an UpdatePanel and the button to apply the filters redirects back to the same page.

This is because I will be using the umbraco.library:RequestQueryString in the XSLT to populate the jobs list.

However, the querystring filter value doesn't seem to select the RadioButtonList. For example:

The page loads, but nothing happens because vt is null:

protected void Page_Load(object sender, EventArgs e)
    {
        string vt = Request.QueryString["vt"];

        if (vt != null)
        {
            foreach (ListItem li in rblVacancyType.Items)
            {
                if (li.Value == vt)
                {
                    li.Selected = true;
                }
            }
        }
    }


    <asp:UpdatePanel ID="upSearchFilters" runat="server">
                <ContentTemplate>
                    <p>
                        <asp:RadioButtonList ID="rblVacancyType" runat="server">
                            <asp:ListItem Text="All" Value="all"></asp:ListItem>
                            <asp:ListItem Text="Permanent" Value="permanent"></asp:ListItem>
                            <asp:ListItem Text="Temporary" Value="temporary"></asp:ListItem>
                        </asp:RadioButtonList>
                    </p>
</ContentTemplate>
</asp:UpdatePanel>

Here's the button:

<asp:ImageButton ID="ibFilters" ImageUrl="~/images/buttons/filter-button.png" OnClick="ibApplyFilters_Click" runat="server" />

Here's the procedure:

protected void ibApplyFilters_Click(object sender, EventArgs e)
    {
        Response.Redirect("/careers/join-us/?filters=true&vt=" + rblVacancyType.SelectedValue.ToString());  
    }

Yet when the page redirects the first time, nothing is selected, I click permanent, permanent gets selected. If I then select 'All' or 'Temporary' the selection doesn't change.

Any ideas?

Upvotes: 0

Views: 2027

Answers (3)

KreepN
KreepN

Reputation: 8598

Due to the strange nature of code + postbacks the logic in Mark's answer seems to be quite accurate, but the suggested fix did not work as I had tried that as a possible solution. The below is a modification, but as far as I could see it works. Give it a try, it might work out for you.

<form id="form1" runat="server">
    <div>

         <asp:ScriptManager ID="ScriptManager1" runat="server">
         </asp:ScriptManager>

         <asp:UpdatePanel ID="upSearchFilters" runat="server">
                <ContentTemplate>
                    <p>
                        <asp:RadioButtonList ID="rblVacancyType" runat="server"  
                            AutoPostBack="True">
                            <asp:ListItem Text="All" Value="all"></asp:ListItem>
                            <asp:ListItem Text="Permanent" Value="permanent"></asp:ListItem>
                            <asp:ListItem Text="Temporary" Value="temporary"></asp:ListItem>
                        </asp:RadioButtonList>

                    </p>
                    <p>
                        <asp:ImageButton ID="ibFilters" runat="server" CausesValidation="False" 
                            Height="30px" OnClick="ibApplyFilters_Click" />
                    </p>
                </ContentTemplate>
         </asp:UpdatePanel>

    </div>

</form>

Code Behind:

protected void Page_Load(object sender, EventArgs e)
{
   ibFilters.PostBackUrl = "~/WebForm1.aspx?filters=true&vt=" + rblVacancyType.Text;
   string vt = Request.QueryString["vt"];    
}

Important:

The way this is set up, it will maintain your selection, update the filter parameters in the url on button push, and then assign vt the correct value to be used on filtering anything else on your page.

You must change out my "~/WebForm1.aspx?filters=true&vt=" for your url as well.

Upvotes: 0

Mark Peters
Mark Peters

Reputation: 17775

Based on the behavior (it works the first time) I believe this describes what's happening:

  • MyPage.aspx (original load)
  • Page controls initialized to default
  • Page Load - No query string, no radio button selected

(user clicks button - causes postback)

  • MyPage.aspx (postback)
  • Page controls initialized to default
  • Radio Button Set from ViewState
  • Page Load - No query string, no radio button selected
  • ButtonClick - uses radio button setting, does response redirect

  • MyPage.aspx?VT=Permanent (load from redirect)

  • Page controls initialized to default
  • Page Load - Query string set, radio button selected

(user clicks button - causes postback)

  • MyPage.aspx?VT=Permanent (postback)
  • Page controls initialized to default
  • Radio Button Set from ViewState
  • Page Load - Query string set, radio button set to Permanent (Here is the problem)
  • ButtonClick - uses radio button setting, does response redirect

I believe a simple (if !IsPostback) will fix things

Upvotes: 1

Chris Shain
Chris Shain

Reputation: 51359

Sounds like postback values being re-applied. See this article on ASP.NET page lifecycle: http://msdn.microsoft.com/en-us/library/ms178472.aspx.

Values for controls are re-applied via postback after page_load.

Upvotes: 0

Related Questions