asp.net//为什么这个值为NULL

本文关键字:NULL net 为什么 asp | 更新日期: 2023-09-27 18:29:18

,我有这样的代码,当我单击按钮4时,我想使用一个变量作为查询字符串重定向到一个链接!我在所有代码中都使用了这个变量,但当它到达按钮类时,它突然变成了null!?!?所以下一页的结果是/。。布拉?电子邮件=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; }
}
  }

asp.net//为什么这个值为NULL

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