我有2个问题。在一个 aspx 页面上,我有 3 个 RadioButtonLists(又名 RBL),它们从数据库表中获取每个数据。在任何 RBL 上进行选择后,都会对同一页面进行回发,并将该特定 RBL 的值输入到 ListView 正在查找的查询字符串中,以根据查询字符串过滤掉产品。列表视图在读取查询字符串并正确过滤列表时工作正常。
问题 1 - 在回发时,您从 3 个 RBL 中选择的值在页面加载时不会被选中。所以我所有的 RBL 都没有任何价值,即使我在页面上设置了默认值。如何使用您选择的 RBL 中选择的值重新加载我的页面?
问题 2 - 如果您对其他 2 个 RBL 中的任何一个进行选择,而不是更新查询字符串,它会在再次回发时清除第一个值。因此,如果您在 RBL-1 中选择某些内容,它会在查询字符串中更新该特定字段后回发,但是如果您在 RBL-2 中选择某些内容,它会回发但仅来自 RBL-2 的值,并清除该值你刚刚从 RBL-1 中挑选出来的。如何让我的页面加载,同时将您之前的任何选择保留在查询字符串中?
ASPX code:
<p>Normally Open or Closed:<asp:RadioButtonList ID="RadioButtonList1" runat="server" EnableViewState="true"
AutoPostBack="true" DataSourceID="NormalitySDS" DataTextField="Op"
DataValueField="Op" onselectedindexchanged="RadioButtonAllLists_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:SqlDataSource ID="NormalitySDS" runat="server"
ConnectionString="<%$ 005 %>"
SelectCommand="SELECT DISTINCT [Op] FROM [Matrix] ORDER BY [Op]">
</asp:SqlDataSource>
</p>
<p>Sizes:<asp:RadioButtonList ID="RadioButtonList2" runat="server" EnableViewState="true"
AutoPostBack="True" DataSourceID="SizesSDS" DataTextField="SIZE" RepeatColumns="2"
DataValueField="SIZE" onselectedindexchanged="RadioButtonAllLists_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:SqlDataSource ID="SizesSDS" runat="server"
ConnectionString="<%$ 005 %>"
SelectCommand="SELECT DISTINCT [SIZE] FROM [Matrix] ORDER BY [SIZE]">
</asp:SqlDataSource>
</p>
<p>Body:<asp:RadioButtonList ID="RadioButtonList3" runat="server" EnableViewState="true"
AutoPostBack="True" DataSourceID="BodySDS" DataTextField="Body"
DataValueField="Body" OnSelectedIndexChanged="RadioButtonAllLists_SelectedIndexChanged">
</asp:RadioButtonList>
<asp:SqlDataSource ID="BodySDS" runat="server"
ConnectionString="<%$ 005 %>"
SelectCommand="SELECT DISTINCT [Body] FROM [Matrix] ORDER BY [Body]">
</asp:SqlDataSource>
<SelectParameters>
<asp:QueryStringParameter DefaultValue="NC" Name="Op" QueryStringField="Op" Type="String" />
<asp:QueryStringParameter DefaultValue="0.25" Name="Sz" QueryStringField="Sz" Type="String" />
<asp:QueryStringParameter DefaultValue="304" Name="Body" QueryStringField="Body" Type="String" />
</SelectParameters>
代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Configurator
{
public partial class Product_Config_Full_wQuery : System.Web.UI.Page
{
string BaseUrl = "/Product_Config_Full_wQuery.aspx";
string op;
string op2;
string sz;
string body;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
op = (Server.UrlDecode(Request.QueryString["op"] ));
RadioButtonList1.SelectedIndex = op2;
RadioButtonList1.DataBind();
sz = Server.UrlDecode(Request.QueryString["sz"]);
body = Server.UrlDecode(Request.QueryString["body"]);
}
}
// Combining all actions into a single protected-event
protected void RadioButtonAllLists_SelectedIndexChanged(object sender, EventArgs e)
{
op = RadioButtonList1.SelectedValue.ToString();
sz = RadioButtonList2.SelectedValue.ToString();
body = RadioButtonList3.SelectedValue.ToString();
if (op != null)
{
BaseUrl += "?Op=" + op + "&";
}
//else op = "NC";
if (sz != null)
{
BaseUrl += "Sz=" + sz + "&";
}
if (body != null)
{
BaseUrl += "Body=" + body + "&";
}
Response.Redirect(string.Format(BaseUrl, Server.UrlEncode(op), Server.UrlEncode(sz), Server.UrlEncode(body)));
}