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來取得頁面是否通過驗證

}

}

posted on 2015-11-11 11:53  明俠遠飄  阅读(408)  评论(0编辑  收藏  举报

导航