Can't change Bootstrap Datepicker format - bootstrap-datepicker

I want to have a datepicker with this format: dd/mm/yyyy. When I set the default date in Javascript I have my date with the good format. But when I click it to choose an another date the format is reset at: mm/dd/yyyy
HTML code:
<div class="input-group" id="datepicker">
<input type="text" class="form-control" data-bind="value: startDate, event: {change: savePerishableDate}" id="date"/>
</div>
Javascript datepicker:
<script type="text/javascript">
$(document).ready(function() {
$("#date").datepicker({
format: "dd/mm/yyyy",
language: "fr",
changeMonth: true,
changeYear: true
});
});
</script>
Javascript:
self.startDate(new Date)
tmpDate = new Date
self.startDate(tmpDate.toLocaleDateString("fr-FR"))
$('#date').datepicker 'setDate', self.startDate()

I think you are using old version of bootstrap datepicker,Using this latest you can set dateformat as you want
$(document).ready(function() {
$("#date").datepicker({
format: "dd/mm/yyyy",
language: "fr",
changeMonth: true,
changeYear: true
});
});
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<link type="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker3.min.css"/>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<div class="input-group" id="datepicker">
<input type="text" class="form-control" data-bind="value: startDate, event: {change: savePerishableDate}" id="date"/>
</div>

Related

Input type="date" not working in certain browsers

I've got an ASP.NET web forms page where I'm setting the value of an input (type="date") in code-behind and it displays correctly in IE 11 and Firefox 51, but in Chrome 56 and Opera 43, it merely displays the mm/dd/yyyy placeholder. Below is my code.
The ASPX...
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Test Page</title>
<link rel="stylesheet" type="text/css" href="../Content/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" />
<link rel="stylesheet" type="text/css" href="../Content/formValidation.min.css" />
<script type="text/javascript" src="../Scripts/jquery-2.2.3.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery-ui-1.11.4.min.js"></script>
<script type="text/javascript" src="../Scripts/bootstrap.min.js"></script>
<script type="text/javascript" src="//momentjs.com/downloads/moment.min.js"></script>
<script type="text/javascript" src="../Scripts/formValidation.popular.js"></script>
<script type="text/javascript" src="../Scripts/framework/bootstrap.js"></script>
<script type="text/javascript" src="../Scripts/bootbox.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<label for="txtChildDOB1" class="control-label col-xs-1">DOB *</label>
<div class="col-xs-1">
<input type="date" class="form-control" style="position: absolute; z-index: 999" id="txtChildDOB1" name="txtChildDOB1" value="" runat="server" />
</div>
</div>
</form>
</body>
And the code-behind...
protected void Page_Load(object sender, EventArgs e)
{
this.txtChildDOB1.Value = new DateTime(2002, 3, 19).ToShortDateString();
}
Before answering your question. There are two types input date in asp.net, server side and client side.
On the client side the input date like this:
<input type="date" id="datePicker" class="form-control" runat="server" />
On the server side the input date is like this:
<asp:TextBox TextMode="Date" ID="datePicker" class="form-control" runat="server" ></asp:TextBox>
You can use both of them for your purpose but before that, you need set your time exactly input date want it.
input date only accepts these format types yyyy-MM-dd or dd-MM-yyyy.
you must choose - as date separators and certainly 4 digits on the year, two digits on month and two digits on days.
to clear my answer I wrote my code here:
string makeDate = DateTime.Now.ToString("yyyy") +
"-" + DateTime.Now.ToString("MM") +
"-" + DateTime.Now.ToString("dd");
Or in custom dates, you can use this method:
public static string makeDate8(DateTime dt)
{
string date = dt.Year.ToString().PadLeft(4,'0') + "-" + dt.Month.ToString().PadLeft(2, '0') + "-" + dt.Day.ToString().PadLeft(2, '0');
return date;
}
Now you can set input value like this:
On client side :
datePicker.Value = today;
On server side:
datePicker.Text= today;

Ajax.BeginForm OnBegin not firing (ASP.NET MVC5 application)

