簡體   English   中英

在BackBack上清除QueryString

[英]Clear QueryString on PostBack

這個問題很簡單,但是我不知道該怎么做。 我有一個帶有GridView的頁面,該頁面最初是用querystring填充的。

獲得querystring值后,不需要查詢字符串,因為我使用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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM