Response.Redirect 同一页面 RadioButtonList SelectedItem

发布于 2024-12-10 18:40:05 字数 1746 浏览 0 评论 0原文

我正在尝试对我们网站上的职位列表进行职位过滤器。作业类型的过滤器包含在 UpdatePanel 中,应用过滤器的按钮会重定向回同一页面。

这是因为我将使用 XSLT 中的 umbraco.library:RequestQueryString 来填充作业列表。

但是,查询字符串过滤器值似乎没有选择RadioButtonList。例如:

页面加载,但没有任何反应,因为 vt 为空:

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>

这是按钮:

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

这是过程:

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

然而,当页面第一次重定向时,没有选择任何内容,我单击永久,永久被选中。如果我选择“全部”或“临时”,选择不会改变。

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风月客 2024-12-17 18:40:05

根据行为(第一次工作),我相信这描述了发生的情况:

  • MyPage.aspx(原始加载)
  • 页面控件初始化为默认
  • 页面加载 - 没有查询字符串,没有选择单选按钮

(用户单击按钮 - 导致回发)

  • MyPage .aspx(回发)
  • 页面控件从 ViewState 页面加载初始化为默认
  • 单选按钮集
  • - 无查询字符串,未选择单选按钮
  • ButtonClick - 使用单选按钮设置,执行响应重定向

  • MyPage.aspx?VT=Permanent(从重定向加载)

  • 页面控件初始化为默认
  • 页面加载 - 查询字符串集,选择单选按钮

(用户单击按钮 - 导致回发)

  • MyPage.aspx?VT=Permanent(回发)
  • 页面控件初始化为默认
  • 值 单选按钮集从 ViewState
  • 页面加载 - 查询字符串集,单选按钮设置为永久(这是问题)
  • ButtonClick - 使用单选按钮设置,是否响应重定向

我相信一个简单的(if !IsPostback)将解决问题

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

惜醉颜 2024-12-17 18:40:05

听起来像是重新应用回发值。请参阅这篇有关 ASP.NET 页面生命周期的文章:http://msdn.microsoft.com /en-us/library/ms178472.aspx

控件的值在 page_load 之后通过回发重新应用。

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.

孤单情人 2024-12-17 18:40:05

由于代码+回发的奇怪性质,马克的回答中的逻辑似乎相当准确,但建议的修复不起作用,因为我已经尝试过将其作为可能的解决方案。下面是一个修改,但据我所知它是有效的。尝试一下,它可能对你有用。

<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>

代码隐藏:

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

重要:

设置方式,它将维护您的选择,在按下按钮时更新 url 中的过滤器参数,然后为 vt 分配正确的值用于过滤页面上的其他任何内容。

您还必须将我的“~/WebForm1.aspx?filters=true&vt=”更改为您的网址。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文