WebForms中的异常处理

本文关键字:异常处理 WebForms | 更新日期: 2023-09-27 18:16:25

我有一个带有formview的webform

<asp:FormView ID="formViewBrouwers" runat="server" AllowPaging="True" 
        DataKeyNames="BrouwerNr" DataSourceID="brouwerDataSource" 
        onitemupdated="formViewBrouwers_ItemUpdated" 
        onitemupdating="formViewBrouwers_ItemUpdating" 
        oniteminserted="formViewBrouwers_ItemInserted" 
        oniteminserting="formViewBrouwers_ItemInserting">
        <EditItemTemplate>
            BrouwerNr:
            <asp:Label ID="BrouwerNrLabel1" runat="server" 
                Text='<%# Eval("BrouwerNr") %>' />
            <br />
            BrNaam:
            <asp:TextBox ID="BrNaamTextBox" runat="server" Text='<%# Bind("BrNaam") %>' />
            <br />
            Adres:
            <asp:TextBox ID="AdresTextBox" runat="server" Text='<%# Bind("Adres") %>' />
            <br />
            Postcode:
            <asp:TextBox ID="PostcodeTextBox" runat="server" 
                Text='<%# Bind("Postcode") %>' />
            <br />
            Gemeente:
            <asp:TextBox ID="GemeenteTextBox" runat="server" 
                Text='<%# Bind("Gemeente") %>' />
            <br />
            Omzet:
            <asp:TextBox ID="OmzetTextBox" runat="server" Text='<%# Bind("Omzet") %>' />
            <br />
            Status:
            <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
            <br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>
        <InsertItemTemplate>
            BrNaam:
            <asp:TextBox ID="BrNaamTextBox" runat="server" Text='<%# Bind("BrNaam") %>' />
            <br />
            Adres:
            <asp:TextBox ID="AdresTextBox" runat="server" Text='<%# Bind("Adres") %>' />
            <br />
            Postcode:
            <asp:TextBox ID="PostcodeTextBox" runat="server" 
                Text='<%# Bind("Postcode") %>' />
            <br />
            Gemeente:
            <asp:TextBox ID="GemeenteTextBox" runat="server" 
                Text='<%# Bind("Gemeente") %>' />
            <br />
            Omzet:
            <asp:TextBox ID="OmzetTextBox" runat="server" Text='<%# Bind("Omzet") %>' />
            <br />
            Status:
            <asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
            <br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Insert" />
            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>
        <ItemTemplate>
            BrouwerNr:
            <asp:Label ID="BrouwerNrLabel" runat="server" Text='<%# Eval("BrouwerNr") %>' />
            <br />
            BrNaam:
            <asp:Label ID="BrNaamLabel" runat="server" Text='<%# Bind("BrNaam") %>' />
            <br />
            Adres:
            <asp:Label ID="AdresLabel" runat="server" Text='<%# Bind("Adres") %>' />
            <br />
            Postcode:
            <asp:Label ID="PostcodeLabel" runat="server" Text='<%# Bind("Postcode") %>' />
            <br />
            Gemeente:
            <asp:Label ID="GemeenteLabel" runat="server" Text='<%# Bind("Gemeente") %>' />
            <br />
            Omzet:
            <asp:Label ID="OmzetLabel" runat="server" Text='<%# Bind("Omzet") %>' />
            <br />
            Status:
            <asp:Label ID="StatusLabel" runat="server" Text='<%# Bind("Status") %>' />
            <br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                CommandName="Edit" Text="Edit" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                CommandName="Delete" Text="Delete" />
            &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />
        </ItemTemplate>
        <PagerSettings Mode="NextPreviousFirstLast" />
    </asp:FormView>

在我的属性Postcode中,我这样检查值:

private Int16 postcodeValue;
public Int16 Postcode
        {
            get
            {
                return postcodeValue;
            }
            set
            {
                if (value < 1000 || value > 9999)
                {
                    throw new Exception("Postcode moet tussen 1000 en 9999 liggen");     
                }
                else
                {
                    postcodeValue = value;
                }
            }
        }

如何处理我抛出的异常?如果出现异常,我希望标签显示以下异常?

WebForms中的异常处理

您可以将InsertItemTemplate和EditItemTemplate中的PostCode文本框与RangeValidator关联,并在条件失败时在旁边添加一个标签,显示错误消息

也许你应该使用派生异常类型,如ArgumentException或自定义类,如PostCodeException。通过这种方式,您可以捕获正确的异常类型并做出相应的反应。

本文可能有用:http://msdn.microsoft.com/en-us/library/ms173160(v=VS.100).aspx

您可以使用RegularExpressionValidator或RangeValidator,它们将在将值提交给服务器之前在客户端验证值。

此'^[1-9]{1}[0-9]{3}?[a - z]{2}$'将是检查荷兰邮政编码的有效正则表达式。

或者,您可以显示通常隐藏在标记中的标签,而不是在属性设置器中抛出异常。

如果你真的想抛出一个异常,看看Page_Error事件。您可以使用它来捕获页面中的所有异常,并在适当的情况下处理它们。