Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET Web Pages with WebMatrix
This is the forum to discuss the Wrox book Beginning ASP.NET Web Pages with WebMatrix by Mike Brind, Imar Spaanjaars ; ISBN: 978-1-1180-5048-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET Web Pages with WebMatrix 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 .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #11 (permalink)  
Old November 3rd, 2014, 04:36 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Are you sure your Helpers code file is correct and in App_Code. This should work, unless Helpers is broken, or in the wrong place.

I do get errors on this code though:

if (HttpContext.Current.Request["title"].Length > 200)

when there is no title. You should wrap that code inside the IsPost check.

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #12 (permalink)  
Old November 3rd, 2014, 05:07 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Helpers

Yes, I have added Helpers, under the folder App_Code:

@helper Selected(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>selected=\"selected\"</text>
}
}
}
@helper Checked(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>checked=\"checked\"</text>
}
}
}

These functions do work, as I ran it in another example (following the book here). But when I try to use it in my main project, the errors pop up (context errors). I'm rather new to all of this; what have I missed?
Reply With Quote
  #13 (permalink)  
Old November 3rd, 2014, 06:28 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

>> I'm rather new to all of this; what have I missed?

I don't know yet. This should just work.

Can you post the exact error message and a description of when you get it? (In VS, in the browser on load or after a postback and so on)?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #14 (permalink)  
Old November 3rd, 2014, 06:29 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

Also, did you correct the other error I mentioned? Finally, can you post the full code for the page as you have it now?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #15 (permalink)  
Old November 3rd, 2014, 06:32 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Errors

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'Helpers' does not exist in the current context

Source Error:


Line 55: <select name="duration">
Line 56: <option value="">--Choose one</option>
Line 57: <option value="1" @Helpers.Selected("Duration", "1")>1 Day</option>
Line 58: <option value="3" @Helpers.Selected("Duration", "3")>3 Days</option>
Line 59: <option value="7" @Helpers.Selected("Duration", "7")>7 Days</option>

Source File: c:\Users\dabbilus\Documents\My Web Sites\Kastla\Sell.cshtml Line: 57


Show Detailed Compiler Output:

Show Complete Compilation Source:


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34212

This is the error I get when I run the program.
Reply With Quote
  #16 (permalink)  
Old November 3rd, 2014, 07:09 PM
Imar's Avatar
Wrox Author
Points: 71,164, Level: 100
Points: 71,164, Level: 100 Points: 71,164, Level: 100 Points: 71,164, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,948
Thanks: 79
Thanked 1,555 Times in 1,532 Posts
Default

What's the file name of the helpers code in App_Data?

Also, can you post your current code as per my previous post?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
  #17 (permalink)  
Old November 3rd, 2014, 08:28 PM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Code

Page.Title = "Post Your Advertisement";
if(IsPost){
if(HttpContext.Current.Request["title"].IsEmpty()){
ModelState.AddError("title", "Please provide a title");}
}
if(HttpContext.Current.Request["title"].Length > 200){
ModelState.AddError("title", "Your title cannot exceed 200 characters");
}
if(HttpContext.Current.Request["description"].IsEmpty()){
ModelState.AddError("description", "You must provide a description");
}
if(HttpContext.Current.Request["duration"].IsEmpty()){
ModelState.AddError("duration", "Please choose a duration");
}
if(HttpContext.Current.Request["price"].IsEmpty()){
ModelState.AddError("price", "You must provide price");
}
if(!HttpContext.Current.Request["price"].IsDecimal() && !HttpContext.Current.Request["price"].IsEmpty()){
ModelState.AddError("price", "Provide a valid number for the price");
}
if(HttpContext.Current.Request["condition"].IsEmpty()){
ModelState.AddError("condition", "State the condition for your item");
}
if(HttpContext.Current.Request["email"].IsEmpty()){
ModelState.AddError("email", "You must provide a valid email");
}
if(!ModelState.IsValid){
ModelState.AddFormError("Please fix the errors");
}
}
<body>
<form id="post-advert" action="@Href("~/Sell")" method="post" >
<fieldset>
<legend>Post Your Advertisement</legend>
@Html.ValidationSummary(true)
<div>
<label for="title">Title</label>
</div>
<div>
<input type="text" name="title" value="@Request["title"]" />
@Html.ValidationMessage("title")
</div>
<div>
<label for="description">Description</label>
</div>
<div>
<textarea name="description">@Request["description"]</textarea>
@Html.ValidationMessage("description")
</div>
<div>
<label for="duration">Duration</label>
</div>
<div>
<select name="duration">
<option value="">--Choose one</option>
<option value="1" @Helpers.Selected("Duration", "1")>1 Day</option>
<option value="3" @Helpers.Selected("Duration", "3")>3 Days</option>
<option value="7" @Helpers.Selected("Duration", "7")>7 Days</option>
<option value="14" @Helpers.Selected("Duration", "14")>14 Days</option>

