c# - asp.net //为什么这个值变为NULL?

时间:2014-11-04 19:41:46

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

,我有这个代码,当我点击按钮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; }
}
  }

1 个答案:

答案 0 :(得分:0)

当用户单击该按钮时,会有另一个页面请求,因此会重新实例化该类。在第二个请求中,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();
}