Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在回发上清除查询字符串_C#_.net_Query String_Postback - Fatal编程技术网

C# 在回发上清除查询字符串

C# 在回发上清除查询字符串,c#,.net,query-string,postback,C#,.net,Query String,Postback,这个问题很简单,但我不知道怎么做。我有一个带有GridView的页面,它最初填充了一个querystring 在获得querystring值之后,我不需要querystring,因为我使用DropDownList的值来填充GridView 我怎样才能摆脱它 回发并不能清除它,它只是一直在跟踪 我尝试了Request.QueryString.Clear,但出现了“只读”错误 我将非常感谢你在解决这个问题上给我的任何帮助 编辑1 using System; using System.Configur

这个问题很简单,但我不知道怎么做。我有一个带有GridView的页面,它最初填充了一个querystring

在获得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)


我的错误对象引用未设置为对象的实例,当更改DDL中的值时,如果您在引用查询字符串之前清除它…那么,那条狗不会狩猎。谢谢,我用我的
代码隐藏
编辑了我的第一篇文章,因为我不明白…我想您正在做的是清除查询字符串。稍后,您试图访问它,这会导致错误…因为它已不存在。谢谢您,先生,但我不理解您的建议。。。我的完整代码在下面的编辑2在第一篇文章。。。当我更改DDL中的值时,GridView总是从querystring中的值填充。。。
PropertyInfo Isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

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

Request.QueryString.Clear();