I’m working with Visual Studio 2017 and Bootstrap 4.5.0. Under a master page, I have a Payment page that contains a txtName textbox, a txtPin textbox and btnPay button. To this, I want to add a DateTimePicker. When btnPay is clicked, values of txtName, txtPin
and DateTimePicker should be saved to the database.
To implement this idea, I installed
Bootstrap.v3.Datetimepicker.CSS 4.17.45 by eonasdan via NuGet Package Manager of Visual Studio. After successful installation, I added relevant js and css references (as defined
here) to the master page and added code to Payment page (as defined
here) to include the DateTimePicker. However, only the textbox of the DateTimePicker is appearing; nothing else is happening.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Payment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
divAlert.InnerText = string.Empty;
divAlert.Attributes["class"] = "d-none";
}
}
protected void PinValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
try
{
// Test if txtPin is not blank, doesn't contain alphanumeric value and contains exactly six digits.
args.IsValid = System.Text.RegularExpressions.Regex.IsMatch(args.Value, "^[0-9]{6}$");
}
catch (Exception ex)
{ args.IsValid = false; }
}
protected void btnPay_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
// Write code for payment.
// If payment is successful...
divAlert.Attributes["class"] = "alert alert-success";
divAlert.InnerText = "Payment completed successfully.";
}
}
}
Am I doing anything wrong? Or is it that this version of DateTimePicker doesn’t work with my version of Bootstrap? Please help me in resolving the issue.
There are several issues with the code. The standard bootstrap datepicker must be applied to an text input not a div. Inputs must have a name otherwise the browser does not submit the input. ASP.NET Validator controls work with server controls and there
is no validator for the date input.
There are several bootstrap 4 date pickers which can be found with a Google search.
The DatePicker of the link you shared is good, but it doesn't seem to have a time component. My project requires a control that has both date and time components attached together, hence, I chose eonasdan DateTimePicker. However, I learnt that the eonasdan
DateTimePicker is for Bootstrap version 3. It doesn't work with the later versions of Bootstrap. Therefore I shifted to Tempus Dominus DateTimePicker, which is the updated successor of eonasdan DateTimePicker
that works with Bootstrap 4 or later.
However, while using Tempus Dominus, though the DateTimePicker is appearing and its functions are working properly on the page, no icons of the DateTimePicker or the Calendar control are appearing. See the images of
date panel and
time panel. The points circled in red are where the icons are supposed to be.
As I couldn't find any NuGet installation package for Tempus Dominus, I downloaded
moment.min.js, tempusdominus-bootstrap-4.min.js and tempusdominus-bootstrap-4.min.css separately from URLs mentioned in the CDNJS section of
this page. I added them to the scripts and content folders of my project, added the references of
moment.min.js, tempusdominus-bootstrap-4.min.js and tempusdominus-bootstrap-4.min.css to the master file. Then, in the Payment page I added the javascript function to initialise the DateTimePicker. Still, no icon of the DateTimePicker
control can be seen.
The problem still persists even if I use the full versions of moment.js, tempusdominus-bootstrap-4.js and tempusdominus-bootstrap-4.css.
Seems like the documentation of Tempus Dominus has curiously missed significant reference of FontAwesome. Hence, had to go through a lot of head scratching to integrate FontAwesome to .Net and make it work with Tempus Dominus DateTimePicker.
Anyway, after up & running, now I face two quests. One is, how do I set the value of the DateTimePicker from code-behind while fetching data values from the database programmatically? You've already shown how to get the value
from the DateTimePicker:
string date = Request["datetimeinput"];
But how to do the reverse?
The second problem is a bit weird. While initialising the DateTimePicker in the aspx page, I'm setting the minDate and maxDate options:
While running the page, when I pop up the calendar control of the DateTimePicker and try to select any date beyond the minDate and maxDate range with the mouse, the control correctly disables the selection and stops me from selecting any invalid date. However,
if I write any invalid date beyond the range by using keyboard, the control isn't validating it, therefore allowing invalid date to pass to the server-side. How do I stop it from doing so? Kindly assist.
None
0 Points
15 Posts
How to add Bootstrap DateTimePicker to ASP.Net application?
Jun 21, 2020 02:41 PM|priyamtheone|LINK
I’m working with Visual Studio 2017 and Bootstrap 4.5.0. Under a master page, I have a Payment page that contains a txtName textbox, a txtPin textbox and btnPay button. To this, I want to add a DateTimePicker. When btnPay is clicked, values of txtName, txtPin and DateTimePicker should be saved to the database.
To implement this idea, I installed Bootstrap.v3.Datetimepicker.CSS 4.17.45 by eonasdan via NuGet Package Manager of Visual Studio. After successful installation, I added relevant js and css references (as defined here) to the master page and added code to Payment page (as defined here) to include the DateTimePicker. However, only the textbox of the DateTimePicker is appearing; nothing else is happening.
Image of my Payment page is here.
Master Page aspx:
Payment Page aspx:
Payment Page Code-behind:
Am I doing anything wrong? Or is it that this version of DateTimePicker doesn’t work with my version of Bootstrap? Please help me in resolving the issue.
Regards
All-Star
53051 Points
23644 Posts
Re: How to add Bootstrap DateTimePicker to ASP.Net application?
Jun 21, 2020 03:06 PM|mgebhard|LINK
There are several issues with the code. The standard bootstrap datepicker must be applied to an text input not a div. Inputs must have a name otherwise the browser does not submit the input. ASP.NET Validator controls work with server controls and there is no validator for the date input.
There are several bootstrap 4 date pickers which can be found with a Google search.
https://bootstrap-datepicker.readthedocs.io/en/latest/
None
0 Points
15 Posts
Re: How to add Bootstrap DateTimePicker to ASP.Net application?
Jun 21, 2020 04:56 PM|priyamtheone|LINK
Hi mgebhard,
Thanks for your reply.
The DatePicker of the link you shared is good, but it doesn't seem to have a time component. My project requires a control that has both date and time components attached together, hence, I chose eonasdan DateTimePicker. However, I learnt that the eonasdan DateTimePicker is for Bootstrap version 3. It doesn't work with the later versions of Bootstrap. Therefore I shifted to Tempus Dominus DateTimePicker, which is the updated successor of eonasdan DateTimePicker that works with Bootstrap 4 or later.
However, while using Tempus Dominus, though the DateTimePicker is appearing and its functions are working properly on the page, no icons of the DateTimePicker or the Calendar control are appearing. See the images of date panel and time panel. The points circled in red are where the icons are supposed to be.
As I couldn't find any NuGet installation package for Tempus Dominus, I downloaded moment.min.js, tempusdominus-bootstrap-4.min.js and tempusdominus-bootstrap-4.min.css separately from URLs mentioned in the CDNJS section of this page. I added them to the scripts and content folders of my project, added the references of moment.min.js, tempusdominus-bootstrap-4.min.js and tempusdominus-bootstrap-4.min.css to the master file. Then, in the Payment page I added the javascript function to initialise the DateTimePicker. Still, no icon of the DateTimePicker control can be seen.
The problem still persists even if I use the full versions of moment.js, tempusdominus-bootstrap-4.js and tempusdominus-bootstrap-4.css.
Where am I doing wrong this time?
All-Star
53051 Points
23644 Posts
Re: How to add Bootstrap DateTimePicker to ASP.Net application?
Jun 21, 2020 06:45 PM|mgebhard|LINK
So you question is how to use the Tempus Dominus library. The docs are very clear you just need to add a name attribute to the text input.
<div class="container"> <div class="row"> <div class="col-sm-6"> <div class="form-group"> <div class="input-group date" id="datetimepicker1" data-target-input="nearest"> <input type="text" name="datetimeinput" class="form-control datetimepicker-input" data-target="#datetimepicker1"/> <div class="input-group-append" data-target="#datetimepicker1" data-toggle="datetimepicker"> <div class="input-group-text"><i class="fa fa-calendar"></i></div> </div> </div> </div> </div> <script type="text/javascript"> $(function () { $('#datetimepicker1').datetimepicker(); }); </script> </div> </div>
You can get the value using standard syntax.
Or use a standard TextBox server control. The assignment will change to...
$('#<%=datetimepicker1.ClientID%>').datetimepicker();
Lastly, fa-calendar is a font awesome icon.
https://fontawesome.com/v4.7.0/icon/calendar
All-Star
58224 Points
15671 Posts
Re: How to add Bootstrap DateTimePicker to ASP.Net application?
Jun 21, 2020 08:00 PM|bruce (sqlwork.com)|LINK
Bootstrap 4 does not include an icon library. You must pick one and add to the project. See
https://getbootstrap.com/docs/4.0/extend/icons/
None
0 Points
15 Posts
Re: How to add Bootstrap DateTimePicker to ASP.Net application?
Jun 28, 2020 03:54 PM|priyamtheone|LINK
Hi mgebhard,
Seems like the documentation of Tempus Dominus has curiously missed significant reference of FontAwesome. Hence, had to go through a lot of head scratching to integrate FontAwesome to .Net and make it work with Tempus Dominus DateTimePicker.
Anyway, after up & running, now I face two quests. One is, how do I set the value of the DateTimePicker from code-behind while fetching data values from the database programmatically? You've already shown how to get the value from the DateTimePicker:
But how to do the reverse?
The second problem is a bit weird. While initialising the DateTimePicker in the aspx page, I'm setting the minDate and maxDate options:
While running the page, when I pop up the calendar control of the DateTimePicker and try to select any date beyond the minDate and maxDate range with the mouse, the control correctly disables the selection and stops me from selecting any invalid date. However, if I write any invalid date beyond the range by using keyboard, the control isn't validating it, therefore allowing invalid date to pass to the server-side. How do I stop it from doing so? Kindly assist.