Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2011, 11:32 PM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default Fear of Bad Habit

Hi Imar,

I greatly appreciate your response to my posts because they mean a lot to me.

The thing is I am preparing to sit the MTA exam and Microsoft recommended this book as a starting point (http://msdn.microsoft.com/en-us/beginner/bb308786.aspx).

I am happy that I can read the book, ask questions and get helpgul answers.

Here is my current delima. Since I read the potion of the book on webservices, I feel like I am bye-passing JavaScript to a great extent. I write the most functional code on the server and then call them from the client side.

I just want to know if this is a good habit. If it isn't, what is an alternative? Second, does calling webservices carry any overhead?

As a classic example, here is a scenario.

In this scenario, I have a ContactUs.aspx page where I have placed a form for a user to submit comments and/or complaints about services in my sister's salon. These commets go straight to my sister's email. However, on the same page, I have a link (a normal html <a> with just the runat attribute added) that allows the users to send complaints directly to me if they found the site difficult to use or had problem, in general, with the site.

When a user clicks this link, a ModalPopUp (taken from the AjaxControlToolkit you mentioned) appears with a similar form as the one on the page itself. I tried to make this form save the user's complain to a database (I know it may not be necessary in this case, but I just wanted to practice working with databases). I noticed that whenever I click the submit button in the modal pop up, the validation controls on the page itslef fire up. So I decided to change the submit button in the modal dialog to an HTML input control that will not cause a postback. However, I still wanted to save the data to the database though. On the Ajax website, they had a tutorial on how to handle postback from the modal dialog but I thought it was too complicated for me to wrap my head around.

So I did the following:
  • Create A Webservice with the following code:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using RubyDBModel;

/// <summary>
/// Summary description for FeedbackService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]
public class FeedbackService : System.Web.Services.WebService {

    public FeedbackService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    RubyDBEntities myEnt = new RubyDBEntities();

    [WebMethod]
    public string SubmitFeedBack(string fullName, string categoryID, string feedback, string email) 
    {
        //string result = "";

        FeedbackTable myFeedack = new FeedbackTable();

        try
        {
            myFeedack.Feedback = feedback;
            myFeedack.UserName = fullName;
            myFeedack.FeedbackCategoryID = Convert.ToInt32(categoryID);
            myFeedack.EmailAddress = email;
            myEnt.AddObject("FeedbackTables", myFeedack);
            myEnt.SaveChanges();

            return "Success";
        }
        catch(Exception ex)
        {
            return "Error";
        }


    }
    
}
  • Call this method from the client side using Ajax:

Code:
function SubmitFeedback()
     {
         var name = $get("<%= nameTextBox.ClientID%>");
         var list = $get("<%= categoryDropDownList.ClientID %>");
         var text = $get("<%= feedbackTextBox.ClientID %>");
         var email = $get("<%= userEmailTextBox.ClientID %>"); 
         if (name.value == "" || !isNaN(name.value) || name.value.indexOf(" ") < 0)
         {
             alert("You have not entered a correct name.");
             name.focus();
         }
         else if (list.options[list.selectedIndex].index == 0)
         {
             alert("You have not selected a feedback category.");
             list.focus();
         }
         else if (text.value == "" || text.value.length < 30)
         {
             alert("Your feedback messages may not be entered or may be less than 30 characters.");
             text.focus();
         }
         else
         {
             FeedbackService.SubmitFeedBack
             (
                name.value, list.options[list.selectedIndex].value, text.value, email.value, SubmitCallback
             );
         }
     }

     function SubmitCallback(result)
     {
         if (result == "Success")
         {
             alert("Your feedback has been successfully submitted.");
         }
         else
         {
             alert("sorry, there was an error. Please try later.");
         }
     }
Is this a good practice? Was there a better way I could have done this?

Finally, since Microsoft trusts you to the extend that they have given you MVP more than once, I can as well trust YOU. Can you please recommend the next title I should read in my preparation for the MTA exam? I intend to sit the exam in March.

Thank you.
 
Old December 24th, 2011, 07:23 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Lots of questions throughout this post. I'll try to find and answer them....

Quote:
I noticed that whenever I click the submit button in the modal pop up, the validation controls on the page itslef fire up
.
That's probably just a matter of setting the right ValidationGroup on the controls. This way you can group validators, so only those in the pop up, or those in the page are avtive, but not both at the same time.

Quote:
Here is my current delima. Since I read the potion of the book on webservices, I feel like I am bye-passing JavaScript to a great extent. I write the most functional code on the server and then call them from the client side.
I just want to know if this is a good habit. If it isn't, what is an alternative? Second, does calling webservices carry any overhead?
That certainly works. What I personally like about this model is the limited amount of data that gets sent back and forth. With a web service call like this, you only submit the form data for the popup, and not the entire page / form. Also, the returned value is much smaller, causing less overhead.

And yes, there is overhead in calling a web service, but it's probably less than doing a full postback because of the smaller amount of data that gets submitted.

Quote:
Can you please recommend the next title I should read in my preparation for the MTA exam? I intend to sit the exam in March.
Don't know what an MTA exam is, so not sure what to recommend. Maybe Professional ASP.NET 4 in C# and VB is a good choice?

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!
 
Old December 24th, 2011, 07:30 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Oh, forgot one:

Quote:
Is this a good practice? Was there a better way I could have done this?
Looks like you invented your own validation wheel. I would instead use the standard validation controls as they can work on the client and at the server (but not in a Web Servcice).

Since you are now calling a Web Service, you should implement your own security in the service method. It would be easy to bypass your client side validation code and submit whatever I feel like directly to your servic method.

Cheers,

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!
 
Old December 24th, 2011, 08:28 AM
Authorized User
 
Join Date: Jan 2011
Posts: 41
Thanks: 0
Thanked 1 Time in 1 Post
Default

Oh yeah I realize I forgot the server side validation. Thanks. Since its just a matter of grouping the validation controls, I will use them instead.

Thanks.

PS: MTA is Microsoft's new entry level certification. I guess when you started, it wasn't available.
 
Old December 24th, 2011, 08:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
PS: MTA is Microsoft's new entry level certification. I guess when you started, it wasn't available.
Ah, I see. Thanks.

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
bad editing econophil BOOK: Beginning JavaScript 4th Edition 4 July 6th, 2010 05:30 PM
502 - Bad Gateway PeterPeiGuo ASP.NET 3.5 Basics 2 March 5th, 2010 12:07 AM
eBook BAD images! trechriron BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 2 June 16th, 2009 03:54 AM
Very slow and almost no replies...BAD..!!! zach007 Forum and Wrox.com Feedback 4 December 23rd, 2006 06:48 PM
Why session_register() is bad. nikolai Pro PHP 17 November 21st, 2004 09:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.