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.