Hello guys I have this problem of getting lastGuestInsertedId from C# cookies in a code behind file to another web form that retrieve lastGuestInsertedId values by jquery and then show the value by alerting. Any ideas?
C# codes
HttpCookie GuestCookie = new HttpCookie("GuestCookie");
GuestCookie["Key"] = Convert.ToString(lastGuestInsertedId) ;
GuestCookie.Expires = DateTime.Now.AddDays(1d);
Response.Cookies.Add(GuestCookie);
JQuery codes
var guestkey = $.cookie("GuestCookie")
alert(guestkey);
$.cookie is not part of default jQuery. You need some jQuery plugin to do that.
To check if you actually have cookies on client side - alert(document.cookie).
Related
I am trying to get strings that I have put in my session variables to another aspx webform. I am getting it inside my JS script and I would like to know how to go about it. I have tried the following but it doesnt seem to work. Please anyone guide me a bit.
Home.aspx.cs
HttpContext.Current.Session["InstitutionID"] = BankID;
HttpContext.Current.Session["InstitutionName"] = txtBankName.Text.Trim();
Index.aspx
$(document).ready(function () {
var param1var ='<%= Session["InstitutionID"]%>';
var param2var = '<%= Session["InstitutionName"]%>';
console.log('param1', param1var);
console.log('param1', param2var);
........
})
Are you using System.Web.SessionState.HttpSessionState.Session object to get the session object data in the Javascript? That is how I use it in one of my WebForms projects. Hope it helps.
HttpContext.Current.Session[Constants.SessionStateVariables.LoginFailed] = false;
var loginIsFailed = '<%=Session[XYZProcet.Common.Constants.SessionStateVariables.LoginFailed]%>';
This question already has answers here:
Pass javascript variable to Codebehind
(2 answers)
Closed 7 years ago.
I'm developing a small app and I have a question about posting variables between WebPage.aspx and WebPage.aspx.cs .
I've got the variable from WebPage.aspx.cs to WebPage.aspx like this:
WebPage.aspx.cs code:
public string AdresKlienta = string.Empty;
public string AdresPizzerii = string.Empty;
WebPage.aspx code:
var origin2 = '<%= AdresPizzerii %>';
var destinationA = '<% = AdresKlienta %>';
On WebPage.aspx I use javascript function that contains variables that I use do some work and I create another variable that I would like to post back (WebPage.aspx -> WebPage.aspx.cs).
I've been searching on the Internet for the solution, but found nothing. I'll appreciate any help.
Posting back a variable simply involves putting it in a form element somewhere. So let's say you create a form element:
<input type="hidden" name="someElement" id="someElement" />
Then in your JavaScript code you would set the value of that element:
document.getElementById('someElement').value = someValue;
When the form containing that element posts to a server-side resource, that resource can then access that value:
var someValue = Request.Form["someElement"];
The point is that in order for server-side code to receive any values from client-side code, those values need to be included in a request to the server of some kind. Whether it's a form POST, a query string value on a GET request (from a redirect or clicking a link), an AJAX request in the background, etc. The client-side code needs to make a new request to the server-side code in order to send it that value.
You can use hidden field, using javascript you can enter a value to this field and can be accessed from your code behind also.
I have created an ASP.NET WebForm application. Let's say for example I have a string value and want to send it to another page in my app e.g.
string sValue = "information";
I then put this into a session as so:
Session["value"] = sValue;
I then want to navigate to another page in my web app so i use:
Response.Redirect("~/ViewInvoices.aspx");
However when I try to extract any information from the session it is null
string sValue = Session["value"]; <-is now null
Could anyone either spot the problem or even better offer a solution to make this work?
Use this for redirecting:
Response.Redirect("~/ViewInvoices.aspx", false);
Hi I have a requirement to deal with an external API in MY ASP.Net Application.
There in a button click in my application, I have to redirect the user to an external URL at the same time POSTING some user credentials(username, password) to allow access to there (The external 3rd party page).
If I try to access to that external page without posting credentials(Just a redirect) I will not be able to view the required page and istead an error message saying invalid credentials(From the 3rd party page).
Suppose following are the information
1. URL need to access : https://externalurl.com/external-page.asp
2. username : myusername
3. password : mypassword
If I do a simple HTML form submission it works brilliantly as follows.
No Idea of how to do this in C# code in ASP .Net.
I can post the data using WebRequest APIs but no way to redirect in the same session. If someone can guide on doing this would be great. Thanks in advance...!!!
Edits...
In my case I can't use javascript or any client side scripting to do this.
Basically I don't have to read any response for my POST data. I need to redirect to that page(External location while posting information)
You can do it with an HttpWebRequest, but you need to cache your credentials so that they get passed with each request, not just the first; like this:
// Set queryUrl to your URL, either hard-coded or passed in
HttpWebRequest req = WebRequest.Create(queryUrl) as HttpWebRequest;
// Set your credentials
NetworkCredential cred = new NetworkCredential(username, pwd, domain);
// Setup cache to store your credentials for future requests
var cache = new CredentialCache {{queryUrl, "Ntlm", cred}};
req.Credentials = cache;
HttpWebResponse response = req.GetResponse() as HttpWebResponse;
Do like this to your button
<asp:Button ID="Button1" PostBackUrl="https://externalurl.com/external-page.asp" runat="server" Text="Submit" />
Hope this is clear
http://msdn.microsoft.com/en-us/library/ms178140%28v=vs.100%29.ASPX
You can either set the form method and action exactly like you have it now. Or use a postbackURL on the button.
The problem is going to be setting the name on the elements to be able to submit them. You have to use javascript to set the hidden fields. Because .net changes the "name" property of the rendered elements on server-side controls.
So, on the client side click event of the button, you can set the values of the hidden fields (just like the ones in your question) to whatever values you need. And, when the form submits, they will be posted with the proper names.
am having Sample.aspx and sample.js both are in different directory.
how can i get the Sample.aspx page session values in sample.js file ?
i cant get the value for the following types
function session() {
alert('<%=Session.getAttribute("hdn_CheckedData")%>');
alert(document.getElementbyId("hdn_CheckedData").value);
alert('<%=Session["CheckedData"]%>');
alert('<%=Session("CheckedData")%>');
alert('<%=Session["CheckedData"].ToString()%>');
alert('<%=Session("CheckedData").ToString()%>');
};
CheckedData - is the session
hdn_CheckedData - is the hiddenfield
i tried both of it.
is it possible then help me pls.....
Hiddenfiled, session, viewstate or anything............
One simple solution is to declare the session variables just before the load of your javascript file and inside the aspx page. Eg on sample.aspx you have
<script>
var sessionCheckData = "<%=session["CheckedData"].ToString()%>";
</script>
<script type="text/javascript" src="sample.js"></script>
and on sample.js you have
function session() {
alert(sessionCheckData);
};
Similar answer: How to get asp.net client id at external javascript file
You can save the session data to a hidden field on your page. After that you have direct access to that field in you js file.
Or you can declare a session variable as Aristos has proposed. That would more straightforward actually.
I would write a generic handler and use JSONP to pass any data to external Javascript.
Please take a look at here, here and here.
Since it is not cross domain, JSON should also work.
You have to assign session values to hiddenfield
after that you can use that values