我有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)));
}
答案 0 :(得分:0)
由于您正在动态设置单选按钮列表中的值,因此页面将设置这些控件的值实际上不在回发上(页面生命周期将尝试在数据源之前将值设置到这些控件中)实际上会检索可能的值并将这些项添加到控件本身中。)
此外,停止使用查询字符串来保存回发值 - 如果你真的需要,设置一些HiddenFields来保存信息,那些将通过回发持续存在而你不必担心奇怪的查询字符串问题
答案 1 :(得分:0)
我想我会发布代码,我过去常常修复自己的问题。基于大约10个不同的网站来源和一些试验/错误。
public partial class Bla zay blaz : System.Web.UI.Page
{
string BaseUrl = "/blahblah.aspx?";
string op;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
op = Server.HtmlDecode(Request.QueryString["op"]);
if (op == null)
op = "A";
RadioButtonList1.SelectedValue = op;
}
protected void RadioButtonChanged(object sender, EventArgs e)
{
op = RadioButtonList1.SelectedValue;
if (op == null)
op = "NC";
if (op != "A")
BaseUrl += "Op=" + op + "&";
Response.Redirect(string.Format(BaseUrl, Server.HtmlEncode(op));
}
}