I've been trying to get a Javascript function to fire when my page's AJAX processing is triggered without success. Most of my time has been spent trying to get the OnBegin AjaxOption value working, but I have spent time with the global JQuery .ajaxSend handler as well (which I believe might not be compatible with ASP.NET MVC), both without success.
I've reduced my page to something pretty simple. My Index.cshtml for the project is:
#using SalePointWebItemInquiry.Controllers
#using System.Web.Mvc.Ajax
#{
ViewBag.Title = "Home Page";
}
#Scripts.Render("~/bundles/jquery", "~/bundles/jqueryval")
<script type="text/javascript">
function TestFunction() {
alert("Calling testfunction.");
}
</script>
#using (Ajax.BeginForm("PressButton",
"Home",
new AjaxOptions
{
OnBegin = "TestFunction"
}))
{
<div>
<input class="btn btn-default" type="submit" id="1" name="buttonType" value="Enter" />
</div>
}
The page that this renders is:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page - My ASP.NET Application</title>
<link href="/Content/bootstrap.css" rel="stylesheet" />
<link href="/Content/site.css" rel="stylesheet" />
<script src="/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div class="container body-content bordered">
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/jquery.validate.js"></script>
<script src="/Scripts/jquery.validate.unobtrusive.js"></script>
<script type="text/javascript">
function TestFunction() {
alert("Calling testfunction.");
}
</script>
<form action="/Home/PressButton" data-ajax="true" data-ajax-begin="TestFunction" id="form0" method="post">
<div>
<input class="btn btn-default" type="submit" id="1" name="buttonType" value="Enter" />
</div>
</form>
<footer>
<div class="row">
<div class="col-md-5">
<p>© 2015 - Application</p>
</div>
<div class="col-md-7 text-right">
Version: Put version here
</div>
</div>
</footer>
</div>
<script src="/Scripts/jquery-1.10.2.js"></script>
<script src="/Scripts/bootstrap.js"></script>
<script src="/Scripts/respond.js"></script>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Internet Explorer","requestId":"d2eef5342ac64f3db1545856102f4a6f"}
</script>
<script type="text/javascript" src="http://localhost:53954/45ae7332668647b5a3795d87cd6ba1a9/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
Does anyone have any thoughts on why the TestFunction() Javascript function isn't firing when I click the ENTER button?
Solution:
Thanks for the help :). I wasn't properly referencing jquery.unobtrusive-ajax.js. The direct solution to my problem was to use Nuget to retrieve a Javascript package that wasn't local using this command at Nuget console:
Install-Package Microsoft.jQuery.Unobtrusive.Ajax
Since I didn't have jquery.unobtrusive-ajax.js locally, all of my attempts to reference it locally failed. I may have tried a CDN reference once or twice, but that never worked out and I had a lot of theories as to the cause of the problem at the time.
I didn't even have to change the code. This code in BundleConfig.cs automatically included the file once I had retrieved it:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.unobtrusive*",
"~/Scripts/jquery.validate*"));
You have duplicated
<script src="/Scripts/jquery-1.10.2.js"></script>
Remove the second one and then you also need to add jquery.unobtrusive-ajax.js in order for Ajax.BeginForm() to work (other wise you just making a normal submit - and therefore TestFunction() will never be called)
Looks like you are loading your script before JQuery can load. Scripts need to load at the bottom of the page just before the body tag and after JQuery loads.
Please try
#section Scripts
{
<script type="text/javascript">
function TestFunction() {
alert("Calling testfunction.");
}
</script>
}
This will place the script after JQuery loads.

Cant not run pikaday.js in WebForm with site master

