Exception handling in WebForms

Posted by user999379 on Stack Overflow See other posts from Stack Overflow or by user999379
Published on 2011-11-30T09:34:02Z Indexed on 2011/11/30 9:50 UTC
Read the original article Hit count: 198

Filed under:
|
|
|

I have a webform with a formview

<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>

In my property Postcode I check the value like this:

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;

                }
            }
        }

How can I handle the exception I threw? If there is an exception I want a label to appear with the following exception?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET