1/2页12 跳转到查看:1416
发新话题 回复该主题

谁知道怎么防止下拉菜单等按钮冲刷密码的值!

谁知道怎么防止下拉菜单等按钮冲刷密码的值!

就是我在先前在文本框写上密码后 在下面用下拉菜单的时候 会把先前写的密码给冲刷掉 听朋友说可以用内嵌解决 但是那样太麻烦 有不有其他办法可以解决啊

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

密码一般会在回发之后由系统自动清空,是因为考虑到安全问题。你的下拉菜单可能开启了AutoPostBack,所以每选一项就会回发一次使密码被清空。把下拉菜单的AutoPostBack属性禁用就可以了,如果下拉菜单确实需要AutoPostBack,就只能用内嵌防止页面刷新了
路漫漫其修远兮 吾将上下而求索

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

我刚才试了一下,下拉菜单需要AutoPostBack的话除了内嵌还可以使用客户端JavaScript自己重新填充,不过这样就会把密码暴露在页面的源文件,造成安全问题。你参考一下吧。

在下拉菜单的SelectedIndexChanged事件中执行如下代码:
string script = "<script>document.getElementById('TextBox1').value = '" + TextBox1.Text + "';</script>";
Page.RegisterStartupScript("", script);

注意输出脚本要使用Page.RegisterStartupScript
使用Response.Write的话密码还是会被清空的
路漫漫其修远兮 吾将上下而求索

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

是加在下拉菜单下吗 我加了 但还是会清空也。

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

不大清楚你是什么情况,要不你把源代码贴上来吧
路漫漫其修远兮 吾将上下而求索

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

好的
这是我的源码了 不知道怎么回事 现在老出现未实例化对象

public class reg : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.CheckBox CheckBox1;
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
        protected System.Web.UI.WebControls.Image Image1;
        protected System.Web.UI.HtmlControls.HtmlTable Table4;
        protected System.Web.UI.WebControls.ImageButton ImageButton2;
        protected System.Web.UI.WebControls.TextBox usernametb;
        protected System.Web.UI.WebControls.TextBox passtb;
        protected System.Web.UI.WebControls.TextBox repasstb;
        protected System.Web.UI.WebControls.TextBox emailtb;
        protected System.Web.UI.WebControls.DropDownList ddl1;
        protected System.Web.UI.WebControls.TextBox resulttb;
        protected System.Web.UI.WebControls.DropDownList ddl2;
        protected System.Web.UI.WebControls.TextBox qmtb;
        protected System.Web.UI.WebControls.TextBox yzmtb;
        protected System.Web.UI.HtmlControls.HtmlInputFile F;
        protected System.Web.UI.WebControls.TextBox fromtb;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator2;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator3;
        protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator4;
        protected System.Web.UI.WebControls.CompareValidator CompareValidator1;
        protected System.Web.UI.WebControls.RegularExpressionValidator RegularExpressionValidator1;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.ImageButton ImageButton1;
   
        private void Page_Load(object sender, System.EventArgs e)
        {
            if(CheckBox1.Checked)
            {
                Table4.Visible=true;
            }
            else
            {
                Table4.Visible=false;
            }
            if(IsPostBack)
                return;
            Image1.ImageUrl="face/"+".gif";
            bind2();
            Table4.Visible=false;
           
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
       
        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {   
            this.ddl1.SelectedIndexChanged += new System.EventHandler(this.ddl1_SelectedIndexChanged);
            this.ddl2.SelectedIndexChanged += new System.EventHandler(this.ddl2_SelectedIndexChanged);
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
        public string filename;

        void bind2()
        {
            ddl2.DataTextField="face";
            ddl2.DataValueField="face";
           
            SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["str"]);
            string sql="select * from bbs_face";
            SqlCommand cmd=new SqlCommand(sql,conn);
            conn.Open();
            ddl2.DataSource=cmd.ExecuteReader();
            ddl2.DataBind();
            conn.Close();
        }
        private void Button1_Click(object sender, System.EventArgs e)
        {
            string mm2=Convert.ToString(passtb.Text);
            string xm=usernametb.Text;
            string mm=FormsAuthentication.HashPasswordForStoringInConfigFile(passtb.Text,"sha1");
            string yx=emailtb.Text;
            string wt=ddl1.SelectedItem.Value;
            string da=resulttb.Text;
            string xb=RadioButtonList1.SelectedItem.Value;
            string lz=fromtb.Text;
            string qm=qmtb.Text;
            string face=ddl2.SelectedItem.Value;
            string yzm=yzmtb.Text;
           
           
           

//            try
//            {   
//               
                if(mm2.Length<6)
                {
                    RegisterStartupScript("","<script>alert('密码不得少于6位数!')</script>");
                }
                else
                {
                    if(yzm==Session["CheckCode"].ToString())
                    {
                       
                        HttpPostedFile hpf=F.PostedFile;
                        int cl=hpf.FileName.Length;
                       
                        if(cl>0)
                        {
                            if(hpf.FileName=="")
                            {
           
                                RegisterStartupScript("","<script>alert('你上传的文件不能为空!')</script>");

                            }
                            else
                            {
                                string kuo=Path.GetExtension(hpf.FileName).ToLower();
                                if(kuo==".jpg" || kuo==".gif" || kuo==".bmp" )
                                {
               
                                    if(hpf.ContentLength<660000)
                                    {
                                        filename=DateTime.Now.ToString("yyyMMdd")+Path.GetFileName(hpf.FileName);
                                        string filepath=Server.MapPath("loadfile/");
                                        hpf.SaveAs(filepath+filename);

                                    }
                                    else
                                    {
                                        RegisterStartupScript("","<script>alert('请将你所上传的文件控制在600kb内!')</script>");
                                    }
                                }
                                else
                                {
                                    RegisterStartupScript("","<script>alert('上传图片的格式要为jpg,gif,bmp!')</script>");
                                }
                            }
                        }
                        else
                        {
                            filename=face;
                        }


                        conndb cdb=new conndb();
                        int i=cdb.reg(xm,mm,filename,xb,qm,yx,wt,da,lz);
                        FormsAuthentication.SetAuthCookie(i.ToString(),false);
                         string a=Request["ReturnUrl"];
                       
                        JScript.Redirect("cg.aspx?d=a");
                    }
                    else
                    {
                        Label1.Text="请正确输入验证码,如果看不清请点击刷新!";
                    }
           
                   
                }
               
//            }
//            catch
//            {
//                RegisterStartupScript("","<script>alert('该用户已经被注册,请重新输入!')</script>");
//            }
//               
           
   

   

        }

        private void ddl2_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string face=ddl2.SelectedItem.Value;
            Image1.ImageUrl="face/"+face;
        }

        private void ddl1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            string script = "<script>document.getElementById('passtb').value = '" + passtb.Text + "';</script>";
            Page.RegisterStartupScript("", script);
           
        }

   
    }
}

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

