The current context for id does not exist - c#

How can I get hold a session from date picker?
For all my textbox, I hold it using :
Session["location"] = DropDownList1.SelectedItem.Text;
Session["time"] = DropDownList2.SelectedItem.Text;
Session["day"] = DropDownList3.SelectedItem.Text;
Session["IsChauffeurUsed"] = DropDownList4.SelectedItem.Text;
But for my text box date picker, when I write the code
Session["date"] = datepicker.Text;
It gives me an error, the current context does not exist.
The date picker text box is :
<div class="demo"><p>Date: <input type="text" id="datepicker"></p></div>
Hope you can help.
Thanks.

Set runat=server attribute to convert html tag to Html server control.
<input type="text" runat="server" id="datepicker">
and use value attribute because it is now ASP.NET Web Server control.
Session["date"] = datepicker.Value;
Edit: This works perfectly on my side.
<head runat="server">
<title></title>
<link rel="Stylesheet" type="text/css" href="http://code.jquery.com/ui/jquery-ui-git.css" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
<script type="text/javascript">
$(function () {
$("#datePicker").datepicker();
$("#<%=TextBox1.ClientID %>").datepicker();
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="datePicker" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
</form>
</body>

Related

display only 5 days in datepicker

I have used a jquery datepicker in my code for user input.But i have a requirement where i have to display only 5 days from a specific date in my jquery datepicker excluding weekends.Could you please assist as i am new to jquery?
Regards,
Miru
I have fetched the 5 days from dat_eta in to an array in a hidden variable.Now i need to know how to enable only these days in the datepicker.
Please find the code below.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="JAVASCRIPT_CALENDAR_TESTING.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="../Content/themes/jquery-ui.css" rel="stylesheet" />
<script src="../Scripts/jquery-1.8.2.js"></script>
<script src="../Scripts/jquery-ui-1.8.24.js"></script>
<script>
$(function() {
$("#datepicker").datepicker();
});
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Request date:</td>
<td><input type="text" id="datepicker" /></td>
<%
int scm_date;
scm_date = (9 / 24 + 1);
DateTime dat_eta = DateTime.Now;
string result;
dat_eta = dat_eta.AddDays(scm_date);
result = dat_eta.ToString("MM:dd:yyyy");
string[] array = new string[6];
for (int i = 0; i <= 5; i++) {
if (i == 0) {} else {
dat_eta = dat_eta.AddDays(1);
}
result = dat_eta.ToString("MM:dd:yyyy");
array[i] = result;
}
hiddenvalue.Value = string.Join(",", array);
%>
<td><input type="hidden" id="hiddenvalue" name="hiddenvalue" runat="server" /></td>
</tr>
</table>
<script type="text/javascript">
</script>
</div>
</form>
</body>
</html>
You can use the beforeShowDay option.
/*
* let's assume your array is like the following one
*/
var datValues = ['03:11:2018','03:12:2018','03:13:2018'];
$("#datepicker").datepicker({
beforeShowDay: function(date) {
return [datValues.indexOf($.datepicker.formatDate('mm:dd:yy', date)) > -1];
}
});
<link href="https://code.jquery.com/ui/1.8.24/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.8.2.min.js"></script>
<script src="https://code.jquery.com/ui/1.8.24/jquery-ui.js"></script>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Request date:</td>
<td><input type="text" id="datepicker"/></td>
<td><input type="hidden" id="hiddenvalue" name="hiddenvalue" runat="server"/></td>
</tr>
</table>
</div>
</form>

Not able to set label text in asp.net master page controls from content page

I am trying to change the text of label in master page which is inside modal and from content page i am trying to set the text.
This is my master page:
<%# Master Language="C#" AutoEventWireup="true"
CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1 maximum-
scale=1, user-scalable=no" />
<link href="../Content/bootstrap.min.css" rel="stylesheet" />
<link href="../Style/StyleSheet.css" rel="stylesheet" />
<link href="../Content/font-awesome.min.css" rel="stylesheet" />
<link href="http://fonts.googleapis.com/css?
family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet"
type="text/css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/jquery-footable/0.1.0/css/footable.min.css" rel="stylesheet" type="text/css" />
<script src="../Scripts/jquery-1.9.1.min.js"></script>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<div id="header">
<form id="Form1" runat="server">
<header>
</header>
<div class="container-fluid" id="body">
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
<div class="modal fade" id="myModal">
<div class="modal-dialog">
<div class="modal-content" style="width: 400px;margin: 0 auto;">
<div class="modal-header" runat="server">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"><asp:Label ID="lblMasterMessage" runat="server" Text="Label"></asp:Label> </h4>
</div>
<div class="modal-body">
<asp:Label ID="lblMasterbodyMessage" runat="server" Text="gh" />
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<footer class="panel-footer white">
<div class="row">
</div>
</footer>
</form>
</div>
<script src="../Scripts/bootstrap.min.js"></script>
</body>
</html>
script in child page which opens modal:
<script type="text/javascript">
function openModal(message) {
$('#myModal').modal('show');
}
</script>
code behind child page calling the script and changing text of label in modalpopup
ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { openModal(); });", true);
Label lblMaster = (Label)Master.FindControl("lblMasterbodyMessage");
string message = "something";
lblMaster.Text = message;
i also tried the second approach by setting properties in master page but text in the popup is not getting updated. any help will be appreciated
Best approach will be to provide a get/set property in your master page that will accept value and assign it to the label value property.
Master Page
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string MasterBodyMessage // you can use some meaningfull name over here.
{
get { return this.lblMasterbodyMessage.Text; }
set { this.lblMasterbodyMessage.Text = value; }
}
}
Child Page
ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "$(function() { openModal(); });", true);
var master = this.Master as MasterPage;
if (master != null)
{
master.MasterBodyMessage = "Some Meaningful Message!!!";
}
Else you can do some work around using javascript/jQuery to add message into label in master page. For this you will need to amend following changes.
HTML Aspx Page
<div class="modal-content" style="width: 400px;margin: 0 auto;">
<div class="modal-header">
<button aria-label="Close" class="close" data-dismiss="modal" type="button"><span aria-hidden="true">×</span></button>
<h4 class="modal-title"><label id="lblMasterMessage"></label></h4>
</div>
<div class="modal-body">
<label id="lblMasterbodyMessage"></label>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal" type="button">Close</button>
</div>
</div>
Code Behind Page
string message = "Some Meaningful Message!!!";
ScriptManager.RegisterStartupScript(this, this.GetType(), "LaunchServerSide", "openModal('"+ message +"');", true);
jQuery Script
<script type="text/javascript">
function openModal(message) {
$('#myModal').modal('show');
$('#lblMasterbodyMessage').html(message);
}
</script>
Specify your master page class name in TypeName attribute of MasterType directive.
<%# MasterType TypeName="MasterPageClassName" %>
Now you can access your public properties as this.Master.propertyname
Also try by moving your code from page load event to PreRenderComplete event of page.
That'll solve you're problem

