问题描述
我做了一个登录界面,需要输入验证码,但是每次单击登录按钮,就只是刷新我的验证码,到底怎么回事啊,请给位大侠帮忙调试下了!login.aspx页面代码<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="login.aspx.cs"Inherits="users_login"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title><styletype="text/css">.style1{width:100%;background-color:#CCFFFF;}.style2{width:500px;}.style3{width:169px;}</style></head><body><formid="form1"runat="server"><div><tablealign="center"cellspacing="1"class="style1"><tr><td><tablealign="center"cellspacing="1"class="style2"><tr><tdclass="style3"align="right">用户名:</td><td><asp:TextBoxID="txt_name"runat="server"></asp:TextBox></td></tr><tr><tdclass="style3"align="right">密码:</td><td><asp:TextBoxID="txt_pwd"runat="server"></asp:TextBox></td></tr><tr><tdclass="style3"align="right">验证码:</td><td><asp:TextBoxID="txt_yzm"runat="server"></asp:TextBox> <asp:ImageID="Image1"runat="server"Height="31px"Width="72px"ImageUrl="~/users/ValidNums.aspx"/></td></tr><tr><tdclass="style3"> </td><td> </td></tr><tr><tdclass="style3"> </td><td><asp:ButtonID="btn_login"runat="server"onclick="btn_login_Click"Text="登录"/> <asp:ButtonID="btn_cancel"runat="server"Text="取消"/></td></tr></table></td></tr></table></div></form></body></html>login.aspx.cs代码如下usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Data.SqlClient;usingSystem.Drawing;publicpartialclassusers_login:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidbtn_login_Click(objectsender,EventArgse){try{if(txt_name.Text==""||txt_pwd.Text==""){this.Page.RegisterStartupScript("ss","<script>alter('用户名和密码信息不能为空!')</script>");return;//返回操作}else{stringnum=this.txt_yzm.Text.Trim();//获取验证码if(Session["ValidNums"].ToString()==num.ToUpper()){//连接数据库SqlConnectionsqlcon=newSqlConnection(ConfigurationManager.ConnectionStrings["ftmydata"].ConnectionString);sqlcon.Open();//打开数据库stringstrselect="select*fromft_userwhereusername='"+txt_name.Text.Trim()+"'";SqlDataAdapteradsas=newSqlDataAdapter(strselect,sqlcon);DataSetaddss=newDataSet();adsas.Fill(addss);//加载数据信息if(addss.Tables[0].Rows.Count>0){stringstrselect1="select*fromft_userwhereusername='"+txt_name.Text.Trim()+"'anduserpwd='"+txt_pwd.Text.Trim()+"'";SqlCommandsqlcom1=newSqlCommand(strselect1,sqlcon);SqlDataReaderdr1=sqlcom1.ExecuteReader();if(dr1.Read())//执行读取数据信息的操作{//登录成功后,跳转到的网站页面Response.Redirect("default.aspx");}//如果密码错误,则弹出提示信息对话框else{this.Page.RegisterStartupScript("ss","<script>alter('密码错误!')</script>");return;}dr1.Close();//关闭操作}//如果用户名称不存在,则弹出提示对话框else{this.Page.RegisterStartupScript("ss","<script>alter('用户名不存在!')</script>");return;}sqlcon.Close();//关闭数据库}//如果验证码输入错误,则弹出提示对话框else{this.Page.RegisterStartupScript("ss","<script>alter('验证码输入错误!')</script>");return;}}}//如果有操作上的错误,执行CATCH语句中的程序代码catch(Exceptionex){this.Page.RegisterStartupScript("ss","<script>alter('验证码输入错误,请刷新页面!')</script>");}}}
解决方案
解决方案二:
图片验证码页面是ValidNums.aspx.cs代码如下usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Drawing;publicpartialclassValidNums:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){stringvalidateNum=CreateRandomNum(4);//成生4位随机字符串CreateImage(validateNum);//将生成的随机字符串绘成图片Session["ValidNums"]=validateNum;//保存验证码}}//生成随机字符串privatestringCreateRandomNum(intNumCount){stringallChar="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,W,X,Y,Z";string[]allCharArray=allChar.Split(',');//拆分成数组stringrandomNum="";inttemp=-1;//记录上次随机数的数值,尽量避免产生几个相同的随机数Randomrand=newRandom();for(inti=0;i<NumCount;i++){if(temp!=-1){rand=newRandom(i*temp*((int)DateTime.Now.Ticks));}intt=rand.Next(35);if(temp==t){returnCreateRandomNum(NumCount);}temp=t;randomNum+=allCharArray[t];}returnrandomNum;}//生成图片privatevoidCreateImage(stringvalidateNum){if(validateNum==null||validateNum.Trim()==String.Empty)return;//生成Bitmap图像System.Drawing.Bitmapimage=newSystem.Drawing.Bitmap(validateNum.Length*12+10,22);Graphicsg=Graphics.FromImage(image);try{//生成随机生成器Randomrandom=newRandom();//清空图片背景色g.Clear(Color.White);//画图片的背景噪音线for(inti=0;i<25;i++){intx1=random.Next(image.Width);intx2=random.Next(image.Width);inty1=random.Next(image.Height);inty2=random.Next(image.Height);g.DrawLine(newPen(Color.Silver),x1,y1,x2,y2);}Fontfont=newSystem.Drawing.Font("Arial",12,(System.Drawing.FontStyle.Bold|System.Drawing.FontStyle.Italic));System.Drawing.Drawing2D.LinearGradientBrushbrush=newSystem.Drawing.Drawing2D.LinearGradientBrush(newRectangle(0,0,image.Width,image.Height),Color.Blue,Color.DarkRed,1.2f,true);g.DrawString(validateNum,font,brush,2,2);//画图片的前景噪音点for(inti=0;i<100;i++){intx=random.Next(image.Width);inty=random.Next(image.Height);image.SetPixel(x,y,Color.FromArgb(random.Next()));}//画图片的边框线g.DrawRectangle(newPen(Color.Silver),0,0,image.Width-1,image.Height-1);System.IO.MemoryStreamms=newSystem.IO.MemoryStream();//将图像保存到指定的流image.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);Response.ClearContent();Response.ContentType="image/Gif";Response.BinaryWrite(ms.ToArray());}finally{g.Dispose();image.Dispose();}}}请大侠么帮我看看,,我一调试login.aspx页面,只要点“登录”按钮,就是不停的刷新验证码,,中间密码错误或者其他什么原因,都不显示的?到底怎么回事,请帮我看看吧注明:我在web.config中设置的数据库连接
解决方案三:
你自己断点跟踪一下,看到了哪步出的问题,这种问题最好自己先找出问题点,要不然我们看代码也是有困难的,毕竟人眼比不过调试器