2014-11-04 39 views
0

,我有这个代码,当我点击按钮4,我想重定向到一个链接使用变量作为querystring!我在所有的代码中使用了变量,但是当它到达按钮类时突然变为空!!!?所以下一页返回到/..blabla?Email=0。变量是“PIDPROF”!c# - asp.net //为什么这个值得到NULL?

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

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; } 
} 
    } 
+0

在断点化后,每个变量看起来像它在页面加载完成后得到的null,因此它不能被解析为URL中的查询字符串?我如何保存它? – frcake 2014-11-04 19:47:42

回答

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(); 
} 
+0

谢谢你!让我感动! – frcake 2014-11-04 19:53:11