验证控件内容是否为空写法一(后台代码)

 

 

//验证提交控件内容是否为空

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        #tbList
        {
            border-top: 1px solid #0088F7;
            border-left: 1px solid #0088F7;
            width: 930px;
        }
       
        #tbList td, th
        {
            border-bottom: 1px solid #0088F7;
            border-right: 1px solid #0088F7;
            font-size: 16px;
        }
       
        .textbox
        {
            border-left: solid 0 #ffffff;
            border-bottom: solid 1px #716E6F;
            border-right: solid 0 #ffffff;
            border-top: solid 0 #ffffff;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <div>
                <br />
                <br />
                <table id="tbList">
                    <tr>
                        <td align="right">
                            姓名
                        </td>
                        <td>
                            <asp:TextBox ID="txtName" runat="server"></asp:TextBox>

                            <asp:Label ID="lbName" runat="server" Text="名字不能为空" ForeColor="Red" Visible="false"></asp:Label>
                       
                        </td>
                        <td align="right">
                            年龄
                        </td>
                        <td>
                            <asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
                            <asp:Label ID="lbAge" runat="server" Text="年龄不能为空" ForeColor="Red" Visible="false"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            民族
                        </td>
                        <td>
                            <asp:TextBox ID="txtPeople" runat="server"></asp:TextBox>
                            <asp:Label ID="lbPeople" runat="server" Text="民族不能为空" ForeColor="Red" Visible="false"></asp:Label>
                        </td>
                        <td align="right">
                            省份
                        </td>
                        <td>
                            <asp:DropDownList ID="ddlProvince" runat="server">
                                <asp:ListItem>辽宁</asp:ListItem>
                                <asp:ListItem>北京</asp:ListItem>
                                <asp:ListItem>上海</asp:ListItem>
                                <asp:ListItem>广州</asp:ListItem>
                                <asp:ListItem>美国</asp:ListItem>
                                <asp:ListItem>英国</asp:ListItem>
                            </asp:DropDownList>
                            <asp:Label ID="lbProvince" runat="server" Text="省份选择不正确" ForeColor="Red" Visible="false"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4">
                            <center>
                                <br />
                                <asp:Button ID="btnSubmit" runat="server" Text="提交数据" OnClick="btnSubmit_Click" />
                                <br />
                            </center>
                        </td>
                    </tr>
                </table>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
    </form>
</body>
</html> 



 //后台CS代码

 //提交按钮的功能
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;//姓名
            string age = txtAge.Text;//年龄
            string people = txtPeople.Text;//民族
            string strDDLProvince = ddlProvince.SelectedItem.Text;//得到选中的省份文本
            if (string.IsNullOrEmpty(name))//如果姓名文本不为空的话
            {
                lbName.Visible = true;//设置姓名错误提示标签为True
                return;
            }
            else
            {
                lbName.Visible = false;//否则的话把姓名错误提示标签为False
            }
            if (string.IsNullOrEmpty(age))//如果姓名不为空的话
            {
                lbAge.Visible = true;//设置年龄错误提示标签为True
                return;
            }
            else
            {
                lbAge.Visible = false;//设置年龄错误提示标签为false
            }

 


            if (string.IsNullOrEmpty(people))//如果民族不为空的话
            {
                lbPeople.Visible = true;//设置年龄错误提示标签为True
                return;
            }
            else
            {
                lbPeople.Visible = false;//设置年龄错误提示标签为False
            }
            if (strDDLProvince == "辽宁")
            {
                lbProvince.Visible = true;//设置省份错误提示标签为True
                return;
            }
            else
            {
                lbProvince.Visible = false;//设置省份错误提示标签为False
            }
            Response.Write("提交成功");
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值