In the webform with sitemaster, i couldnt use the function of pikaday,but in the new webform i created for testing without sitemaster i could use the function. How do i solve the problems?
With sitemaster:
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<link href="css/pikaday.css" rel="stylesheet" />
<link href="css/theme.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<script src="moment.js"></script>
<script src="pikaday.js"></script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<script>
var picker = new Pikaday({
field: document.getElementById('TextBox1'),
firstDay: 1,
format: 'YYYY-MM-DD',
minDate: new Date('2015-01-01'),
maxDate: new Date('2050-12-31'),
yearRange: [2015, 2050],
numberOfMonths: 1,
});
</script> </asp:Content>
Without sitemaster:
<body>
<form id="form1" runat="server">
<div>
<link href="css/pikaday.css" rel="stylesheet" />
<link href="css/site.css" rel="stylesheet" />
<link href="css/theme.css" rel="stylesheet" />
<script src="moment.js"></script>
<script src="pikaday.js"></script>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<script>
var picker = new Pikaday({
field: document.getElementById('TextBox1'),
firstDay: 1,
format: 'YYYY-MM-DD',
minDate: new Date('2015-01-01'),
maxDate: new Date('2050-12-31'),
yearRange: [2015, 2050],
numberOfMonths: 1,
});
</script>
</div>
</form> </body>
i have exactly the same problem and after doing research I found out the following: it seems that there's a css property that needs to be changed on the pikaday.css file, specifically on line 35:
.pika-single.is-bound{position: absolute !important}
Just add the !important next to the "position: absolute" statement.
Also in your javascript you have to reference the textbox like this:
var $txtDate = $("[id$=Client_Id_Generated_by_Aspx_page]")
This selector will find the reference of the textbox ID that you assigned on the ASPX page on your IDE (for example Visual Studio) at the end, according to the "$=" operator. This applies for .Net Frameworks 3.5 or less since it seems that on .Net Framework 4.0 or more there is a way to assign a ClientID on the server side for you to use it on the frontEnd (.js files for example).
I hope this will help you.
Cheers...

_doPostBack added twice. one by manual and one by gridview in asp.net

I have a page where i am rendering GridView. In GridView i have a CheckBox and once you check, uncheck, Jquery Dialog appears and i need a postback event on that dialog. so i am adding a manual postback on that page. everything is fine till gridview has only 1 page. as soon as gridview has more records, it add a _doPostBack method on page. so i now have a 2 _doPostBack method on page and nothing works because of this.
How can i define in my page that i do not want to add any _doPostBack method by any controls because i have already defined it manually?
Here is my HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>Store Management</title>
<script src="/js/jquery-1.9.1.min.js" type="text/javascript"></script>
<script src="/js/jquery-ui.min-1.10.1.js" type="text/javascript"></script>
<link href="/assets/styles/StorePortal/style-22012013.css" rel="stylesheet"
type="text/css" />
<link href="/assets/styles/StorePortal/jquery-ui.css" rel="stylesheet"
type="text/css" />
<link href="../App_Themes/AbleCommerce/ComponentArt.css" rel="stylesheet"
type="text/css" />
<link href="../App_Themes/AbleCommerce/print.css" rel="stylesheet" type=
"text/css" />
<link href="../App_Themes/AbleCommerce/style.css" rel="stylesheet" type=
"text/css" />
<link href="../App_Themes/AbleCommerce/webparts.css" rel="stylesheet" type=
"text/css" />
</head>
<body>
<form action=
"portal.aspx?q=bbaae796d951c95311f5ec3e599784079c6093ee&q1=COV" id=
"form1" method="post" name="form1">
<div>
<input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" value=
"" /> <input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type=
"hidden" value="" />
</div><script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<div>
<input id="__VIEWSTATEENCRYPTED" name="__VIEWSTATEENCRYPTED" type=
"hidden" value="" />
</div>
<script type="text/javascript"> --> this one is added by me manually
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</script>
<div>
<input id="__EVENTTARGET" name="__EVENTTARGET" type="hidden" value=
"" /> <input id="__EVENTARGUMENT" name="__EVENTARGUMENT" type=
"hidden" value="" /> /// sOME CODE HERE
</div>
--> this one is added by me manually
</form>
</body>
</html>
Now, if i remove the _doPostBack added by me, gridview paging works. BUT when i have only 1 page in gridview, my event in jquery dialog does not work because it cant find _doPostBack.
ANSWER:
I have removed the _doPostBack() added by me and instead added this on my Page_Load() which adds a _doPostBack() even if there are no asp.net controls which requires this method.
ClientScript.GetPostBackEventReference(this, string.Empty);
BIG THANKS TO #Tim Schmelter for pointing to that LINK
I've had a similar problem, when trying to popup a dialog confirming a record deletion. This is the code I've used (jQuery UI dialog) and it works perfectly.
Don't add a new doPostBack. Instead, try to follow this idea. You can adapt it to fit your requirements:
// When the user tries to delete a record, show a confirmation.
$(".confirmationButton").click(function(e) {
// Stop the PostBack
e.preventDefault();
$(".confirmationDialog").dialog("open");
});
$(".confirmationDialog").dialog({
buttons: {
"No": function() {
$(this).dialog("close");
},
"Yes": function() {
$(this).dialog("close");
// User confirmed deletion, so carry on with the PostBack.
eval($("#ctl00_MainContentPlaceHolder_hdnBtnPostBack").val());
}
}
});
Somewhere on your code (when checking the page source), you'll find something like this:
<input id="ctl00_MainContentPlaceHolder_hdnBtnPostBack" type="hidden" value="__doPostBack('ctl00$MainContentPlaceHolder$btnDelete','')" name="ctl00$MainContentPlaceHolder$hdnBtnPostBack">
That's the object you will be using with eval().

