Anda di halaman 1dari 7

8/7/13 Gridview validation problem

social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 1/7
Answered by:
Top related threads
Gridview TextboxColumn Validations
problem
Gridview edit button problem -
Specified argument was out of the
range of valid values.
DataKey And Custom Validation In A
Gridview
Validation of GridView contents
TextBox In gridview validations
Ask a question
Quick access
Search related threads Search forum questions
2 0 0
hedgeu's threads
View Profile
55 Points
hedgeu
Windows Forms forums >
Gridview validation problem
Question
0
Sign in to
vote
Reply | Quote |
Thursday, June 15, 2006 12:07 AM
80 Points
I have a integer column on a gridview of a webform. The problem is that the user can put in a
character (like an "A") in the field and when the update button is selected the user gets an ugly
message.
What is the best way to deal with this problem?
Answers
0
Sign in to
vote
Hi WoodrowS,
I am assuming that the column where the user is entering an integer is a template column. To
validate a user input for an integer you need to use compare validator control.
I have given a sample gridview below that contains a template column and compare validator control.
Particularly look at this item template section: you need to set ControlToValidate, Operator and Type
parameters to configure to your requirement.
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Value should be integer" Operator="DataTypeCheck" Type="Integer">
</asp:CompareValidator>
</ItemTemplate>

---------------------------------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="OrderID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True"
SortExpression="OrderID" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Value should be integer" Operator="DataTypeCheck" Type="Integer">
</asp:CompareValidator>
</ItemTemplate>
Windows Forms General
WoodrowS
Dev Center
Sign in
8/7/13 Gridview validation problem
social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 2/7
Reply | Quote |
Thursday, June 15, 2006 4:46 AM
55 Points
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID" InsertCommand="INSERT
INTO [Orders] ([CustomerID], [EmployeeID], [OrderDate]) VALUES (@CustomerID, @EmployeeID,
@OrderDate)"
SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate] FROM [Orders]"
UpdateCommand="UPDATE [Orders] SET [CustomerID] = @CustomerID, [EmployeeID] =
@EmployeeID, [OrderDate] = @OrderDate WHERE [OrderID] = @OrderID">
<DeleteParameters>
<asp:Parameter Name="OrderID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
<asp:Parameter Name="OrderDate" Type="DateTime" />
<asp:Parameter Name="OrderID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
<asp:Parameter Name="OrderDate" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>

HTH
Uday
All replies
0
Sign in to
vote
Hi WoodrowS,
I am assuming that the column where the user is entering an integer is a template column. To
validate a user input for an integer you need to use compare validator control.
I have given a sample gridview below that contains a template column and compare validator control.
Particularly look at this item template section: you need to set ControlToValidate, Operator and Type
parameters to configure to your requirement.
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Value should be integer" Operator="DataTypeCheck" Type="Integer">
</asp:CompareValidator>
</ItemTemplate>

---------------------------------------------------
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="OrderID"
DataSourceID="SqlDataSource1">
<Columns>
hedgeu
8/7/13 Gridview validation problem
social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 3/7
Reply | Quote |
Thursday, June 15, 2006 4:46 AM
55 Points
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True"
SortExpression="OrderID" />
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" />
<asp:CommandField ShowEditButton="True" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1"
ErrorMessage="Value should be integer" Operator="DataTypeCheck" Type="Integer">
</asp:CompareValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Orders] WHERE [OrderID] = @OrderID" InsertCommand="INSERT
INTO [Orders] ([CustomerID], [EmployeeID], [OrderDate]) VALUES (@CustomerID, @EmployeeID,
@OrderDate)"
SelectCommand="SELECT [OrderID], [CustomerID], [EmployeeID], [OrderDate] FROM [Orders]"
UpdateCommand="UPDATE [Orders] SET [CustomerID] = @CustomerID, [EmployeeID] =
@EmployeeID, [OrderDate] = @OrderDate WHERE [OrderID] = @OrderID">
<DeleteParameters>
<asp:Parameter Name="OrderID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
<asp:Parameter Name="OrderDate" Type="DateTime" />
<asp:Parameter Name="OrderID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="CustomerID" Type="String" />
<asp:Parameter Name="EmployeeID" Type="Int32" />
<asp:Parameter Name="OrderDate" Type="DateTime" />
</InsertParameters>
</asp:SqlDataSource>

