2017-07-18 13 views
0

ユーザーが出席者ページから別のページに移動したときにテキストボックスの値を固定し、ラベルを固定したい場合enableviewstate="true"を設定しましたが、私は消えた別のページに行く。ページで静的な値を保持する方法

は、ここに私のhtmlコードです:

<tr> 
    <td class="auto-style3">Login Time:</td> 
    <td class="auto-style4"> 
     <asp:Label ID="lblLogin" enableviewstate="true" runat="server"></asp:Label> 
    </td> 
    <td class="auto-style5">Logout Time:</td> 
    <td> 
     <asp:Label ID="lblLogout" enableviewstate="true" runat="server"></asp:Label> 
    </td> 
</tr> 
<tr> 
    <td class="auto-style3">Remarks:</td> 
    <td class="auto-style4"> 
     <asp:TextBox ID="TextBoxRemarks" runat="server" enableviewstate="true" TextMode="MultiLine"></asp:TextBox> 
    </td> 
</tr> 

はここに私のc-シャープコードです:

TimeSpan logintime = System.DateTime.Now.TimeOfDay; 
TimeSpan time = TimeSpan.Parse("09:20:00.000"); 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     lblAttendance.Text = Session["user"].ToString().Split('^')[1]; 
     lblDate.Text = DateTime.Now.ToString("dd/MM/yyyy"); 
    } 
} 

protected void btnLogin_Click(object sender, EventArgs e) 
{ 
    if (btnLogin.Text == "Login(Daily Attendance)") 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["REGDataConnectionString"].ConnectionString); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Track_UserLog", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@EmployeeId", Session["user"].ToString().Split('^')[0]); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     lblLogin.Text = System.DateTime.Now.ToString("hh:mm"); 
     btnLogin.Text = "Logout(Daily Attendance)"; 
     if (logintime > time)//&& TextBoxRemarks.Text.ToString() == String.Empty) 
     { 
      DateTime today = DateTime.Today; 
      SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["REGDataConnectionString"].ConnectionString); 
      cn.Open(); 
      SqlCommand cd = new SqlCommand("update tblAttendanceDetails set [email protected] where LoginDate=convert(date,GETDATE()) AND EmployeeId=' " + Session["User"].ToString().Split('^')[0] + " '", cn); 
      cd.Parameters.AddWithValue("@rem", TextBoxRemarks.Text); 
      cd.ExecuteNonQuery(); 
      cn.Close(); 
     } 
    } 
    else if (btnLogin.Text == "Logout(Daily Attendance)") 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["REGDataConnectionString"].ConnectionString); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand("Track_logout", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Parameters.AddWithValue("@EmployeeId", Session["user"].ToString().Split('^')[0]); 

     cmd.ExecuteNonQuery(); 

     lblLogout.Text = System.DateTime.Now.ToString("hh:mm"); 
     btnLogin.Text = "Login(Daily Attendance)"; 
    } 
} 
+0

私はあなたがセッションオブジェクトを読むのを見ますが、どこで設定しますか? –

+0

@PatrickHofman私はログインページに設定しました... – Simranjeet

+0

正確にどこが間違っているのかは私には不明です。実際に何が起こったのか、どのようなステップを再現するのかをもっと説明できますか? –

答えて

0

ViewStateは、同じページのポストバックのために動作します。 MSDNから

ビューステートは、特定のASP.NETページの状態情報を提供します。複数のページに関する情報を使用する必要がある場合や、Webサイトを訪問しても情報を保持する必要がある場合は、別の方法を使用して状態を維持する必要があります。アプリケーション状態、セッション状態、またはプロファイルプロパティを使用できます。

このデータを要求するときに同じデータを表示するには、データをアプリケーション状態、セッション状態、またはプロファイルプロパティに保存する必要があります。

次に、このデータをpages要素に手動でバインドする必要があります。

+0

OPはビューステートをまったく使用していないようですので、あなたの答えは役に立たないようです。彼はセッションオブジェクトを使用して変数を格納します。 –

+0

彼はenableviewstate = trueを使用したと述べましたが、最初の段落の別のページに行くとデータが消えています。私はopが別のページからviewstatedページに来るときにデータが残ることを期待すると思います。 –

関連する問題