我用c#+asp+sqlserver做了一个极其简单的投票网站,
从登录网页跳转到投票网页用的是下面这条语句(百度上面搜了好多语句,只试验成功这一条):Response.Write("<script>window.open('Vote.aspx')</script>");
我希望把登录网页的登录用户名传递到投票页面,并且关闭登录网页。这应该怎么做啊?完整的代码在下面:登录页面代码
protected void btn_login_Click(object sender , EventArgs e)
{
SqlParameter idParm = new SqlParameter("name" , txt_username.Text.Trim());
SqlParameter pwParm = new SqlParameter("password" , txt_password.Text.Trim());
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(idParm);
cmd.Parameters.Add(pwParm); StringBuilder strSQL = new StringBuilder();
strSQL.Append("select status from [VoterInfo] where name=@name and password=@password"); using (SqlConnection conn = new SqlConnection("Data Source=lx-work;Initial Catalog=testdb;Integrated Security=True"))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL.ToString(); SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
if (1 == rdr.GetInt32(0))
lbl_loginInfo.Text = "您已经投过票了";
else
{
//skip to vote page
lbl_loginInfo.Text = "ok";
//?id=" + id + "
Response.Write("<script>window.open('Vote.aspx')</script>");
//Response.Redirect("http:////localhost//Unsealed//Vote1//Vote.aspx");
}
else
lbl_loginInfo.Text = "您所输入的用户名密码有误";
}
}
}投票页面代码(每人限投5票,投票成功后将登录用户的登录状态记为1,表示他投过一次不能再投):
protected void Button1_Click(object sender , EventArgs e)
{
IList<CandidateInfo> canList = new List<CandidateInfo>(); if (chk_nly_8.Checked == true)
canList.Add(new CandidateInfo(8));
if (chk_jl_12.Checked == true)
canList.Add(new CandidateInfo(12));
if (chk_pys_13.Checked == true)
canList.Add(new CandidateInfo(13));
if (chk_yj_14.Checked == true)
canList.Add(new CandidateInfo(14));
if (chk_zyb_19.Checked == true)
canList.Add(new CandidateInfo(19));
if (chk_lxk_20.Checked == true)
canList.Add(new CandidateInfo(20));
if (chk_lq_21.Checked == true)
canList.Add(new CandidateInfo(21));
if (chk_jy_23.Checked == true)
canList.Add(new CandidateInfo(23));
if (chk_zj_28.Checked == true)
canList.Add(new CandidateInfo(28));
if (chk_lgs_41.Checked == true)
canList.Add(new CandidateInfo(41)); if (canList.Count > 5)
{
lbl_voteInfo.Text = "每人最多只能投5票喔!";
return;
}
else
{
SqlCommand cmd = new SqlCommand();
SqlTransaction tran;
SqlParameter canParm,voterParm;
voterParm=new SqlParameter("name",votername);
cmd.Parameters.Add(voterParm);
StringBuilder strSQL = new StringBuilder();
strSQL.Append("update [CandidateInfo] set votes=votes+1 where "); for (int i = 0 ; i < canList.Count ; i++)
{
canParm = new SqlParameter("sn" + i.ToString() , canList[i].sn);
cmd.Parameters.Add(canParm);
strSQL.Append("sn=@sn" + i.ToString() + " or ");
} strSQL.Remove(strSQL.Length - 4 , 4);
strSQL.Append(";").Append("update [VoterInfo] set status=1 where name= @name;"); using (SqlConnection conn = new SqlConnection("Data Source=LX-WORK;Initial Catalog=testdb;Integrated Security=True"))
{
conn.Open();
tran = conn.BeginTransaction(votername + " submit");
cmd.Connection = conn;
cmd.Transaction = tran;
cmd.CommandText = strSQL.ToString(); try
{
cmd.ExecuteNonQuery();
tran.Commit();
}
catch (Exception ex1)
{ lbl_voteInfo.Text=
"Commit Exception Type " + ex1.GetType().ToString() + ":" +
Environment.NewLine + Environment.NewLine +
ex1.Message.ToString();
try
{
tran.Rollback();
}
catch (Exception ex2)
{ lbl_voteInfo.Text=
"Commit Exception Type " + ex2.GetType().ToString() + ":" +
Environment.NewLine + Environment.NewLine +
ex1.Message.ToString();
}
} }
} }
从登录网页跳转到投票网页用的是下面这条语句(百度上面搜了好多语句,只试验成功这一条):Response.Write("<script>window.open('Vote.aspx')</script>");
我希望把登录网页的登录用户名传递到投票页面,并且关闭登录网页。这应该怎么做啊?完整的代码在下面:登录页面代码
protected void btn_login_Click(object sender , EventArgs e)
{
SqlParameter idParm = new SqlParameter("name" , txt_username.Text.Trim());
SqlParameter pwParm = new SqlParameter("password" , txt_password.Text.Trim());
SqlCommand cmd = new SqlCommand();
cmd.Parameters.Add(idParm);
cmd.Parameters.Add(pwParm); StringBuilder strSQL = new StringBuilder();
strSQL.Append("select status from [VoterInfo] where name=@name and password=@password"); using (SqlConnection conn = new SqlConnection("Data Source=lx-work;Initial Catalog=testdb;Integrated Security=True"))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL.ToString(); SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.Read())
if (1 == rdr.GetInt32(0))
lbl_loginInfo.Text = "您已经投过票了";
else
{
//skip to vote page
lbl_loginInfo.Text = "ok";
//?id=" + id + "
Response.Write("<script>window.open('Vote.aspx')</script>");
//Response.Redirect("http:////localhost//Unsealed//Vote1//Vote.aspx");
}
else
lbl_loginInfo.Text = "您所输入的用户名密码有误";
}
}
}投票页面代码(每人限投5票,投票成功后将登录用户的登录状态记为1,表示他投过一次不能再投):
protected void Button1_Click(object sender , EventArgs e)
{
IList<CandidateInfo> canList = new List<CandidateInfo>(); if (chk_nly_8.Checked == true)
canList.Add(new CandidateInfo(8));
if (chk_jl_12.Checked == true)
canList.Add(new CandidateInfo(12));
if (chk_pys_13.Checked == true)
canList.Add(new CandidateInfo(13));
if (chk_yj_14.Checked == true)
canList.Add(new CandidateInfo(14));
if (chk_zyb_19.Checked == true)
canList.Add(new CandidateInfo(19));
if (chk_lxk_20.Checked == true)
canList.Add(new CandidateInfo(20));
if (chk_lq_21.Checked == true)
canList.Add(new CandidateInfo(21));
if (chk_jy_23.Checked == true)
canList.Add(new CandidateInfo(23));
if (chk_zj_28.Checked == true)
canList.Add(new CandidateInfo(28));
if (chk_lgs_41.Checked == true)
canList.Add(new CandidateInfo(41)); if (canList.Count > 5)
{
lbl_voteInfo.Text = "每人最多只能投5票喔!";
return;
}
else
{
SqlCommand cmd = new SqlCommand();
SqlTransaction tran;
SqlParameter canParm,voterParm;
voterParm=new SqlParameter("name",votername);
cmd.Parameters.Add(voterParm);
StringBuilder strSQL = new StringBuilder();
strSQL.Append("update [CandidateInfo] set votes=votes+1 where "); for (int i = 0 ; i < canList.Count ; i++)
{
canParm = new SqlParameter("sn" + i.ToString() , canList[i].sn);
cmd.Parameters.Add(canParm);
strSQL.Append("sn=@sn" + i.ToString() + " or ");
} strSQL.Remove(strSQL.Length - 4 , 4);
strSQL.Append(";").Append("update [VoterInfo] set status=1 where name= @name;"); using (SqlConnection conn = new SqlConnection("Data Source=LX-WORK;Initial Catalog=testdb;Integrated Security=True"))
{
conn.Open();
tran = conn.BeginTransaction(votername + " submit");
cmd.Connection = conn;
cmd.Transaction = tran;
cmd.CommandText = strSQL.ToString(); try
{
cmd.ExecuteNonQuery();
tran.Commit();
}
catch (Exception ex1)
{ lbl_voteInfo.Text=
"Commit Exception Type " + ex1.GetType().ToString() + ":" +
Environment.NewLine + Environment.NewLine +
ex1.Message.ToString();
try
{
tran.Rollback();
}
catch (Exception ex2)
{ lbl_voteInfo.Text=
"Commit Exception Type " + ex2.GetType().ToString() + ":" +
Environment.NewLine + Environment.NewLine +
ex1.Message.ToString();
}
} }
} }
这个也可以传值
session保存登录用户名
Response.Write("<script language='javascript'>window.open('index.aspx','_self')</script>");
response.redirect
比如:
a.aspx
b.aspx
在a中
//定义session 对象
session["a"]=a;
在b中
//接受变量
string str;
str=session["a"];
request.querystring["id"]
1、Responsel.Write("<script>window.open('xxx.aspx?xxx=xxx')</script>");
2、Response.Redirect("xxx.aspx?xxx=xxx");
3、Literal.Text = "<script>location.href='xxx.aspx?xxx=xxx';</script>";
4、Session["xxx"] = xxx;
5、....................