表单加载操作运行两次加载和卸载! [英] form load operation running twice as load and unload!
本文介绍了表单加载操作运行两次加载和卸载!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在表中保存用户移动。如果用户登录它将保存用户登录的记录(两次!!)我怀疑代码在加载和卸载时运行两次。我的证据是,如果用户登录并关闭窗口(不登出),则记录1条记录,但如果用户注销,则记录2条记录!
这是我的代码:
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Web;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.Services;
使用 System.Data;
使用 System.Data.SqlClient;
public partial class test:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
if (会话[ usermenu]!= null )
{
string test = Session [ UserMenu]。ToString();
SqlConnection con = new SqlConnection( Data Source = MZC-RWAHDAN \\ MYPROJECT2015; Persist Security Info = True; User ID = sa; Password = Fatima2010; Initial Catalog = Testing_database);
SqlCommand command = new SqlCommand( SELECT * FROM users_login WHERE UserMenu =' + test + ',con);
con.Open();
SqlDataReader DataReader = command.ExecuteReader();
if (会话[ usermenu]。ToString()== Admin)
{
if (DataReader.Read())
{
DateTime DateTimeVariable = DateTime.Now;
SqlConnection con1 = new SqlConnection( Data Source = MZC-RWAHDAN \\ MYPROJECT2015; Persist Security Info = True; User ID = sa; Password = Fatima2010; Initial Catalog = Testing_database);
SqlCommand command1 = new SqlCommand( 插入TrackIt(FullName,UserName,actiontype,actiondesc,actiontime)VALUES(@ param1,@ param2,@ param3,@ param4,@ param5),con1);
con1.Open();
command1.Parameters.Add( new SqlParameter( @ param1,DataReader [ FullName]));
command1.Parameters.Add( new SqlParameter( @ param2,DataReader [ username]));
command1.Parameters.Add( new SqlParameter( @ param3, Accessed_Page));
command1.Parameters.Add( new SqlParameter( @ param4, 访问Admin_Page));
command1.Parameters.Add( new SqlParameter( @ param5,DateTimeVariable));
command1.ExecuteNonQuery();
}
var menu = Page.Master.FindControl( NavigationMenu) as 菜单;
var menu2 = Page.Master.FindControl( NavigationMenu) as 菜单;
menu.Items.Remove(menu.FindItem( Home) );
MenuItem mnuTest = new MenuItem();
mnuTest.NavigateUrl = 〜/ Default.aspx;
mnuTest.Text = Admin;
menu2.Items.Add(mnuTest);
按钮l1 = Page.Master.FindControl( Button1)< span class =code-keyword> as 按钮;
l1.Text = logout;
TextBox1.Text = Session [ usermenu]。ToString();
}
其他
{
如果(DataReader.Read())
{
DateTime DateTimeVariable = DateTime.Now;
SqlConnection con1 = new SqlConnection( Data Source = MZC-RWAHDAN \\ MYPROJECT2015; Persist Security Info = True; User ID = sa; Password = Fatima2010; Initial Catalog = Testing_database);
SqlCommand command1 = new SqlCommand( 插入TrackIt(FullName,UserName,actiontype,actiondesc,actiontime)VALUES(@ param1,@ param2,@ param3,@ param4,@ param5),con1);
con1.Open();
command1.Parameters.Add( new SqlParameter( @ param1,DataReader [ FullName]));
command1.Parameters.Add( new SqlParameter( @ param2,DataReader [ username]));
command1.Parameters.Add( new SqlParameter( @ param3, Accessed_Page));
command1.Parameters.Add( new SqlParameter( @ param4, 试图以 + DataReader [ FullName] +
as + DataReader [ UserMenu ] + ));
command1.Parameters.Add( new SqlParameter( @ param5,DateTimeVariable));
command1.ExecuteNonQuery();
}
Session.Abandon();
Response.Redirect( 〜/ login.aspx,真跨度>);
}
}
else
{
Session.Abandon();
Response.Redirect( 〜/ login.aspx,真跨度>);
}
}
}
谢谢。
解决方案
我找到了解决方案但是不知道为什么会这样做!我添加了unload事件,现在没问题。
受保护覆盖void OnUnload(EventArgs e)
{
base.OnUnload(e);
Session.Abandon();
}
现在它只记录一次数据。
Hi,
I am saving user movement in my table. If the user logs in it will save the record that the user logged in (twice!!) I suspect that the code runs twice on load and unload. my proof is, if the user logs in and closes the window (not logging out) then it records 1 record but if the user logs off then it records 2 records!
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["usermenu"] != null)
{
string test = Session["UserMenu"].ToString();
SqlConnection con = new SqlConnection("Data Source=MZC-RWAHDAN\\MYPROJECT2015;Persist Security Info=True;User ID=sa; Password=Fatima2010;Initial Catalog=Testing_database");
SqlCommand command = new SqlCommand("SELECT * FROM users_login WHERE UserMenu = '" + test + "'", con);
con.Open();
SqlDataReader DataReader = command.ExecuteReader();
if (Session["usermenu"].ToString() == "Admin")
{
if (DataReader.Read())
{
DateTime DateTimeVariable = DateTime.Now;
SqlConnection con1 = new SqlConnection("Data Source=MZC-RWAHDAN\\MYPROJECT2015;Persist Security Info=True;User ID=sa; Password=Fatima2010;Initial Catalog=Testing_database");
SqlCommand command1 = new SqlCommand("insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5)", con1);
con1.Open();
command1.Parameters.Add(new SqlParameter("@param1", DataReader["FullName"]));
command1.Parameters.Add(new SqlParameter("@param2", DataReader["username"]));
command1.Parameters.Add(new SqlParameter("@param3", "Accessed_Page"));
command1.Parameters.Add(new SqlParameter("@param4", "Accessed Admin_Page"));
command1.Parameters.Add(new SqlParameter("@param5", DateTimeVariable));
command1.ExecuteNonQuery();
}
var menu = Page.Master.FindControl("NavigationMenu") as Menu;
var menu2 = Page.Master.FindControl("NavigationMenu") as Menu;
menu.Items.Remove(menu.FindItem("Home"));
MenuItem mnuTest = new MenuItem();
mnuTest.NavigateUrl = "~/Default.aspx";
mnuTest.Text = "Admin";
menu2.Items.Add(mnuTest);
Button l1 = Page.Master.FindControl("Button1") as Button;
l1.Text = "logout";
TextBox1.Text = Session["usermenu"].ToString();
}
else
{
if (DataReader.Read())
{
DateTime DateTimeVariable = DateTime.Now;
SqlConnection con1 = new SqlConnection("Data Source=MZC-RWAHDAN\\MYPROJECT2015;Persist Security Info=True;User ID=sa; Password=Fatima2010;Initial Catalog=Testing_database");
SqlCommand command1 = new SqlCommand("insert into TrackIt (FullName,UserName,actiontype,actiondesc,actiontime) VALUES(@param1,@param2,@param3,@param4,@param5)", con1);
con1.Open();
command1.Parameters.Add(new SqlParameter("@param1", DataReader["FullName"]));
command1.Parameters.Add(new SqlParameter("@param2", DataReader["username"]));
command1.Parameters.Add(new SqlParameter("@param3", "Accessed_Page"));
command1.Parameters.Add(new SqlParameter("@param4", "Tried to access Admin_Page as " + DataReader["FullName"] +
" as " + DataReader["UserMenu"] + ""));
command1.Parameters.Add(new SqlParameter("@param5", DateTimeVariable));
command1.ExecuteNonQuery();
}
Session.Abandon();
Response.Redirect("~/login.aspx", true);
}
}
else
{
Session.Abandon();
Response.Redirect("~/login.aspx", true);
}
}
}
Thanks.
解决方案
Hi,
I found the solution but not sure why it acted like that! I added unload event and now it is ok.
protected override void OnUnload(EventArgs e) { base.OnUnload(e); Session.Abandon(); }
Now it only records the data once.
这篇关于表单加载操作运行两次加载和卸载!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文