gpt4 book ai didi

c# - 清除 PostBack 上的 QueryString

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:21 24 4
gpt4 key购买 nike

简单的问题,但我不知道该怎么做。我有一个带有 GridView 的页面,该页面最初填充了一个查询字符串。

获得查询字符串值后,我不需要查询字符串,因为我使用 DropDownList 的值来填充 GridView。

我怎样才能摆脱它?

回发不会清除它,它只是继续标记。

我尝试了 Request.QueryString.Clear,但出现“只读”错误。

如果您能在解决这个问题时给我任何帮助,我将不胜感激。

编辑 1

using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Globalization;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

public partial class GV : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
my_DDL();
GridViewBind();
}
}

protected void my_DDL()
{
.......
}

protected void DDL_SelectedIndexChanged(object sender, EventArgs e)
{
PropertyInfo Isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
Isreadonly.SetValue(Request.QueryString, false, null);
Request.QueryString.Clear();
}

public DataTable GridViewBind()
{
//here use in the query the value of querystring or DDL value

}

}

编辑 2

using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GV : System.Web.UI.Page
{
OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);

OdbcDataAdapter dadapter;
DataSet dset;
DataTable dt = new DataTable();
string sql1;
string sql2;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RTD_DDL();

if (Request.QueryString["RTD"].ToString() != "")
{
RTD.SelectedValue = Request.QueryString["RTD"].ToString();
}

if (Request.QueryString["Month"].ToString() != "")
{
MonthYear.SelectedValue = Request.QueryString["Month"].ToString();
}

GridViewBind();
}
}

protected void MonthYear_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewBind();
}

protected void RTD_DDL()
{
RTD.AppendDataBoundItems = true;

string strQuery = " SELECT ... ; ";

OdbcCommand objCmd = new OdbcCommand(strQuery, myConnectionString);
objCmd.CommandType = CommandType.Text;
objCmd.CommandText = strQuery;

try
{
myConnectionString.Open();
RTD.DataSource = objCmd.ExecuteReader();
RTD.DataTextField = "RTD1";
RTD.DataValueField = "RTD";
RTD.DataBind();
RTD.Items.Add(new ListItem("------", ""));
RTD.Items.Add(new ListItem("1", "1"));
RTD.AppendDataBoundItems = true;
GridViewBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
myConnectionString.Close();
}
}

protected void RTD_SelectedIndexChanged(object sender, EventArgs e)
{
MonthYear.Items.Clear();
MonthYear.Items.Add(new ListItem("------", ""));
MonthYear.AppendDataBoundItems = true;

if (RTD.SelectedItem.Value == "1")
{
sql1 = " SELECT ... ; ";
}
else
{
sql1 = " SELECT ...; ";
}


OdbcCommand objCmd = new OdbcCommand(sql1, myConnectionString);
objCmd.Parameters.AddWithValue("?", RTD.SelectedItem.Value);

objCmd.CommandType = CommandType.Text;
objCmd.CommandText = sql1;
objCmd.Connection = myConnectionString;

try
{
myConnectionString.Open();
MonthYear.DataSource = objCmd.ExecuteReader();
MonthYear.DataTextField = "value1";
MonthYear.DataValueField = "value2";
MonthYear.DataBind();
GridViewBind();

if (MonthYear.Items.Count > 1)
{
MonthYear.Enabled = true;
}
else
{
MonthYear.Enabled = false;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
myConnectionString.Close();
}
}

public DataTable GridViewBind()
{

sql2 = " SELECT ... ; ";

try
{
dadapter = new OdbcDataAdapter(sql2, myConnectionString);

if (Request.QueryString["RTD"] != "")
{
dadapter.SelectCommand.Parameters.Add("param1", Request.QueryString["RTD"].ToString());
}

if (RTD.SelectedIndex != 0)
{
dadapter.SelectCommand.Parameters.Add("param1", RTD.SelectedValue.ToString());
}

dadapter.SelectCommand.Parameters.Add("param2", MonthYear.SelectedValue.ToString());
dset = new DataSet();
dset.Clear();
dadapter.Fill(dset);
DataTable dt = dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
return dt;
}
catch (Exception ex)
{
throw ex;
}
finally
{
dadapter.Dispose();
dadapter = null;
myConnectionString.Close();
}
}
}

最佳答案

可能就是您正在寻找的(它使用 System.Reflection)

PropertyInfo Isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

Isreadonly.SetValue(Request.QueryString, false, null);

Request.QueryString.Clear();

关于c# - 清除 PostBack 上的 QueryString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25061089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com