009. 简单的注册页和重置

前端html代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div >
        <table style="width:500px ; text-align:left; margin:auto;">
            <tr>
                <td  style="text-align:right">
                    <asp:Label ID="Label1" runat="server" Text="用户名:"></asp:Label>
                    </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td  style="text-align:right">
                    <asp:Label ID="Label2" runat="server" Text="密码:" ></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtpwd" runat="server" TextMode="Password"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td   style="text-align:right; left:50px;">
                    <asp:Label ID="Label3" runat="server" Text="性别:"></asp:Label>
                </td>
                <td>
                    <asp:RadioButtonList ID="rblsex" RepeatDirection="Horizontal" 
                        runat="server" 
                        Width="173px">
                        <asp:ListItem Selected="True"></asp:ListItem>
                        <asp:ListItem></asp:ListItem>
                    </asp:RadioButtonList>
                </td>
            </tr>
               <tr>
                <td   style="text-align:right">
                    <asp:Label ID="Label4" runat="server" Text="住址:"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
                </td>
            </tr>
                <tr>
                <td colspan="2" style="text-align:center;">
                    <asp:Button ID="btnreg" runat="server" Text="注册" onclick="btnreg_Click" />   &nbsp; &nbsp;
                    <asp:Button ID="btncancel" runat="server" Text="重置" onclick="btncancel_Click" />
                </td> 
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

后台CS代码(含如何向客户端发送js代码)

    protected void btnreg_Click(object sender, EventArgs e)
    {
        if (txtname.Text.Length == 0)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请输入用户名')", true);
        }
        else
        {
            if (txtpwd.Text.Trim().Length == 0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请输入密码!')", true);
            }
            else
            {
                if (txtaddress.Text.Trim().Length == 0)
                {
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请输入地址!')", true);
                }
                else
                {
                    this.gender = rblsex.SelectedItem.Text;
                }
            }
        }
        /*
        if (rblsex.SelectedIndex == 0)
        {
            // Page.ClientScript.RegisterClientScriptBlock():添加脚本块, 页面加载前执行
            //RegisterStartupScript 注册脚本并在页面加载完时执行。
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('男')", true);
            //Page.ClientScript.RegisterClientScriptInclude
            //把JavaScript放置在.js文件中,使用RegisterClientScriptInclude方法可以注册.js文件中的 JavaScript
            //string myScript = “myJavaScriptCode.js”
            //Page.ClientScript.RegisterClientScriptInclude(“myKey”, myScript);
            //这将在ASP.NET页面产生如下结构:
            //<script src=”myJavaScriptCode.js” type=”text/javascript”></script>
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('女')", true);
        }
         */
    }
    protected void btncancel_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Page.Controls.Count; i++)
        {
            foreach (Control item in Page.Controls[i].Controls)
            {
                if (item is TextBox)
                {
                  ((TextBox)item).Text=""; //对象转换
                  
                }
            }
        }
    }

 

posted on 2016-11-10 15:20  印子  阅读(212)  评论(0编辑  收藏  举报

导航