Response.Redirect 同一页面 RadioButtonList SelectedItem
我正在尝试对我们网站上的职位列表进行职位过滤器。作业类型的过滤器包含在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据行为(第一次工作),我相信这描述了发生的情况:
(用户单击按钮 - 导致回发)
ButtonClick - 使用单选按钮设置,执行响应重定向
MyPage.aspx?VT=Permanent(从重定向加载)
(用户单击按钮 - 导致回发)
我相信一个简单的(if !IsPostback)将解决问题
Based on the behavior (it works the first time) I believe this describes what's happening:
(user clicks button - causes postback)
ButtonClick - uses radio button setting, does response redirect
MyPage.aspx?VT=Permanent (load from redirect)
(user clicks button - causes postback)
I believe a simple (if !IsPostback) will fix things
听起来像是重新应用回发值。请参阅这篇有关 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.
由于代码+回发的奇怪性质,马克的回答中的逻辑似乎相当准确,但建议的修复不起作用,因为我已经尝试过将其作为可能的解决方案。下面是一个修改,但据我所知它是有效的。尝试一下,它可能对你有用。
代码隐藏:
重要:
设置方式,它将维护您的选择,在按下按钮时更新 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.
Code Behind:
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.