Maximum number of words plugin issue on ckeditor

I am trying to implement a word count feature on some of my text areas that have been converted to wswgs' using ckeditor tutorial found here: http://www.n7studios.co.uk/2010/03/01/ckeditor-word-count-plugin/. This tutorial uses a custom plugin to count words for ckeditor. I get the correct word count but I am not able to see the color change to red when I surpass the maximum number of words allowed. I have used firebug to see how my text area looks like after it has been transformed into a wswg using ckeditor but I don't see anything strange. Here is my code:
<asp:Content ID="Content3" ContentPlaceHolderID="JSContent" runat="server">
<script src="<%: Url.Content("~/Scripts/jquery-1.7.min.js") %>" type="text/javascript"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.combobox.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/ckeditor/ckeditor.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/ckeditor/adapters/jquery.js") %>"></script> <%-- Must be linked last --%>
<script type="text/javascript">
$(document).ready(function () {
$(".cke_editor").ckeditor({
toolbar: 'custom'
});
});
<div class="form-row form-row-inline">
<%: Html.TextAreaFor(model => model.AssignmentText, new { #name = "table", #class = "cke_editor", #style = "display:none", #onchange = "setDirty(true)" })%>
<input name="tableWordCount" type="hidden" value="10" />
</div>
Here is the screen:
Any help greatly appreciated.
I finally figured it out. Html.TextAreaFor() asp.net mvc helper method takes, as a parameter, an object of html attributes as shown on the question code above. I originally had used the "name" property for the textarea so that it matches the "name property" on the ckeditor word count plugin. I had to instead use the "id" property.
ckeditor sample:
<html>
<head>
<title>CKEditor Word Count Demonstration</title>
<script type="text/javascript" src="jquery/jquery.1.4.min.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
</head>
<body>
<form action="#" method="POST">
<p>
<label for="content">Content with 250 word limit</label>
<textarea name="content" class="ckeditor">Lorem Ipsum.</textarea>
<input type="hidden" name="contentWordCount" value="250" />
</p>
</form>
</body>
</html>
my new fixed code:
<asp:Content ID="Content3" ContentPlaceHolderID="JSContent" runat="server">
<script src="<%: Url.Content("~/Scripts/jquery-1.7.min.js") %>" type="text/javascript"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/jquery.combobox.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/ckeditor/ckeditor.js") %>"></script>
<script type="text/javascript" src="<%: Url.Content("~/Scripts/ckeditor/adapters/jquery.js") %>"></script> <%-- Must be linked last --%>
<script type="text/javascript">
$(document).ready(function () {
$(".cke_editor").ckeditor({
toolbar: 'custom'
});
});
<div class="form-row form-row-inline">
<%: Html.TextAreaFor(model => model.AssignmentText, new { #id = "taxta", #class = "cke_editor", #style = "display:none", #onchange = "setDirty(true)" })%>
<input name="taxtaWordCount" type="hidden" value="10" />
</div>
So, the only thing I had to change is the "id" property. I hope this helps others. Again, Firebug was very helpful in delving through the html DOM and looking at how everything is rendered on the browser.

Resources