I'm having some problem with validation control in formview. I could add a validation control inside a formview control it was working fine until I tried to group them using validationgroups. Also if I dont enter a value the program still allow to add it in the database. Could someone help me point out the problem or error in my program? Here's the Insert statement I've made.
<InsertItemTemplate>
Book ID:
<asp:TextBox ID="bookidTextBox" runat="server" Text='<%# Bind("bookid") %>' />
<asp:RequiredFieldValidator ID="RequesFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="bookidTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" Display="Dynamic" ControlToValidate="bookidTextBox" ValidationExpression="^([\S\s]{13,13})$" ErrorMessage="Value must be at least 13 characters long">
</asp:RegularExpressionValidator>
<br />
Title:
<asp:TextBox ID="booktitleTextBox" runat="server"
Text='<%# Bind("booktitle") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="* Required" ControlToValidate="booktitleTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Author's lastname:
<asp:TextBox ID="lastnameTextBox" runat="server"
Text='<%# Bind("lastname") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="* Required" ControlToValidate="lastnameTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Author's firstname:
<asp:TextBox ID="firstnameTextBox" runat="server"
Text='<%# Bind("firstname") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="* Required" ControlToValidate="firstnameTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Description:
<asp:TextBox ID="descriptionTextBox" runat="server"
Text='<%# Bind("description") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="* Required" ControlToValidate="descriptionTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Category:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="categoryDataSource" DataTextField="name"
DataValueField="categoryid" SelectedValue='<%# Bind("categoryid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="categoryDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]">
</asp:SqlDataSource>
<br />
Date added:
<asp:TextBox ID="dateaddedTextBox" runat="server"
Text='<%# Bind("dateadded") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="* Required" ControlToValidate="dateaddedTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
Status:
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="statusDataSource" DataTextField="statusname"
DataValueField="statusid" SelectedValue='<%# Bind("statusid", "{0}") %>'>
</asp:DropDownList>
<asp:SqlDataSource ID="statusDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>"
SelectCommand="SELECT [statusid], [statusname] FROM [BookStatus]">
</asp:SqlDataSource>
<br />
Quantity:
<asp:TextBox ID="quantityTextBox" runat="server"
Text='<%# Bind("quantity") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" ErrorMessage="* Required" ControlToValidate="quantityTextBox" ValidationGroup="InsertBook">
</asp:RequiredFieldValidator>
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="Add" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
Any help would be much appreciated ;)
Thanks in advance.
You have to set ValidationGroup="InsertBook" as you have set Validation group to your validation.
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Add" ValidationGroup="InsertBook" />
Related
im working on a project and im combining 3 tables the tables are already associated in the dbml but my problem is when i use the data list it diaplays my table names not the correct fields this is a college assignment but i have tried everything i know to do.
I've tried to combine them seperatly in sql manager in vs but im just not understanding why it isn't showing the correct thing
<form id="form1" runat="server">
<div>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="assign_6_final.DataClasses1DataContext" EntityTypeName="" Select="new (CustomerID, Name)" TableName="Customers">
</asp:LinqDataSource>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="LinqDataSource1" DataTextField="Name" DataValueField="CustomerID" OnSelectedIndexChanged="Page_Load">
</asp:DropDownList>
<asp:DataList ID="DataList1" runat="server" DataSourceID="LinqDataSource2" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" OnDataBinding="DataList1_SelectedIndexChanged">
<ItemTemplate>
IncidentID:
<asp:Label ID="IncidentIDLabel" runat="server" Text='<%# Eval("IncidentID") %>' />
<br />
DateOpened:
<asp:Label ID="DateOpenedLabel" runat="server" Text='<%# Eval("DateOpened") %>' />
<br />
DateClosed:
<asp:Label ID="DateClosedLabel" runat="server" Text='<%# Eval("DateClosed") %>' />
<br />
Title:
<asp:Label ID="TitleLabel" runat="server" Text='<%# Eval("Title") %>' />
<br />
Description:
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
CustomerID:
<asp:Label ID="CustomerIDLabel" runat="server" Text='<%# Eval("CustomerID") %>' />
<br />
Technician:
<asp:Label ID="TechnicianLabel" runat="server" Text='<%# Eval("Technician") %>' />
<br />
Product:
<asp:Label ID="ProductLabel" runat="server" Text='<%# Eval("Product") %>' />
<br />
<br />
<br />
</ItemTemplate>
</asp:DataList>
</div>
<asp:LinqDataSource ID="LinqDataSource2" runat="server" ContextTypeName="assign_6_final.DataClasses1DataContext" EntityTypeName="" Select="new (IncidentID, DateOpened, DateClosed, Title, Description, CustomerID, Technician, Product)" TableName="Incidents" Where="CustomerID = #CustomerID">
<WhereParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
<asp:EntityDataSource ID="EntityDataSource1" runat="server">
</asp:EntityDataSource>
</form>
</body>
the outcome i expected was that the last 2 items the product and technician would output correctly but they haven't and im kind of lost my teacher said this is a common problem in asp but ive never seen it
Try using the following and so forth:
<%# DataBinder.Eval(Container.DataItem,"CustomerID") %>
when i put RequiredFieldValidator in asp.net page then it disturbs page layout. It works in all places but in specific places it disturbs page layout.
Before Putting Validator:
<asp:Panel ID="pnlSearchComplaint" Visible="false" BackColor="#dbdbdb"
runat="server" Width="400px">
<br />
<label>Search By Subject</label>
<asp:TextBox ID="txtSearchCompBySubject" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="txtSearchCompBySubject" ErrorMessage="Enter Subject"
ForeColor="Red" ValidationGroup="GroupSearchCompSubject">Enter Subject
</asp:RequiredFieldValidator>
<br/>
<asp:Button ID="btnSearchBySubject" ValidationGroup="GroupSearchCompSubject"
CssClass="btn btn-success" runat="server" Text="Search"
ClientIDMode="Static" OnClick="btnSearchBySubject_Click" />
<asp:GridView ID="gridViewComplaintsBySubject" AllowPaging="true" OnPageIndexChanging="gridViewComplaintsBySubject_PageIndexChanging" AutoGenerateSelectButton="true" runat="server" CssClass="mGrid" OnSelectedIndexChanged="gridViewComplaintsBySubject_SelectedIndexChanged" Width="408px">
<EmptyDataRowStyle BorderStyle="None" ForeColor="Red" BorderWidth="0px" />
<EmptyDataTemplate>
No Data Found for this Input. Try Again.
</EmptyDataTemplate>
<SelectedRowStyle CssClass="selected-row" ForeColor="white" />
</asp:GridView>
<br />
<label>Search By Date</label>
<asp:Label ID="lblDateFrom" runat="server" Text="From"></asp:Label>
<asp:TextBox ID="txtFromDate" runat="server" ></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtenderFromDate" TargetControlID="txtFromDate" runat="server">
</asp:CalendarExtender>
<asp:Label ID="lblDateTo" runat="server" Text="To"></asp:Label>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtenderToDate" TargetControlID="txtToDate" runat="server">
</asp:CalendarExtender>
<asp:Button ID="btnSearchByDate" CssClass="btn btn-success" runat="server" Text="Search"
ClientIDMode="Static" OnClick="btnSearchByDate_Click" />
<asp:GridView ID="gridViewComplaintsByDate" AllowPaging="true" AutoGenerateSelectButton="true" runat="server" CssClass="mGrid" Width="408px" OnPageIndexChanging="gridViewComplaintsByDate_PageIndexChanging" OnSelectedIndexChanged="gridViewComplaintsByDate_SelectedIndexChanged">
<EmptyDataRowStyle BorderStyle="None" ForeColor="Red" BorderWidth="0px" />
<EmptyDataTemplate>
No Data Found for this Input. Try Again.
</EmptyDataTemplate>
<SelectedRowStyle CssClass="selected-row" ForeColor="white" />
</asp:GridView>
<br />
</asp:Panel>
After Putting Validator:
Code:
<asp:Panel ID="pnlSearchComplaint" Visible="false" BackColor="#dbdbdb"
runat="server" Width="400px">
<br />
<label>Search By Subject</label>
<asp:TextBox ID="txtSearchCompBySubject" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="txtSearchCompBySubject" ErrorMessage="Enter Subject"
ForeColor="Red" ValidationGroup="GroupSearchCompSubject">Enter Subject
</asp:RequiredFieldValidator>
<br/>
<asp:Button ID="btnSearchBySubject" ValidationGroup="GroupSearchCompSubject"
CssClass="btn btn-success" runat="server" Text="Search"
ClientIDMode="Static" OnClick="btnSearchBySubject_Click" />
<asp:GridView ID="gridViewComplaintsBySubject" AllowPaging="true" OnPageIndexChanging="gridViewComplaintsBySubject_PageIndexChanging" AutoGenerateSelectButton="true" runat="server" CssClass="mGrid" OnSelectedIndexChanged="gridViewComplaintsBySubject_SelectedIndexChanged" Width="408px">
<EmptyDataRowStyle BorderStyle="None" ForeColor="Red" BorderWidth="0px" />
<EmptyDataTemplate>
No Data Found for this Input. Try Again.
</EmptyDataTemplate>
<SelectedRowStyle CssClass="selected-row" ForeColor="white" />
</asp:GridView>
<br />
<label>Search By Date</label>
<asp:Label ID="lblDateFrom" runat="server" Text="From"></asp:Label>
<asp:TextBox ID="txtFromDate" runat="server" ></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server"
ControlToValidate="txtFromDate" ErrorMessage="Enter Subject"
ForeColor="Red" ValidationGroup="GroupSearchCompSubject">Enter Subject
</asp:RequiredFieldValidator>
<asp:CalendarExtender ID="CalendarExtenderFromDate" TargetControlID="txtFromDate" runat="server">
</asp:CalendarExtender>
<asp:Label ID="lblDateTo" runat="server" Text="To"></asp:Label>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
<asp:CalendarExtender ID="CalendarExtenderToDate" TargetControlID="txtToDate" runat="server">
</asp:CalendarExtender>
<asp:Button ID="btnSearchByDate" CssClass="btn btn-success" runat="server" Text="Search"
ClientIDMode="Static" OnClick="btnSearchByDate_Click" />
<asp:GridView ID="gridViewComplaintsByDate" AllowPaging="true" AutoGenerateSelectButton="true" runat="server" CssClass="mGrid" Width="408px" OnPageIndexChanging="gridViewComplaintsByDate_PageIndexChanging" OnSelectedIndexChanged="gridViewComplaintsByDate_SelectedIndexChanged">
<EmptyDataRowStyle BorderStyle="None" ForeColor="Red" BorderWidth="0px" />
<EmptyDataTemplate>
No Data Found for this Input. Try Again.
</EmptyDataTemplate>
<SelectedRowStyle CssClass="selected-row" ForeColor="white" />
</asp:GridView>
<br />
</asp:Panel>
Please set the "Display='dynamic'" property in your Validation control.
<asp:RequiredFieldValidator Display="Dynamic"></asp:RequiredFieldValidator>
If you do not want disturb your UI when it shows the message then
<asp:RequiredFieldValidator Display="Dynamic" ErrorMessage="*" ToolTip="Set your message here"></asp:RequiredFieldValidator>
Edit 1:
<label>
Search By Date</label>
<asp:Label ID="lblDateFrom" runat="server" Text="From"></asp:Label>
<asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>
<asp:Label ID="lblDateTo" runat="server" Text="To"></asp:Label>
<asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>
<div>
<asp:RequiredFieldValidator Display="Dynamic" ID="RequiredFieldValidator9" runat="server" ControlToValidate="txtFromDate"
ErrorMessage="Enter Subject" ForeColor="Red" ValidationGroup="GroupSearchCompSubject">Enter Subject
</asp:RequiredFieldValidator>
</div>
<asp:Button ID="btnSearchByDate" CssClass="btn btn-success" runat="server" Text="Search"
ClientIDMode="Static" />
I have 2 radio button (radiobtn1 and radiobtn2) in one group and 4 text box which names are a, b, c, d. And end of the page there is standard button. I add asp:requiredFieldValidators to all of textboxes. So when I clicked my button asp:validationsummary works for all of textboxes.
I want to do that if radiobtn1 was clicked, validationsummary should work for textbox a and b, if radiobtn2 was clicked, validationsummary should work for should textbox c and d.
how can I do that?
thx.
this is working as of your requirement..
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox2" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 1">*</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="TextBox3" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
<br />
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="TextBox4" ErrorMessage="*" Font-Size="Medium" ForeColor="Red" ValidationGroup="group 2">*</asp:RequiredFieldValidator>
<br />
<br />
<br />
<br />
<br />
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" Text="valid first 2" CausesValidation="True" GroupName="g" ValidationGroup="group 1"/>
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" Text="valid last 2" CausesValidation="True" GroupName="g" ValidationGroup="group 2" />
<br />
<br />
</div>
</form>
I am trying to bind data from DropDownList in ListView in EditItemTemplate but it didn't work.
Code:
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("type")%>'>
<asp:ListItem Text="Fatwa" Value="1"></asp:ListItem>
<asp:ListItem Text="Interviewer" Value="2"></asp:ListItem>
<asp:ListItem Text="Programs" Value="3"></asp:ListItem>
</asp:DropDownList>
Here is sample of concept you can use:
<EditItemTemplate>
<asp:Label ID="DataLabel" runat="server" Text="<% $resources:AppResource,UserData %>"
CssClass="OneColumnTitleLabel"></asp:Label>
<br />
<br />
<asp:Label ID="EmailLabel" CssClass="RightColumnLabel" runat="server" Text='<%# Resources.AppResource.Email + ":" %>'></asp:Label><br />
<asp:TextBox ID="EmailTextBox" runat="server" CssClass="RightColumnText" Text='<%# Eval("Email") %>'
ReadOnly="true"></asp:TextBox>
<br />
<asp:Label ID="LanguageLabel" CssClass="RightColumnLabel" runat="server" Text='<%# Resources.AppResource.Language + ":" %>'></asp:Label><br />
<asp:DropDownList ID="LanguageDropDown" runat="server" CssClass="RightColumnDropDown"
DataSourceID="LanguageDataSource" DataTextField="Name" DataValueField="Id" SelectedValue='<%# Bind("LanguageId") %>'
AppendDataBoundItems="True">
<asp:ListItem Value="" Text="" Selected="True">
</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="Label2" CssClass="RightColumnLabel" runat="server" Text='<%# Resources.AppResource.Company + ":" %>'></asp:Label><br />
<asp:TextBox ID="CompanyTextBox" runat="server" CssClass="RightColumnText" Text='<%# Bind("Company") %>'></asp:TextBox>
<br />
<asp:Label ID="PhoneLabel" CssClass="RightColumnLabel" runat="server" Text='<%# Resources.AppResource.Phone + ":" %>'></asp:Label><br />
<asp:TextBox ID="PhoneTextBox" runat="server" CssClass="RightColumnText" Text='<%# Bind("Phone") %>'></asp:TextBox>
<br />
<asp:Button ID="UpdateUserData" runat="server" Text='<%# Resources.AppResource.Save %>'
CssClass="Button" CommandName="Update" CausesValidation="true" />
</EditItemTemplate>
Take notice on LanguageDropDown (LanguageId is binded back to listview). This dropdown is binded to datasource like this one:
<asp:ObjectDataSource ID="LanguageDataSource" runat="server" SelectMethod="GetLanguages"
TypeName="GP.Solutions.Services.LanguageManager"></asp:ObjectDataSource>
This is how you can bind dropdown inside listview.
My code working well but I forgot to type DataKeyNames in listview :D
<asp:ListView ID="TodayOrdersList" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ID">
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" />
<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" />
<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" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<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?
You can associate the PostCode textbox in InsertItemTemplate and EditItemTemplate with a RangeValidator and have a label beside that shows the error message when the condition fails.
Maybe you should use a derived exception type, like ArgumentException or a custom class, something like PostCodeException.
This way you can catch the correct exception type and react accordingly.
This article may be useful: http://msdn.microsoft.com/en-us/library/ms173160(v=VS.100).aspx
You could use a RegularExpressionValidator or RangeValidator that would validate the value on the client side before submitting it to the server.
This '^[1-9]{1}[0-9]{3} ?[A-Z]{2}$' would be a valid regex to check a Dutch postal code.
Or instead of throwing an exception in your property setter, you could show a label that is normally hidden in your markup.
If you really want to throw an exception have a look at the Page_Error event. You can use that to catch all exceptions in your page and handle them if appropriately.