2014-02-20 69 views
1

我有一個簡單的查詢表單,其中包括名字,姓氏等....首先我插入記錄,但當我點擊提交按鈕它說你必須首先登錄提交查詢。所以我登錄到網站,我需要的數據,我插入第一次在查詢形式的網站.....如何保持這一數據???。 ....所以我不需要再寫一遍。 這裏是我下面的代碼:在asp.net中保存數據

protected void btnSubmit_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      if (Session["userid"] != null) 
      { 
       if (FileUpload1.HasFile) 
       { 
        string str = FileUpload1.FileName; 
        FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "//images//" + str); 
        string path = "~//images//" + str.ToString(); 
        con.Open(); 
        con.Close(); 
        Label1.Text = "File uploaded successfully"; 
        main addinfo = new main(); 
        string Id = Request.QueryString["id"]; 
        string SQL = "insert into mandir(user_id,name,place,description,with_religion_category,with_district,with_religion,photo)values('" + Session["userid"] + "','" + txttemplename.Text.Trim() + "','" + txtaddress.Text.Trim() + "','" + txtdescription.Value + "','" + ddlreligioncategory.SelectedItem.Value + "','" + ddlreligion.SelectedItem.Value + "','" + ddldistrict.SelectedItem.Value + "','" + path + "')"; 
        addinfo.saveData(SQL); 
        txttemplename.Text = ""; 
        txtaddress.Text = ""; 
        txtdescription.Value = ""; 
        lblenquirymsg.Text = "Thank you for providing the information"; 
       } 
       else 
       { 
        Label1.Text = "File not uploaded successfully"; 
       } 
      } 

      else 
      { 
       lblinfo.Text = "You Must Login Or Register To give information"; 

      } 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("{0} Exception caught.", ex); 
     } 
    } 
} 
+0

像userid存儲會話中的整個表單字段,並在這裏檢索它 – Rohan

+0

你可以plz更具體 – user3132068

回答

0

你可能想使用會話吧。會話使用存儲在客戶端中的Cookie。我個人用這種方法,它完全適用於我的需要:)這方面的一個樣本是...

Session["whatevername"]=username; 

如果你想從這個值檢索數據,就像如果會話ID仍處於使用,那麼......

if (Session["whatevername"]!=null) 
{ 
    //Whatever you want to do... 
} 

除了這個......另一種方式做,這是保存值直接餅乾...說你剛按下登錄按鈕...下添加以下代碼按鈕點擊事件...

HttpCookie cookie = new HttpCookie("cookieid"); 
cookie.Values.Add("Username", Username.Text); 
cookie.Values.Add("Password", Password.Text); 

在重裝形式......而不是再次輸入憑據,你做檢測,如果有這樣的餅乾......將這個負載下事件代碼...

if (HttpContext.Current.Request.Cookies["cookieid"] != null) 
{ 
    Username.Text = HttpContext.Current.Request.Cookies["cookieid"]["Username"].ToString(); 
    Password.Text = HttpContext.Current.Request.Cookies["cookieid"]["Password"].ToString(); 
} 
+0

是的我已經在我的代碼中做過,但我只是需要插入值的形式,我做過之前日誌記錄...如何保持這些值,以便我不需要一次又一次地寫入它? – user3132068

+0

編輯我的代碼:) –

0

您需要創建會話變量當登錄成功時。例如:

Session["userid"] = username; 

由於您的代碼檢查「userid」作爲值的會話。用戶名應該是您插入的文本框的值。