Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have a problem that I hope you will be able to help resolve.

I have built an asset booking system to enable staff to book out equipment. When booking, staff have to choose the time using the provided calendar, and then assets using a checkbox. All is working, but I would like to PostBack once the user choose time, then check if there are any assets that are booked for the same time, then display 'Unavailable' label box instead of the checkbox.

I would greatly appreciate it if you have any suggestions on how to go about achieving this.
Posted

hmm. maybe you need a mix of Asynchronous Javascript And Xml? That's a mouthful. lets shorten it to AJAX :Þ

Here is a good article:
How to call a Web Service from client-side JavaScript using ASP.NET AJAX[^]


Basically, this call will send to a webservice and receive what is returned. This all happens via javascript so the page doesn't need to reload or anything.

The data you send and how to handle the returned values is the only challenge here as you need to do this all from javascript.

Have fun ^_^
 
Share this answer
 
Comments
Awoldeselassie 27-May-15 8:04am    
Thanks for your suggestion Andy, but I have created the application using web form and fully working. I just need to add that functionality using whatever, (C# if possible, rather than creating a new webservice.
Andy Lanng 27-May-15 8:14am    
So you want the whole form to postback when they type? or on a button?
F-ES Sitecore 27-May-15 11:05am    
Use a WebMethod instead then, google "calling a webmethod from jQuery" for example code.
Awoldeselassie 27-May-15 12:06pm    
There is a calendar control for date, and time is just a normal dropdown menu populated at runtime. It would be ideal when the user choose a date, if I can compare it with the startDate column of the tblBooking, and if it matches, then compare the StartTime too. If that matches too, or is in between the StartTime and EndTime, then Checkbox.visible = false and lable.visible = true. If not insert booking data into the database.

I have few dropdown. Every time a dropdown changes, the page postBack and populate a gridView and other dropdown depending on the first dropdown option.

Sorry if this sounds complicated.
Andy Lanng 27-May-15 12:16pm    
It's ok. We just need to understand what the question is.
If I have this correct: The page already posts back when dropdowns are changed. You want it to also postback if the calendar control dropdowns change.

I assume that the calender control does not expose any obvious methods to allow for the postback.

hmm

Please post the markup where you add the calendar control. There are many calendars out there so I need to know which one you are using.
The issue is that you need both the data and the time to be populated before you do a postback, but you want to postback as soon as these details are complete. Here's one way this can be done:

JavaScript
$.(function(){
 //get the textbox control with a jquery selector
 var startTextBox = $("#tbxStartDate");

 //and the time drop down
 var startTimeDropDown = $("#ddlStartTime");

 //capture text change events
 $(startTextBox).on('input propertychange paste', function(event){
  //prevent auto-postback;
  event.preventDefault();
  validateStartDateTime(startTextBox,startTimeDropDown );
 });

 //capture drop down change events
 $(startTimeDropDown ).on('change', function(){
  validateStartDateTime(startTextBox,startTimeDropDown );
 });

});

function validateStartDateTime (dateTextBox, timeDropDown){
  
  //Make sure that both fields are valid after the change
  

  //perform the postback event.  
  // This will cause the page to reload and the textbox's TextChanged event to fire
  __doPostBack("tbxStartDate","TextChanged");
}

....

EDIT: As for the database search:
C#
public void test(){
DateTime startDateTime = DateTime.ParseExact(txtStartDate.Text,"dd-MM-yyy",CultureInfo.InvariantCulture)
    + TimeSpan.ParseExact(ddlStartTime.SelectedText, "HH:mm", CultureInfo.InvariantCulture);

DateTime endDateTime = DateTime.ParseExact(txtEndDate.Text,"dd-MM-yyy",CultureInfo.InvariantCulture)
    + TimeSpan.ParseExact(ddlEndTime.SelectedText, "HH:mm", CultureInfo.InvariantCulture);

//Check that the booking is valid:
if(endDateTime > startDateTime)
 throw new YouMadBroException();

string queryString = @"
Select *
From Bookings b
Where b.EndDateTime > @StartDateTime AND b.StartDateTime < @EndDateTime
";
//it may look odd but that's all you need to check for overlap!


}
 
Share this answer
 
v5
Comments
Awoldeselassie 28-May-15 5:43am    
Thanks Andy, I been trying your code for an hour but still no joy... You are right, all four boxes need to be populated before postback, and even then postback is not the problem as they can postback individually. The problem is checking the date and time against the database.

I am now thinking it might be easier to make the gridview visible = false and use button to check availability. That way I can add event handler to the button. Too much work, and makes the form look ugly, but I am struggling doing it this way. You think that is better way of handling it?
Andy Lanng 28-May-15 5:57am    
I do. It will fit with the users expected experience a lot better. It can get annoying as a user if a form keeps posting back unless you specifically tell it to. People expect buttons to post data.

Your comment that I am responding to is MUCH clearer.


To check the database, you need to find overlaps. I have had fun with this but it's really quite simple.


I'll add it to the solution


Awoldeselassie 28-May-15 6:16am    
Ok awesome thanks, so is that an event handler for a button, or addition to the js postback function?
Andy Lanng 28-May-15 6:18am    
both - either. I would use the button event handler.

It is obviously incomplete. You have to set up the sql connection and add the parameters, but the query will do the trick
Awoldeselassie 28-May-15 6:24am    
yeah off course,.. thank you so much for your help Andy, I should add a button and see if it works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900