还有aspx页面的源代码呢?少了那个也没办法调试喔......
路漫漫其修远兮 吾将上下而求索

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

我上面的是aspx源码啊 你说的是html代码吗

<%@ Register TagPrefix="uc1" TagName="head" Src="head.ascx" %>
<%@ Page language="c#" Codebehind="reg.aspx.cs" AutoEventWireup="false" Inherits="BBS.reg" validateRequest=false  %>
<%@ Register TagPrefix="uc1" TagName="foot" Src="foot.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>reg</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body bottomMargin="0" leftMargin="0" topMargin="0">
        <form id="Form1" method="post" runat="server">
            <P align="center"><uc1:head id="Head1" runat="server"></uc1:head><FONT face="宋体"><BR>
                </FONT><FONT face="宋体"></FONT><FONT face="宋体">
                    <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="760" border="0">
                        <TR>
                            <TD style="WIDTH: 24px"><asp:imagebutton id="ImageButton1" runat="server" ImageUrl="pic/s.gif"></asp:imagebutton></TD>
                            <TD><FONT color="#2b76b0"><FONT size="2">口袋吧</FONT> » </FONT><FONT color="#000000"><FONT size="2">
                                        注册</FONT></FONT></TD>
                        </TR>
                    </TABLE>
                    <BR>
                    <TABLE id="Table2" cellSpacing="1" cellPadding="0" width="760" bgColor="#7692b7" border="0">
                        <TR bgColor="#ffffff">
                            <TD vAlign="middle">
                                <TABLE id="Table3" cellSpacing="1" cellPadding="0" width="99%" align="center" bgColor="#7692b7"
                                    border="0">
                                    <TR>
                                        <TD style="HEIGHT: 25px" align="center" bgColor="#e8f3fd" colSpan="2">
                                            <P align="left"><FONT color="#666666" size="2"> 基本信息(*为必填)</FONT></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD style="WIDTH: 229px">
                                            <P align="left"><FONT size="2"> 用 户 名<FONT color="#ff0000">*</FONT><BR>
                                                     不能有空格,可以是中文</FONT></P>
                                        </TD>
                                        <TD align="center">
                                            <P align="left"><asp:textbox id="usernametb" runat="server" Width="160px"></asp:textbox>
                                                <asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" Font-Size="12px" ErrorMessage="用户名不能缺省!"
                                                    ControlToValidate="usernametb"></asp:RequiredFieldValidator></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD style="WIDTH: 229px">
                                            <P align="left"><FONT size="2"> 密    码<FONT color="#ff0000">*</FONT><BR>
                                                     允许英文字母或数字,长度最小值6</FONT></P>
                                        </TD>
                                        <TD align="center">
                                            <P align="left"><asp:textbox id="passtb" runat="server" Width="160px" TextMode="Password"></asp:textbox>
                                                <asp:RequiredFieldValidator id="RequiredFieldValidator2" runat="server" Font-Size="12px" ErrorMessage="密码不能缺省!"
                                                    ControlToValidate="passtb"></asp:RequiredFieldValidator></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD style="WIDTH: 229px">
                                            <P align="left"><FONT size="2"> 重复密码</FONT><FONT color="#ff0000" size="3">*</FONT></P>
                                        </TD>
                                        <TD align="center">
                                            <P align="left"><asp:textbox id="repasstb" runat="server" Width="160px" TextMode="Password"></asp:textbox>
                                                <asp:RequiredFieldValidator id="RequiredFieldValidator3" runat="server" Font-Size="12px" ErrorMessage="重复密码不能缺省!"
                                                    ControlToValidate="repasstb">*</asp:RequiredFieldValidator>
                                                <asp:CompareValidator id="CompareValidator1" runat="server" Font-Size="12px" ForeColor="Blue" ErrorMessage="两次密码输入不一致!"
                                                    ControlToValidate="repasstb" ControlToCompare="passtb"></asp:CompareValidator></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD style="WIDTH: 229px">
                                            <P align="left"><FONT size="2">  E_mial</FONT><FONT color="#ff0000" size="3">*</FONT></P>
                                        </TD>
                                        <TD align="center">
                                            <P align="left"><asp:textbox id="emailtb" runat="server" Width="160px" Font-Size="12px"></asp:textbox>
                                                <asp:RequiredFieldValidator id="RequiredFieldValidator4" runat="server" Font-Size="12px" ErrorMessage="邮箱不能缺省!"
                                                    ControlToValidate="emailtb">*</asp:RequiredFieldValidator>
                                                <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" Font-Size="12px" ErrorMessage="请输入正确的邮箱格式!"
                                                    ControlToValidate="emailtb" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#e8f3fd">
                                        <TD colSpan="2">
                                            <P align="left"><FONT size="2"> 高级选项</FONT>                    
                                                <asp:checkbox id="CheckBox1" runat="server" Font-Size="12px" Text="显示高级用户选项" AutoPostBack="True"></asp:checkbox></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD colSpan="2">
                                            <TABLE id="Table4" cellSpacing="1" cellPadding="0" width="100%" border="0" bgColor="#7692b7"
                                                runat="server">
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px; HEIGHT: 17px"><FONT size="2">  安全保护(用于忘记了密码找回!)</FONT></TD>
                                                    <TD style="HEIGHT: 17px">
                                                        <asp:DropDownList id="ddl1" runat="server">
                                                            <asp:ListItem Selected="True">无安全提问</asp:ListItem>
                                                            <asp:ListItem Value="你上小学的名字">你上小学的名字</asp:ListItem>
                                                            <asp:ListItem Value="你母亲的名字">你母亲的名字</asp:ListItem>
                                                            <asp:ListItem Value="你最喜欢的动物">你最喜欢的动物</asp:ListItem>
                                                            <asp:ListItem Value="你最喜欢听的歌">你最喜欢听的歌</asp:ListItem>
                                                            <asp:ListItem Value="你最爱的人是谁">你最爱的人是谁</asp:ListItem>
                                                            <asp:ListItem Value="你的学生证号是多少">你的学生证号是多少</asp:ListItem>
                                                        </asp:DropDownList></TD>
                                                </TR>
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px">
                                                        <P align="center"> <FONT size="2">回    答</FONT></P>
                                                    </TD>
                                                    <TD>
                                                        <asp:TextBox id="resulttb" runat="server"></asp:TextBox></TD>
                                                </TR>
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px; HEIGHT: 47px">
                                                        <P align="center"> <FONT size="2">性    别</FONT></P>
                                                    </TD>
                                                    <TD style="HEIGHT: 47px">
                                                        <asp:RadioButtonList id="RadioButtonList1" runat="server" Font-Size="12px" RepeatDirection="Horizontal"
                                                            Width="69px">
                                                            <asp:ListItem Value="<img src=pic/male.gif>" Selected="True"><img src=pic/male.gif></asp:ListItem>
                                                            <asp:ListItem Value="<img src=pic/female.gif>"><img src=pic/female.gif></asp:ListItem>
                                                        </asp:RadioButtonList></TD>
                                                </TR>
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px">
                                                        <P align="center"> <FONT size="2">来    自</FONT></P>
                                                    </TD>
                                                    <TD>
                                                        <asp:TextBox id="fromtb" runat="server"></asp:TextBox></TD>
                                                </TR>
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px; HEIGHT: 5px">
                                                        <P align="center"> <FONT size="2">头像选择</FONT></P>
                                                    </TD>
                                                    <TD style="HEIGHT: 5px" vAlign="top">
                                                        <asp:DropDownList id="ddl2" runat="server" Width="95px" AutoPostBack="True"></asp:DropDownList>
                                                        <asp:Image id="Image1" runat="server" Height="50px" Width="50px"></asp:Image></TD>
                                                </TR>
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px">
                                                        <P align="center"> <FONT size="2">自定义头像</FONT></P>
                                                    </TD>
                                                    <TD><INPUT type="file" id="F" name="File1" runat="server"></TD>
                                                </TR>
                                                <TR bgColor="#ffffff">
                                                    <TD style="WIDTH: 206px">
                                                        <P align="center"> <FONT size="2">个性签名</FONT></P>
                                                    </TD>
                                                    <TD>
                                                        <asp:TextBox id="qmtb" runat="server" Width="401px" TextMode="MultiLine"></asp:TextBox></TD>
                                                </TR>
                                            </TABLE>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD colSpan="2">
                                            <P align="left"><FONT size="2"> 认 证 码 <FONT color="#ff0000">*</FONT></FONT>
                                                <asp:TextBox id="yzmtb" runat="server"></asp:TextBox>
                                                <asp:ImageButton id="ImageButton2" runat="server" ImageUrl="Code.aspx"></asp:ImageButton>
                                                <asp:Label id="Label1" runat="server" Font-Size="12px" ForeColor="Red"></asp:Label></P>
                                        </TD>
                                    </TR>
                                    <TR bgColor="#ffffff">
                                        <TD align="center" colSpan="2">
                                            <asp:Button id="Button1" runat="server" Font-Size="12px" Height="20px" Width="49px" Text="提 交"
                                                ForeColor="#005368" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" BackColor="#C9E8EC"></asp:Button></TD>
                                    </TR>
                                </TABLE>
                            </TD>
                        </TR>
                    </TABLE>
                    <BR>
                    <uc1:foot id="Foot1" runat="server"></uc1:foot></FONT></P>
        </form>
    </body>