HTH
Uday
hedgeu
8/7/13 Gridview validation problem
social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 4/7
0
Sign in to
vote
Reply | Quote |
Tuesday, June 20, 2006 4:18 PM
5 Points
I don't know whether you are aware, but if you have a GridView with quite a few lines 100+, then
puting the CompareValidator within the ItemTemplate increase the page size drastically and slows
down the loading.
It would appear that each line of the GridView is linked to the CompareValidator routine (try "View
Source" when the page is loaded to see what I mean). I replaced it with some javascript, using the
"onblur" event of the text box. The code kind-of works, but is nothing like as good as the
validation that the CompareValidator gives.
Hopefully, somebody else will have a better solution
Simon
0
Sign in to
vote
Reply | Quote |
Tuesday, June 20, 2006 4:34 PM
80 Points
Given what you've said, I hope someone will too. It seems to me that the DataFormatString
property is a very good option for simple validation but I have not been able to get it to work, for
example, I hae not been able to come up with a value there that allows only 3 digits or less.
0
Sign in to
vote
Reply | Quote |
Wednesday, June 21, 2006 8:03 AM
5 Points
I know this will only slightly help, but you can restict the number of characters in an asp:textbox
by setting maxlength, e.g.
<asp:TextBox ID="TextBox4" runat="server" Columns="3" MaxLength="3"></asp:TextBox>
The trouble is of course that it can be any characters.
I don't know about the DataFormatString, I would think that's really for displaying data
- I hope I'm wrong.
If you're doing postbacks on a line-by-line basis, then put the validator control in the
<EditItemTemplate> of your <TemplateControl> and you shouldn't get the problems I
was getting. I was posting the whole grid (to cut down on the postbacks), rather than
on a line by line basis, which is why I had to put the validator control in the
<ItemTemplate>, causing my "large page" problem.
Simon
shaggymog the first
WoodrowS
shaggymog the first
8/7/13 Gridview validation problem
social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 5/7
0
Sign in to
vote
Reply | Quote |
Wednesday, July 12, 2006 6:00 PM
0 Points
Is there any way to validate GridView fields to avoid inserting empty strings for fields that are
required? For the fields that allow NULL, to avoid an error, I used
ConvertEmptyStringToNull="false", the only problem left is to control the fields that do not
allow NULL values
<code>
<asp:GridView ID="Companies_GridView" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Companies" DataKeyNames="CompanyID,UserID,ModuleId"
CellPadding="4" CellSpacing="1" EnableViewState="False" ForeColor="Teal">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="UserID" HeaderText="UserID" SortExpression="UserID" Visible="False" />
<asp:BoundField DataField="ModuleId" HeaderText="ModuleId" SortExpression="ModuleId"
Visible="False" />
<asp:BoundField DataField="CompanyID" HeaderText="Company ID" SortExpression="CompanyID" />
<asp:BoundField DataField="CompanyAbbrev" HeaderText="Company Abbrev"
SortExpression="CompanyAbbrev" />
<asp:BoundField DataField="CompanyName" HeaderText="Company Name"
SortExpression="CompanyName" />
<asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
<asp:BoundField DataField="Zip" HeaderText="Zip/Postal Code" SortExpression="Zip" />
<asp:BoundField DataField="MainPhone" HeaderText="Main Phone" SortExpression="MainPhone" />
<asp:BoundField DataField="MainFax" HeaderText="Main Fax" SortExpression="MainFax"
ConvertEmptyStringToNull="false" />
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country"
ConvertEmptyStringToNull="false" />
<asp:BoundField DataField="Website" HeaderText="Website" SortExpression="Website"
ConvertEmptyStringToNull="false" />
<asp:BoundField DataField="CompanyDateCreated" HeaderText="Date Created"
SortExpression="CompanyDateCreated"
DataFormatString="{0:d}" HtmlEncode="False" ReadOnly="true" />
</Columns>
<EmptyDataTemplate>
There are no any Companies at this time
</EmptyDataTemplate>
</asp:GridView>
</code>
Thanks,
Waldis
Waldis
8/7/13 Gridview validation problem
social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 6/7
0
Sign in to
vote
Reply | Quote |
Thursday, September 20, 2007 1:41 PM
0 Points
Well, to start with, convert your boundfields to be TemplateFields. In that way you can give
meaningful names to your input boxes like "txtCompanyName" which can be used in the
aspRequiredFieldValidator. For example using your bound field
<code>
<asp:TemplateField >
<EditItemTemplate>
<asp:TextBox ID="txtCompanyName" runat="Server" Text='<%=Bind("Name") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator ID="CompanyNameValidator" runat="server"
ErrorMessage="Name cannot be empty"
ControlToValidate="txtCompanyName">Name cannot be empty</asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%=Bind("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</code>

Now in the GridView create a CommandField and have it appear as a link. Make sure that the
ShowEditButton is set to true and then convert the commandField to be a TemplateField. The
code for it will look like the following snippet

<code>
<asp:TemplateField >
<EditItemTemplate>
<asp:LinkButton ID="NameEdit" runat="server" CausesValidation="True"
CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="NameCancel" runat="server" CausesValidation="False"
CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="NameEdit" runat="server" CausesValidation="True"
CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
/asp:TemplateField>
</code>

This sample works for me in .NET 2.0 using Visual Studio 2005. I have not tested the above using
the earlier versions.

Hope this helps.
-venu
rvenu
8/7/13 Gridview validation problem
social.msdn.microsoft.com/Forums/windows/en-US/71a9af07-e9c9-4698-b81b-f16128f1e5a6/gridview-validation-problem 7/7
Centers
Dev Center Home
Windows Store apps
Internet Explorer
Desktop
Hardware
Other Wi ndows si tes
Enterpri se
Smal l busi ness
Students
Home users
Downl oads
Wi ndows 8
Vi sual Studi o Express 2012 f or
Wi ndows 8
Vi sual Studi o Prof essi onal 2012
Internet Expl orer 10 f or Wi ndows 7
Stay connected
Wi ndows Store newsl etter
Mi crosof t events
Wi ndows App Bui l der Bl og
2013 Mi crosof t Hel l o f rom Seattl e. Uni ted States ( Engl i sh) Terms of Use Trademarks Pri vacy and Cooki es

Anda mungkin juga menyukai