</select>
@Html.ValidationMessage("duration")
</div>
<div>
<label for="price">Price</label>

</div>
<div>
<input type="text" name="price" value="@Request["price"]"/>
@Html.ValidationMessage("price")
</div>
<div>
<label for="condition">Price</label>

</div>
<div>
<input type="radio" name="condition" value="@Request["condition"]"
@Helpers.Checked("condition","Fair")/> Fair
<input type="radio" name="condition" value="Good"
@Helpers.Checked("condition", "Good")/>Good
<input type="radio" name="condition" value="As New"
@Helpers.Checked("condition", "As New")/>As New
@Html.ValidationMessage("condition")
</div>
<div>
<label for="email">Your Email Address</label>
</div>
<div>
<input type="text" name="email" value="@Request["email"]"/>
@Html.ValidationMessage("email")
</div>
<div>
<input type="submit" name="submit" value="Post" />
</div>
</fieldset>

</form>
</body>
@section footer{
This is a footer for the Sell page
}/>

Helpers.cshtml is the Form name under App_Code Folder.


Functions.cshtml is the form for Functions
@using System.Text.RegularExpressions;
@functions{
public static bool IsValidEmail(string s){
string pattern = @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
return Regex.IsMatch(s, pattern);

}
}

I know that this will help: Intellisense doesn't recognize the two functions in Helpers. There was 4 methods under Intellisense, and neither Selected and Checked was not there ( BrowserHelper was one of them). Should I make a Using statement for ASP.Web Net? MVC is not using it now.
Reply With Quote
  #18 (permalink)  
Old November 4th, 2014, 12:45 AM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

Can you post the code that's in Helpers.cshtml?
Reply With Quote
  #19 (permalink)  
Old November 4th, 2014, 07:31 AM
Registered User
Points: 28, Level: 1
Points: 28, Level: 1 Points: 28, Level: 1 Points: 28, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Helpers code

@helper Selected(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>selected=\"selected\"</text>
}
}
}
@helper Checked(string option, string value){
if(HttpContext.Current.Request[option] !=null){
var values = HttpContext.Current.Request[option].Split(',');
if(values.Contains(value)){
<text>checked=\"checked\"</text>
}
}
}

There you go. There is only the two functions.

Like you, I have authored seven books (Trafford here in the USA and Örn og Örlygur publishers in Iceland). In the 90's I authored Easy VB (I was a Systems Analyst) in Iceland and in 2005 I had a massive stroke which left me totally unable to read, write and speak for almost five years. I had to retrain myself to where I could do languages (English and Icelandic), but unfortunately, I had lost my computer skills (I worked on the FCAT Team in Florida and used programming in C# with ASP). Since I have not worked ever since the stroke, I just wanted to see if I could still do the work. It is frustrating to say the least, so please bear with me.

Snæbjörn
Reply With Quote
  #20 (permalink)  
Old November 4th, 2014, 08:36 AM
Wrox Author
Points: 412, Level: 7
Points: 412, Level: 7 Points: 412, Level: 7 Points: 412, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 57
Thanks: 1
Thanked 19 Times in 19 Posts
Default

Your error messages have me confused. There must be something else going on that we cannot see. I suggest you download the code for the chapter and compare it to what you have there - both content and structure. Make sure the chapter code runs first. The latest version of WebMatrix may prompt you to upgrade to a more recent version of Web Pages. You should say no to that invitation for the time being.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Validating Input XML ssbsts XSLT 4 January 5th, 2011 11:59 AM
Chapter 9 Validating User Input Don Hurlbert BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 7 August 18th, 2010 02:42 PM
validating input bukky VBScript 1 April 1st, 2004 03:30 AM
validating user input hosefo81 Javascript How-To 12 March 3rd, 2004 08:32 AM
Validating user input stu9820 VB.NET 2002/2003 Basics 2 January 15th, 2004 11:51 AM



All times are GMT -4. The time now is 09:32 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.