2014-02-20 66 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”作为值的会话。用户名应该是您插入的文本框的值。