Hi
I have one problem,i need to pass value from aspx page to html page using session.I don’t want pass value using url query string,
For example,i have login.aspx page and Welcome.html,in this if user login i need to pass username to welcome page.When login time can store in session or public variable and in welcome page retrieve and show in welcome page input box.
Note:I don’t want url query string like Welcome.html?Username=aaaa
Pls reply asap
Regards
Aravind
html files are not processed by .net so you can’t use server code on them. You could store the data in a cookie, or failing that you’ll have to configure iis to process html files and that will let you add the relevant asp.net code to them.
http://community.discountasp.net/threads/mapping-html-to-aps-net-on-iis-7.6039/
Hi Aravind,
You can use two aspx pages and jquery to interact with the code behind methods in the welcome page and show the user name from the code behind to the clinet side.
For the Login page do something like this :
function redirect() { var textUserName = document.getElementById('txtUserName').value; $.ajax({ type: "POST", url: "Welcome.aspx/getUserName", data: JSON.stringify({name:textUserName}), contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { window.open("Welcome.aspx"); } }); }
you can call the function redirect on clicking the login button and from there the username will be sent to the code behind method called getUserName() that is present in the welcome page.
And now in the welcome page the client side script will be like :
function init() { $.ajax({ type: "POST", url: "Welcome.aspx/getUserNameToWelcome", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { $('#lblUserName').html(msg.d); } }); }
you can call the init method on onload event like <body onload="init()">
and the welcome page code behind will be as:
public static string userName = ""; protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static string getUserName(string name) { userName ="Welcome, "+ name; return userName; } [WebMethod] public static string getUserNameToWelcome() { return userName; }
and it works perfectly fine without
using query string parameter.
Hope this helps.
Please revert if any discrepancy found.
thank u for ur reply,but welcome.html page,how i write webmethod code in html page ?
Hi aravind,
Thanks for your post.
AidyF
html files are not processed by .net so you can’t use server code on them. You could store the data in a cookie
As for your problem, I agree with AidyF’s opinion. If you’d like to pass data from aspx page to html page by using cookie, you could refer to the following code.
Login.aspx page:
<head runat="server"> <title></title> <script> function StoreUserName() { var username = document.getElementById("txtUserName").value; document.cookie = "username=" + username; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> <br /> <asp:Button ID="btnlogin" runat="server" Text="Login" OnClientClick="return StoreUserName();" OnClick="btnlogin_Click" /> </div> </form> </body>
Login.aspx.cs
protected void btnlogin_Click(object sender, EventArgs e) { Response.Redirect("welcome.html"); }
Welcome html page
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script src="../../../Scripts/jquery-1.8.2.js"></script> <script> $(function () { var cname = "username"; var username = getCookie(cname); $("#userName").append(username); }) function getCookie(cname) { var name = cname + "="; var ca = document.cookie.split(';'); for (var i = 0; i < ca.length; i++) { var c = ca[i]; while (c.charAt(0) == ' ') c = c.substring(1); if (c.indexOf(name) != -1) return c.substring(name.length, c.length); } return ""; } </script> </head> <body> <div id="userName"></div> </body> </html>
If you have any question about my reply, please let me know freely.
Best Regards,
Fei Han
Hi Aravind,
You can’t really write code behind in a html page, so one of the workarounds would be by using this Welcome apge as a layer to pass values from the login.aspx and get the value to the Welcome.html
so the changes would look like this :
Loing page:
function redirect() { var textUserName = document.getElementById('txtUserName').value; $.ajax({ type: "POST", url: "TemporaryPage.aspx/getUserName", data: JSON.stringify({name:textUserName}), contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { window.open("Welcome.html"); } }); }
TemporaryPage.aspx.cs :
public static string userName = ""; protected void Page_Load(object sender, EventArgs e) { } [WebMethod] public static string getUserName(string name) { userName ="Welcome, "+ name; return userName; } [WebMethod] public static string getUserNameToWelcome() { return userName; }
and the Welcome.html will look like this :
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Weclome Home!</title> <script src="jquery.js"></script> <script type="text/javascript"> function init() { $.ajax({ type: "POST", url: "MultiLineTitleForAnchor.aspx/getUserNameToWelcome", data: "{}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { $('#anchor').html(msg.d); } }); } </script> </head> <body onload="init()"> <div> <a href="#" id="anchor"></a> </div> </body></html>
So in order to transfer the data without using query string you can either use a cookie as suggested by
AidyF and
Fei Han
Or use one more aspx page as a temporary page which helps you to the transmit the data .
Hope this helps.