button not disabling after click

I am using this post to disable my buttons on click:
Prevent multiple form submits in MVC 3 with validation
This is my page source after it is rendered:
<!DOCTYPE html>
<html>
<head>
<title>Do not bookmark this page.</title>
<link href="/Content/reset.css" rel="stylesheet" type="text/css" />
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="/Content/styer.css?t=335" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/Views/main.js" type="text/javascript"></script>
<link rel="shortcut icon" href="/Content/favicon.ico" type="image/ico" />
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<script language="javascript" type="text/javascript">
$(function () {
$('form').submit(function () {
if ($(this).valid()) {
$('input[type="submit"]').attr('disabled', 'disabled');
}
});
});
</script>
</head>
<body>
<div class="page">
<div id="header">
<div id="headerimg"></div>
<div id="logindisplay">
[ Log On ]
</div>
<div id="menucontainer">
<ul id="menu">
<li>Home</li>
<li>Logon</li>
<li>Register</li>
<li>Contact Us</li>
<li>News</li>
</ul>
</div>
<div id="main">
<h2>Forgot Username</h2>
<p>
Please provide the email address on your web account. Once validated, your username will be sent to your email address for your records.
</p>
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/Account/ForgotUsername" method="post"> <div>
<fieldset>
<legend>Email address</legend>
<div class="editor-label">
<label for="EmailAddress">Email</label>
</div>
<div class="editor-field focus">
<input class="GenericTextBox" data-val="true" data-val-length="The Email must be at least 7 characters long." data-val-length-max="100" data-val-length-min="7" id="EmailAddress" name="EmailAddress" type="text" value="" />
<span class="field-validation-valid" data-valmsg-for="EmailAddress" data-valmsg-replace="true"></span>
</div>
<p>
<input id="btn" class="makePaymentInput" type="submit" value="Submit"/>
</p>
</fieldset>
</div>
</form>
<div style="clear: both;"></div>
</div>
<div id="footer">
</div>
</div>
</div>
<!-- Start of StatCounter Code for Default Guide -->
<script type="text/javascript">
var sc_project = 9150358;
var sc_invisible = 1;
var sc_security = "1af31df9";
var scJsHost = (("https:" == document.location.protocol) ? "https://secure." : "http://www.");
document.write("<sc" + "ript type='text/javascript' src='" + scJsHost + "statcounter.com/counter/counter.js'></" + "script>");
</script>
<noscript>
<div class="statcounter"><a title="web analytics" href="http://statcounter.com/" target="_blank"><img class="statcounter" src="http://c.statcounter.com/9150358/0/1af31df9/1/" alt="web analytics"></a></div>
</noscript>
<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
{"appName":"Internet Explorer"}
</script>
<script type="text/javascript" src="http://localhost:25759/610299fe88d74cee8d0267b4fc859da0/browserLink" async="async"></script>
<!-- End Browser Link -->
</body>
</html>
This works on most forms but this one, which is the main reason I used this code.
Anyone see why this one is not working?
Instead of writing this :-
$('input[type="submit"]').attr('disabled', 'disabled');
You should write this :-
$('input[type="submit"]').prop('disabled','true');

How to add in session in the javascript code?

