|
 |
BOOK: Beginning ASP.NET 4.5 : in C# and VB
 | This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 4.5 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

January 14th, 2014, 04:57 PM
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
Custom Validator
Hi Imar,
I've been trying to get a Custom Validator to work in a Listview. I've been using your advice starting on page 327. The server side is fine and I'm able to get into the client JS but I'm not able to access the data field that is being validated.
I set up a small website and a stripped down listview just to test this out with a trivial validation test. I've tried everything you wrote about and lots of time on stack overflow. My web.config has "<add key="ValidationSettings:UnobtrusiveValidationMode" value="WebForms"/>". I did change the Global.asax file per your suggestion and added the scriptmanager code but that didn't seem to help either. So, I've back out the scriptmanager for now. I'm including all the code for the aspx and vb:
aspx
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
function checklength(source, args) {
// this function does fire but can't get at the field
//alert("in js")
//if (SecondField != '') {
// args.IsValid = true;
//}
//else {
// args.IsValid = false;
//}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataKeyNames="myKey"
itemtype="TestTable"
selectmethod="ListView1_GetData"
updatemethod="ListView1_UpdateItem" >
<EditItemTemplate>
<tr style="">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="myKeyLabel1" runat="server" Text='<%# Eval("myKey") %>' />
</td>
<td>
<asp:TextBox ID="FirstFieldTextBox" runat="server" Text='<%# Bind("FirstField") %>' />
</td>
<td>
<asp:TextBox ID="SecondFieldTextBox" runat="server" Text='<%# Bind("SecondField") %>' ClientIDMode="Static" />
</td>
<td>
<asp:TextBox ID="commentTextBox" runat="server" Text='<%# Bind("comment") %>' />
</td>
</tr>
<tr>
<td></td><td></td>
<td>
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="xx not allowed"
ControlToValidate="SecondFieldTextBox"
Display="Dynamic"
ClientValidationFunction="checklength"
OnServerValidate="CustomValidator1_ServerValidate" >
</asp:CustomValidator>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<ItemTemplate>
<tr style="">
<td>
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="myKeyLabel" runat="server" Text='<%# Eval("myKey") %>' />
</td>
<td>
<asp:Label ID="FirstFieldLabel" runat="server" Text='<%# Eval("FirstField") %>' />
</td>
<td>
<asp:Label ID="SecondFieldLabel" runat="server" Text='<%# Eval("SecondField") %>' />
</td>
<td>
<asp:Label ID="commentLabel" runat="server" Text='<%# Eval("comment") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="0" style="">
<tr runat="server" style="">
<th runat="server"></th>
<th runat="server">myKey</th>
<th runat="server">FirstField</th>
<th runat="server">SecondField</th>
<th runat="server">comment</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style=""></td>
</tr>
</table>
</LayoutTemplate>
</asp:ListView>
</div>
<div>
<asp:label id="msglabel" runat="server" Text=""></asp:label>
</div>
</form>
</body>
</html>
VB (validator code from stackoverflow)
Code:
Imports TinyDBEntities
Imports System.Data
Imports System.Linq
Partial Class _Default
Inherits System.Web.UI.Page
Public Function ListView1_GetData() As IQueryable(Of TestTable)
Dim myContext As New TinyDBEntities()
Return From mydata In myContext.TestTables Select mydata
End Function
Public Sub ListView1_UpdateItem(testtable As TestTable)
If Page.IsValid Then
Else
msglabel.Text = " Your update Failed"
Exit Sub
End If
Dim myContext As New TinyDBEntities()
Dim item As TestTable = (From s In myContext.TestTables
Where s.myKey = testtable.myKey
Select s).SingleOrDefault()
If item Is Nothing Then
msglabel.Text = " Your update Failed"
Return
End If
TryUpdateModel(item)
item.FirstField = Convert.ToInt32(testtable.FirstField)
If ModelState.IsValid Then
myContext.SaveChanges()
msglabel.Text = " Your update was Successful"
Else
msglabel.Text = " Your update Failed"
End If
End Sub
Protected Sub CustomValidator1_ServerValidate(source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
Dim cv As CustomValidator = CType(source, CustomValidator)
Dim testpos As Integer
Dim test As String
test = args.Value
testpos = InStr(1, test, "xx")
If testpos > 0 Then
args.IsValid = False
cv.ErrorMessage = "xx is not allowed"
Else
args.IsValid = True
End If
'MsgBox(args.Value)
End Sub
End Class
So, the problem is that if I want to warn the user at client time, I just can't seem to get at the data field. I've tried to also use the name as shown in the browser source code.
Thanks.
|

January 14th, 2014, 05:08 PM
|
 |
Wrox Author
Points: 72,055, Level: 100 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
|
|
Hi there,
First of all, I think your field is called SecondFieldTextBox, and not SecondField as you use in the JavaScript function.
But more importantly, since this code is in a list, there is not a single SecondFieldTextBox, there are multiple which means you won't be able to write the JavaScript function to target a single control.
You want to look at args.Value instead, as that provides you with the value for the control that fired the validation function. For more information, check http://msdn.microsoft.com/en-us/libr...vs.110%29.aspx
Hope this helps,
Imar
|
The Following User Says Thank You to Imar For This Useful Post:
|
|

January 14th, 2014, 06:58 PM
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
I can see the field now!
Yes, I tried all variations of that name. But, as you suggest, that approach is going to be ambiguous.
Quote:
You want to look at args.Value instead, as that provides you with the value for the control that fired the validation function.
Imar
|
Yes, you are right as usual! I should have thought of that because args.value is used on the server side - but, important to use "Value" and not "value" just as you showed.
So, this works:
Code:
function checklength(source, args) {
var str = args.Value
var n = str.indexOf("xx");
if (n == -1) {
args.IsValid = true;
}
else {
args.IsValid = false;
alert ("no xx allowed")
}
}
I still have some fooling around to do with completely understanding the flow of control with both client and server code - but, this was the big show stopper.
Thanks once again. BTW, I read a comment where MS plans to reinstate the EF model they had in EF5. Is this true?
|

January 14th, 2014, 08:40 PM
|
 |
Wrox Author
Points: 72,055, Level: 100 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
|
|
Just in case it helps, here's what happens:
1. The page loads
2. You fill in some values
3. You hit the submit button
4. The client side code fires, and validates the fields. When at least one of them doesn't pass validation, one or more error messages are shown and the form is *not* submitted.
5. You complete the form and hit submit again.
6. On the server, the validators run again. Since the form is validated already at the client, they should pass without further validation.
However, if you disable client side scripts (a malicious user could do that easily), step 4 is skipped, and step 6 will still catch any incorrect entries at the server. That's why server-side validation is so important.
Hope this helps.
>> BTW, I read a comment where MS plans to reinstate the EF model they had in EF5. Is this true?
Not sure I understand what you mean with this.
Cheers,
Imar
|

January 14th, 2014, 09:06 PM
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
EF5 and 6
Hi Imar,
Thanks for the flow explanation - yes, that helps.
Re EF:
"We are planning to have a preview of a version of the EntityDataSource that works with EF6 out soon. I will update this thread when it is available."
This is at or near the bottom of this thread where I was venting a bit about lack of doc for VB-linq-EF6 in VS2013:
https://entityframework.codeplex.com/discussions/434936
"divega" posted this on what looks like 12-21-13. I've not heard anything since.
|

January 15th, 2014, 08:31 AM
|
 |
Wrox Author
Points: 72,055, Level: 100 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
|
|
Oh, I see. Yes, I read that elsewhere too. But in general it seems Microsoft is recommending against using this control now. They update it primarily for Dynamic Data Sites. The preferred solution is to use Model Binding which I am covering in my upcoming ASP.NET 4.5.1 / VS 2013 book.
Cheers,
Imar
|

January 15th, 2014, 10:28 AM
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
Model Binding
Hi Imar,
Yes, I also like the Model Binding methods. My big complaint with Microsoft has been the lack of documentation for EF6-Linq- VB - especially for using controls like Listview. We need your new book! I got a notice from Amazon saying "delivery estimate: March 19, 2014".
BTW, I've launched a companion site for doing a survey of bicyclists using my main site. Also some changes to the main one. Although most of the difficult changes are in the admin area and not visible, there are a few worth looking at. The "Why Ozaukee " page has a slide show using code from a fellow in Russia and the "Routes" page has been overhauled plus some cute jquery. The survey site uses a template from a UK guy (note: I gave you a mention on the Contact Us page). Getting to be an international effort.
Main site: bikex (which also has a link to the survey)
Survey: survey
|

January 15th, 2014, 03:59 PM
|
 |
Wrox Author
Points: 72,055, Level: 100 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
|
|
Quote:
I got a notice from Amazon saying "delivery estimate: March 19, 2014".
|
Yeah, I think that's about accurate....
Quote:
note: I gave you a mention on the Contact Us page
|
Thanks! But you may want to fix the spelling of my name ;-)
Cheers,
Imar
|

January 15th, 2014, 04:42 PM
|
Authorized User
|
|
Join Date: Dec 2011
Posts: 86
Thanks: 20
Thanked 3 Times in 3 Posts
|
|
Fixed
Quote:
Originally Posted by Imar
fix the spelling of my name
|
Sorry about my fat fingers - but, at least I had the link behind your name spelled correctly for your website. It's Fixed now:
|

January 15th, 2014, 05:05 PM
|
 |
Wrox Author
Points: 72,055, Level: 100 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
|
|
Thanks. Happens to me all the time too....
Imar
|
Thread Tools |
Search this Thread |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
Thread |
Thread Starter |
Forum |
Replies |
Last Post |
Custom validator |
barakros |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 |
1 |
February 25th, 2010 07:58 AM |
Custom Validator against a database |
Steve Spicer |
ASP.NET 2.0 Professional |
16 |
January 12th, 2008 06:25 AM |
custom validator question |
kscdave |
Classic ASP Professional |
3 |
April 24th, 2005 07:11 AM |
Custom Validator |
bmains |
ASP.NET 1.0 and 1.1 Basics |
4 |
December 18th, 2003 11:45 AM |
|
 |
All times are GMT -4. The time now is 10:25 AM.
|