• RequiredFieldValidator的使用


    特別說明:1.一個Button要對頁面的多個控件進行驗證,則需要設置button和其它受控控件的ValidationGroup屬性

    aspx頁面實例:

      <tr class="h">
                                <td class="tdr">
                                    起始日:
                                </td>
                                <td class="tdl">
                                    <asp:TextBox ID="tb_fromdate" runat="server" CssClass="AlignLeftBc" ValidationGroup="SaveInsert"></asp:TextBox>
                                    <ccl:CalendarExtender ID="tb_fromdate_CalendarExtender" runat="server" Format="yyyy/MM/dd HH:mm:ss"
                                        TargetControlID="tb_fromdate">
                                    </ccl:CalendarExtender>
                                    <asp:RequiredFieldValidator ID="Rfvfrom" runat="server" ControlToValidate="tb_fromdate"
                                        ErrorMessage="不能為空!" ValidationGroup="SaveInsert">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr class="h">
                                <td class="tdr">
                                    截止日:
                                </td>
                                <td class="tdl">
                                    <asp:TextBox ID="tb_todate" runat="server" CssClass="AlignLeftBc" ValidationGroup="SaveInsert"></asp:TextBox>
                                    <ccl:CalendarExtender ID="tb_todate_CalendarExtender" runat="server" Format="yyyy/MM/dd HH:mm:ss"
                                        TargetControlID="tb_todate">
                                    </ccl:CalendarExtender>
                                    <asp:RequiredFieldValidator ID="Rfvto" runat="server" ControlToValidate="tb_todate"
                                        ErrorMessage="不能為空!" ValidationGroup="SaveInsert">*</asp:RequiredFieldValidator>
                                </td>
                            </tr>
                            <tr style="height: 30px">
                                <td align="center" colspan="2" valign="middle">
                                    <asp:LinkButton ID="lbt_Sure" runat="server" Font-Underline="false" OnClick="lbt_Sure_Click"
                                        ValidationGroup="SaveInsert" OnClientClick="return ConfirmMe()">
                <img alt="保存" src="../../../images/Save01.gif" style=" border:none" />
                                    </asp:LinkButton>
                                    &nbsp;&nbsp;
                                    <asp:LinkButton ID="lbt_close" runat="server" Font-Underline="false" OnClick="lbt_close_Click">
                <img alt="關閉" src="../../../images/Cancel01e.gif" style=" border:None" />
                                    </asp:LinkButton>
                                </td>
                            </tr>

    後台代碼:

     protected void lbt_Sure_Click( object sender, EventArgs e)
        {
            if (!!Page.IsValid)
            {

     //通過Page.IsValid來取得頁面是否通過驗證

    }

    }

    多一分冷靜,少一分浮躁
  • 相关阅读:
    抽象类 C#
    多态
    父类与子类的转换as,is
    try catch finally 与continue的使用
    封装、多态、继承
    new关键字 、this关键字、base关键字
    进程的使用
    Spring IOC
    使用annotation配置hibernate(3):一对一关系配置
    使用annotation配置hibernate(3):多对多关系配置
  • 原文地址:https://www.cnblogs.com/AnnyGird-LiMing/p/4955652.html
Copyright © 2020-2023  润新知