我用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();
}
} }
} }

解决方案 »

  1.   

    Response.Write("<script>window.open('Vote.aspx?xxx=xxxx')</script>");
      

  2.   

    Response.Write("<script>location.href='Vote.aspx';window.opener=null;window.close()</script>");
      

  3.   

    如果是普通页面,直接Response.Redirect即可,无需Response.Write
      

  4.   

    Response.Redirect("XXXX.aspx?para=XX")
      

  5.   

    location.href='Vote.aspx?a=1&b=2'
    这个也可以传值
      

  6.   

    url传值
    session保存登录用户名
    Response.Write("<script language='javascript'>window.open('index.aspx','_self')</script>");
    response.redirect
      

  7.   

    Response.Write("<script>window.open('Vote.aspx?xxx=xxxx')</script>");后台读取string id=Request.QueryString["id"].ToString();
      

  8.   

    建议用这个,通过window.open('Vote.aspx?xxx=xxxx')这种方式 的话,用户可以随便修改url地址的
      

  9.   

    用session 方便点
    比如:
    a.aspx
    b.aspx
    在a中
    //定义session 对象
    session["a"]=a;
    在b中
    //接受变量
    string str;
    str=session["a"];
      

  10.   

    Response.Redirect("XXXX.aspx?para=XX")
      

  11.   

    Response.Write("<script>window.open('Vote.aspx?id=你要传递的值')</script>");
    request.querystring["id"]
      

  12.   

    SERVER.trsafer("")  //服务器跳转 URL地址不变 比较牛B点上面的也可以,就是从地址一看就知道页面跳转
      

  13.   

    传值:    Response.Write("<script>window.open('Vote.aspx?用户名=当前用户的ID')</script>");Vote.ASPX 的 page load() 里:    string userID=Request.QueryString["用户名"].ToString();
      

  14.   

    还有个问题,怎么关闭原来的网页?以及,怎样用密码框控件?我用的是普通的winform控件,找不到密码文本框。html的控件不会在c# script里面调用和操纵
      

  15.   

    这个跳转页面和传递参数的方法有很多的:
    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、....................
      

  16.   

    Response.Write("<script>window.open('Vote.aspx?xxx=xxxx')</script>");