今天在使用ServerTransfer和Response.Redirect定位到当前页面来实现刷新页面时,发现了一些现象:
1.使用Response.Redirect刷新本页面,造成当前页面显示的数据消失的情况:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { String Path; string connString = "server=.;database=ComInfo;integrated security=true"; SqlConnection conn = new SqlConnection(connString); conn.Open(); String PassUserid = Request.QueryString["C_Id"];//获取上一页面传递过来的字段值 String strsql = "select E_Id,E_Name,E_Sex,E_Position,E_Organisation,E_Phone,E_Address from Emp where C_Id ='" + PassUserid + " '"; SqlDataAdapter da = new SqlDataAdapter(strsql, conn); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); conn.Close(); } }
A页面,在GridView控件中加一列HypLinkField,其Text值为:详细信息。设置DataNavigateUrlFormatString属性为“~/B..aspx”?C_Id";DataNavigateUrlField属性填写要传递的字段名:C_Id。B页面的.cs文件Load函数中写下以上代码。这样,在A页面点击"详细信息”就能跳转到B页面,在B页面显示Emp表中与C_Id相应的值详细信息。
AntRh8xkP0ima0sgOOzxTomiOdHTOPANFK5GOavhTLyHV7zKgrQaoQoCNL/VPKhwMzGQ+1GkqukUnXV/2/tRitpIqg2LqoZOznIFiG6ZKL5n2tuYXxL+c1uxsMersYVZa8omS5S+ZP0lHdatsXoSVm/I3TMF1XRXvMkAFPAcTRxHg1Zjteq7fvgygwsEzE8erYZJcUzS/6F7oaQQfgpGsKutB6VS8y8A09rVtO4n0oEyXkf4k3XsXrHcuuezoFr0YokHpJZdB3z3gLJgOkknLuwwOQQtnTDmq07Nd9oBspNnQahEtNcYe55Pu5KhCWLJTNrY2p+6ifalBmLPtXQaRKKt/6gJzRvCrwWrz4TZhbLEhFs3vfiD3YfjIf2u8tM05ifE3pjaB7CJUdFPpV1sSDd4t7f7wJjw6xho4+MGswZSh5V0GbEVTZ516b8Ob0+fjIo/tYJl19gGqiKZXA6M1Vo9Z0664eVQS6Gsd3KbhXGCwJIzLqyNXqfYqJs0361IHqJdnc6dSsTdGoltNCd3F7xSg6iJbQCrZONT35ulL5ImQWvJEiE6eCEk2JTU+2YvU+GQv8l0GyWbcG09IZORaS0auOhm5JpuSGp/sRWp8shep8clevGh8bX4BypeQCjTUjCphaa6wTQZTqkh5dFD64SGPv3OClupdJwPeZeBJdOlMhmETqduqkjArpPLPTl0eiGf8/annnUnq94Lz40s6VJWCekqOOkwwSbdgl2l65iKpkR6Q2Ac3ubTkwaXKscNOcy2pAFKF6LokFS0zkJf8eJQQS6eZ3dzhV3RKhb3l6MjIm2fjS/2TCgcDM5kPdcUiG6foAKpEqbM5B3P2kJ7PM1DKUqaYfdaT+hfHv5zW7E4x6tT0sCWfKFn+7Eo45AmtnWgk6lRzdpQTIco2p5h5z5EWSpvqB3g1Zrue67cvA3SwA4bUcOLokoC3wGLwV+hv6H4v9UxMSxrGjx/vFIWOqb8Pbeoe6UxLHidVcSrJIk4O651LLju6RS+GkGaf1gaH8fboCD+rgauT3c5Y9J4rZUz0qGD4eKH1bDa0mrdGjTFr49FdFzjARWcNlTRlUMqgfSN+6E70TrKj3qD6zXxb95Q1M8Omovnwr/lFkMIqswtKMWWj6wS5VTGDLL5NGwbs9FdUEpoWWoPn6Y3J7BEpRo76qVVR4ApA973qFn/giPwT9JN0BYXLhSPKv/+OXGkco4fhdB+gYRB193sEpc7f8AWgB+IXeWx3kO6vsA8oIaPk1cA5pbcfwgKQOgvD5YbIFUk7Y0h/vBp9fZtuOrT0dH9oVnrWpQ5QL8/mznb/Xr1XxFbQXfwAGRSHYjz35ulL5ImQWvJEiE6eCEk2JTU+2YvU+GQv8l0GyV58fH193S1DksTxP7bUDakmL4nfAAAAAElFTkSuQmCC" alt="" />
这种情况下,使用Response.Redirect("B..aspx");刷新时,这些显示的信息就会消失。而使用Server.Transfer则不会出现这种状况。
2.使用Response.Redirect刷新本页面不会造成显示信息消失的情况:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { String strConn = "server=.;database=ComInfo;integrated security=true;"; String PassUserid = Session["C_Id"].ToString(); SqlConnection conn = new SqlConnection(strConn); conn.Open(); string strsql = "select E_Id,E_Name,E_Sex,E_Position,E_Organisation,E_Phone,E_Address from Emp where C_Id ='" + PassUserid + " '"; SqlDataAdapter da = new SqlDataAdapter(strsql, conn); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); conn.Close(); } }
这次页面间传值使用的方法是:Session,在A中用Session["C_Id"]=strId,记录下C_Id的值,在B页面中获取。之后,不管用Response.Redirect还是server.transfer刷新页面,都不会造成数据消失。
分析:第一种方法传递到B页面的值,通过Response.redirect刷新后会释放,导致查询语句查找不到相应的信息。以后再用第一方法进行页面间传值时,要注意使用的跳转语句,不要出现第一种状况。
以下是Server.Transfer和Response.Redirect的对比