我有两个问题。在aspx页面上,我有3个RadioButtonLists (也称为RBL),它们从数据库表中获取各自的数据。在选择任何RBL时,将回发到同一页面,并将该特定RBL的值输入到ListView正在查找的查询字符串中,以根据该查询字符串过滤出产品。当列表视图读取查询字符串并正确过滤列表时,它工作得很好。
问题1-在回发时,当页面加载时,您从3个RBL中选择的值不会被选中。所以我所有的RBL都没有任何值,即使我在页面上设置了默认值。如何使用您选择的RBL中选择的值重新加载我的页面?
问题2-如果你选择了其他两个RBL中的任何一个,那么当它再次回发时,它不会更新查询字符串,而是会清除第一个值。因此,如果您选取RBL-1中的值,它将回发在查询字符串中更新的特定字段,但是如果您选取RBL-2中的值,它将只回发RBL-2中的值,并清除您刚刚从RBL-1中选取的值。我怎样才能加载我的页面,同时在querystring中保留你之前的选择?
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)));
}发布于 2013-06-13 22:21:13
我想我应该发布代码,我过去常常修复我自己的问题。基于大约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));
}
}发布于 2013-05-03 06:00:48
由于您是动态设置单选按钮列表中的值,因此在回发时页面将为这些控件设置的值实际上并不在那里(页面生命周期将尝试在数据源实际检索可能的值并将这些项添加到控件本身之前将值设置到这些控件中)。
此外,停止使用查询字符串来保存回发值-如果您确实需要,可以设置一些HiddenFields来保存信息,这些将在回发过程中持续存在,您不必担心奇怪的查询字符串问题
https://stackoverflow.com/questions/16348204
复制相似问题