Before I add in this part of session code, it will redirect me to BlogEntry.aspx if my username and password is correct. After I add in the session code, when I clicked the button nothing happening. Can you help me convert my session code to work in the javascript? I need to code it in the javascript and not in the code behind because I am using a html button. Because I want the design of the button, I need to do this. Unless you tell me there's a way to do it in the code behind. After login in login.aspx. It will redirect to logined.aspx and display welcome admin in the label.
Login.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
</style>
<link rel="stylesheet" type="text/css" href="stylesheets/loginstyle.css" />
<script language="javascript" type="text/javascript">
// <![CDATA[
function Button1_onclick() {
if (document.getElementById('txtUserName').value == "Admin" && document.getElementById('txtPassword').value == "123") {
//After add in session it cannot redirect START HERE
Session.Add("Username", txtUserName.Value);
Session.Add("Password", txtPassword.Value);
FormsAuthentication.SetAuthCookie(txtUserName.Value, true);
window.location.assign("BlogEntry.aspx")
//After add in session it cannot redirect END HERE
}
else
{
document.getElementById("<%=lblError.ClientID%>").innerHTML = "Name can't be blank!";
}
}
// ]]>
</script>
</head>
<body>
<div id="wrapper">
<form name="login-form" class="login-form" action="" method="post">
<div class="header">
<h1>Login Form</h1>
<span>Fill out the form below to login to my super awesome imaginary control panel.</span>
</div>
<div class="content">
<input name="username" type="text" class="input username" placeholder="Username" runat="server" id="txtUserName" />
<div class="user-icon"></div>
<input name="password" type="password" class="input password" placeholder="Password" runat="server" id="txtPassword" />
<div class="pass-icon"></div>
</div>
<div class="footer">
<input type="button" name="submit" value="Login" class="button" runat="server" id="Button1" önserverclick="Button1_Click" onclick="return Button1_onclick()" />
<asp:Label ID="lblError" runat="server" Text=""></asp:Label>
</div>
</form>
</div>
<div class="gradient"></div>
</body>
</html>
Logined.aspx
protected void Page_Load(object sender, EventArgs e)
{
//Logout.Visible = false;
string memName = (String)Session["UserName"];
lblUsername.Text = String.Concat("Welcome Guest!");
if (Session["Username"] != null && Session["Username"] != String.Empty)
{
lblUsername.Text = "Welcome, " + memName + "!";
}
}
For #Sangram
You can use html session storage or localStorage to store your key values. this will not need any server side code. The info will be there even if the user close the browser.
for browser compatibility: http://www.html5rocks.com/en/features/storage
for example: http://coding.smashingmagazine.com/2010/10/11/local-storage-and-how-to-use-it/

Export HTML table to MS Excel Format Using jQuery

I'm trying to follow this example. To export an HTML table to MS Excel format using jQuery.
Here's my .aspx:
<head runat="server">
<title></title>
<script src="Scripts/jQuery.1.8.3.js" type="text/javascript"></script>
<script src="Scripts/JScript2.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Table ID="tbl" runat="server">
</asp:Table>
<input type="button" id="btnExport" value=" Export Table data into Excel " />
</div>
</form>
</body>
The .js (JScript2.js):
$("#btnExport").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('#tbl').html());
e.preventDefault();
});
... And the codebehind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class JQuery_Export_To_Excel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "AAA";
tr.Cells.Add(tc);
tc = new TableCell();
tc.Text = "BBB";
tr.Cells.Add(tc);
tbl.Rows.Add(tr);
}
}
Generated page source:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title>
<script src="Scripts/jQuery.1.8.3.js" type="text/javascript"></script>
<script src="Scripts/JScript2.js" type="text/javascript"></script>
</head>
<body>
<form method="post" action="JQuery_Export_To_Excel.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2NTA0OTEwODdkZKr9kRtjn1C5sAo2woCwfF/8uHOVcyNi1bu4OtVBNKlS" />
</div>
<div class="aspNetHidden">
<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAIdFUHUqRwMmhFieKB52uC8avDSflAS9b8PVcR1BxTTBqeDRyg6lH5NKPWh6Jt5ee2zX+bYNkguHBdZjCzKvoJa" />
</div>
<div>
<table id="tbl">
<tr>
<td>AAA</td><td>BBB</td>
</tr>
</table>
<input name="btnExport" type="button" id="btnExport" value=" Export Table data into Excel " />
</div>
</form>
</body>
</html>
I don't know what I'm doing wrong, but when I click btnExport, nothing happens. How can I fix this problem?
put this in your form
<div>
<div id="container">
<asp:Table ID="tbl" runat="server">
</asp:Table>
</div>
<input type="button" id="btnExport" value=" Export Table data into Excel " />
</div>
Put this in the header section with script tag
$(document).ready(function () {
$("#btnExport").click(function (e) {
window.open('data:application/vnd.ms-excel,' + $('#container').html());
e.preventDefault();
});
});
I believe nothing is wrong with your code except that since your code is asp.net. you cannot
directly use $('#tbl') . You have to use $("#" +'<%=tbl.ClientID%>')

Categories

Resources