</HTML>

TOP

 

回复: 谁知道怎么防止下拉菜单等按钮冲刷密码的值!

把整个reg传上来行吗
附件:您所在的用户组无法下载或查看附件

TOP

 

回复:谁知道怎么防止下拉菜单等按钮冲刷密码的值!

抱歉,今天比较忙,没办法帮你详细调试了,我大概看了一下代码,问题有可能出在这里:
<asp:DropDownList id="ddl2" runat="server" Width="95px" AutoPostBack="True"></asp:DropDownList>

这个下拉菜单开启了自动回发,所以应该在相应的事件中输出填充脚本:
private void ddl2_SelectedIndexChanged(object sender, System.EventArgs e)
{
            //其它代码......
            string script = "<script>document.getElementById('passtb').value = '" + passtb.Text + "';</script>";
            Page.RegisterStartupScript("", script);
}

下拉菜单ddl1没有开启自动回发,不需要输出填充脚本

另外,如果你想每次回发都自动填充密码,而不想每个控件事件都加入上述代码的话,可以在Page_Load事件中:
if(IsPostBack)
{
            string script = "<script>document.getElementById('passtb').value = '" + passtb.Text + "';</script>";
            Page.RegisterStartupScript("", script);
}
最后编辑suresy 最后编辑于 2008-07-24 23:09:12
路漫漫其修远兮 吾将上下而求索

TOP

 
1/2页12 跳转到
发表新主题 回复该主题