【问题标题】:c# - asp.net // why does this value get NULL?c# - asp.net // 为什么这个值会变成 NULL?
【发布时间】:2014-11-04 19:41:45
【问题描述】:

,我有这段代码,当我单击按钮 4 时,我想使用变量作为查询字符串重定向到链接!我在所有代码中都使用了该变量,但是当它到达按钮类时突然变为空!?!?所以下一页结果为/..blabla?Email=0。变量是“PIDPROF”!

求助,我卡住了!提前致谢! :)

namespace DisplayingImages
 {
    public partial class PageView : System.Web.UI.Page
    {

    public string query, constr, query1, query2, query3, PIDVIEW;
    public int PIDPROF;
    public SqlConnection con;
    public void connection()
    {
        constr = ConfigurationManager.ConnectionStrings["Myconnection"].ToString();
        con = new SqlConnection(constr);
        con.Open();

    }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            PIDVIEW = Request.QueryString["Email"];
            PIDPROF = Convert.ToInt32(PIDVIEW);

            HttpContext context = HttpContext.Current;
            SearchedUser();
            imagebindGrid();
            PostSelection();
    }
}


    public void SearchedUser()
    {
        connection();
        String str = "select First_Name,Email_Account,Surname,id from ID where ( id = @search )";
        SqlCommand Srch = new SqlCommand(str, con);
        Srch.Parameters.Add("@search", SqlDbType.Int).Value = PIDPROF;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = Srch;
        DataTable dt = new DataTable();
        da.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            lblemail.Text = dt.Rows[0]["First_Name"].ToString();
            lblname.Text = dt.Rows[0]["Email_Account"].ToString();
        }

    }


    /* Gridview για εικονες */
    public void imagebindGrid()
    {
        connection();
        query = "Select Image from ImageToDB where user_id= " + PIDPROF;
        SqlCommand com = new SqlCommand(query, con);
        SqlDataReader dr = com.ExecuteReader();
        dr.Read();
        Image1.ImageUrl = "Handler1.ashx?id_Image=" + PIDPROF;

   }


    /* Κλασση για το Post */
    private void Txt()
    {
        try
        {

            if (PIDPROF != null)
            {
                connection();
                query1 = "Insert into  Posttext (user_id,Posttext) values (@user_id,@Your_Post)";
                SqlCommand com2 = new SqlCommand(query1, con);

                com2.Parameters.AddWithValue("@user_id", PIDPROF);
                com2.ExecuteNonQuery();

                PostSelection();
            }

        }

        catch (Exception ex)
        {

        }

    }
    /* Κανει select τα κειμενα και τα ανεβαζει απο την βαση στο grid */
    public void PostSelection()
    {
        connection();

        query2 = "Select Posttext from Posttext where user_id= " + PIDPROF;
        SqlCommand com1 = new SqlCommand(query2, con);
        SqlDataReader Read = com1.ExecuteReader();
        grdemployee7.DataSource = Read;
        grdemployee7.DataBind();
        Read.Close();

    }


    /* --------------------Κουμπι για search PROFILE -----------------------------------*/
    public void Button3_Click1(object sender, EventArgs e)
    {
                Response.Redirect("~/WebForm7.aspx");
    }


    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/PhotoView.aspx?Email=" + PIDPROF);
    }

    protected void Button5_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Default.aspx?Email=" + PID);
    }

    /*Logout Button */
    protected void Button1_Click(object sender, EventArgs e)
    {

        System.Web.Security.FormsAuthentication.SignOut();
        Session.Clear();
        Session.RemoveAll();
        Session.Abandon();
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
        HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("Expires", "0");
        FormsAuthentication.SignOut();
        HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
        Response.Redirect("~/Logout.aspx");
    }
    public string USER_PIDPROF { get; set; }
    public DateTime _myid { get; set; }
    public string SN { get; set; }
    public string PS { get; set; }
    public string EM { get; set; }
    public int PID { get; set; }
}
  }

【问题讨论】:

  • 在断点后每个变量似乎在页面完全加载后都为空,因此它不能被解析为 URL 中的查询字符串?!怎么保存?

标签: c# asp.net null request.querystring


【解决方案1】:

当用户点击按钮时,页面有另一个请求,所以类被重新实例化。在第二个请求中,Page.IsPostBacktrue,因此填充 PIDPROF 的代码永远不会被调用。如果你将它移到if 块之外,它应该对你有用。

protected void Page_Load(object sender, EventArgs e)
{
    PIDVIEW = Request.QueryString["Email"];
    PIDPROF = Convert.ToInt32(PIDVIEW);

    if (!IsPostBack)
    {
        HttpContext context = HttpContext.Current;
        SearchedUser();
        imagebindGrid();
        PostSelection();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多