I lost the web session when i tried window.location.replace("webform2.aspx") from webform1.
I tried href as well. Also I passed "/webform2.aspx" as a paramaeter.
However the session is lost on the redirection.
Can anyone help.
Webform1.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function pageload() {
window.location.href("/webform2.aspx");
}
</script>
</head>
<body onload="pageload()">
<form id="form1" runat="server" >
</form>
</body>
</html>
WebForm1.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
string str = this.Session.SessionID;
}
Webform2.aspx just an empty form and having the aspx.cs as the above code to note session id by debugging.
Update:
I maanged to acheive using the below code. However I just keep this question open to know redirection with relative URL(without session id) lose the session. One more point here is I made cookieless as true hence only my url will have the session id.
var path = window.location.href;
var i = path.lastIndexOf("/") + 1;
var loc = path.substring(0, i ).concat("webform2.aspx");
window.location.href(loc);
I used cookieless session and hence a relative URL redirection doesnt have knowledge about session, hence a new session is initiated.
I used below code to overcome the problem.
var path = window.location.href;
var i = path.lastIndexOf("/") + 1;
var loc = path.substring(0, i ).concat("webform2.aspx");
window.location.href(loc);
Related
I want to show data stored in Array-list in body of Mailbox. User can then add mail ids in To: according to his/her convenience and send the mail.
But its throwing JavaScript error when large amount of data is sent to body.
Here is the test code of .aspx page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function mail()
{
var array_store;
array_store = document.getElementById("array_store");
var vs = array_store.value;
window.location.href = "mailto: &body=" + encodeURIComponent(decodeURIComponent(vs));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Button_mail" runat="server" Text="OpenInMailBox" OnClientClick="mail()" />
<input type="hidden" id="array_store" name = "ArrayStore" value = '<%=this.ArrayStore %>' />
</form>
</body>
</html>
Code of .CS page :
public partial class mailto_check : System.Web.UI.Page
{
protected System.Text.StringBuilder ArrayStore = new System.Text.StringBuilder();
string str = "";
protected void Page_Load(object sender, EventArgs e)
{
ArrayList al = new ArrayList();
al.Add("Test1");
al.Add("hello ");
al.Add("Testing");
try
{
for (int y = 0; y < al.Count; y++)
{
str = al[y].ToString();
this.ArrayStore.Append(str+",%0D");
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
for large data I tested it by adding values to array-list.
Please help me with this.
Thanks in advance for positive answers.
I am using an iframe to upload an image. So I have two files - the file for the iframe and the file that will display the image. When the link to display the iframe is clicked this code is run:
function ShowUploadImageOut(RecordID) {
$('#<%=hfPieceID.ClientID %>').val(RecordID);
$("#dvAddImageOutturn").html(
'<iframe id="iframeUpload" src="utilities/UploadPOD.aspx?id=uploadOutturn"></iframe>'
);
}
On the iframe is an upload button that saves the image to a folder on the computer and sets the image file name to FileNameHidden
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fuUpload.HasFile)
{
if (Request.QueryString["id"] == "uploadOutturn")
{
String Path = ConfigurationManager.AppSettings["PieceOutturnFolder"].ToString()
+ Company.Current.CompCode;
string FileName = fuUpload.FileName;
if (File.Exists(Path + "/" + FileName))
{
File.Delete(Path + "/" + FileName);
}
fuUpload.SaveAs(Path + "/" + FileName);
filename = FileName;
imgTemp.ImageUrl = "/thumbnail.ashx?ImgFilePath=" + Path.Replace("/", "\\")
+ "\\" + FileName + #"&width=200&height=400";
FileNameHidden.Value = FileName;
}
}
}
FileNameHidden has the property clientidmode set to "static":
<input type="hidden" id="FileNameHidden" runat="server" clientidmode="Static" />
Back on the page for displaying the image there is a save button. This button takes the value from FileNameHidden and sets it to a hidden field. This new hidden field is used to save the file name to the database:
<asp:HiddenField ID="hfUploadedPieceHubImageFile" runat="server" />
<div id="dvAddEditImageOutturn" class="dvdraggable">
<div id="dvAddImageOutturn"></div>
<asp:LinkButton ID="btnPieceHubUpdateImage"
onclick="btnUpdatePieceImage_Click"
OnClientClick="CloseUploadWindow();return GetPieceOutturnFilename();"
CssClass="btnSaveSmall"
runat="server"
></asp:LinkButton>
</div>
function GetPieceOutturnFilename() {
if ($('#iframeUpload').contents().find('#FileNameHidden').length > 0) {
$("#<%= hfUploadedPieceHubImageFile.ClientID %>").val(
$('#iframeUpload').contents().find('#FileNameHidden').val()
);
}
return true;
}
The problem is in the GetPieceOutturnfile function. $('#iframeUpload').contents().find('#FileNameHidden').length is always zero. It should be finding the FileNameHidden from the iframe.
Going through the code I can see FileNameHidden definitely gets set the name of the image:
<input name="FileNameHidden" type="hidden" id="FileNameHidden" value="test.jpg">
So I don't understand why it says the length is zero
The problem was with the btnPieceHubUpdateImage for the onClientClick:
OnClientClick="CloseUploadWindow();
return GetPieceOutturnFilename();"
It was closing the window first then trying to save the FileNameHidden from the Iframe. Swapping these around so it calls GetPieceOutturnFileName first solved the problem.
I got it working with this code. Make sure the content loaded into the iframe (child.html) is on the same domain or it will not work for security reasons. Also, it will not work running the files on your desktop. They need to be served from a server.
index.html
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script>
$(document).ready(function() {
$("#iframeUpload").load(function() {
var fileName = $(this).contents().find("#FileNameHidden").val();
console.log(fileName);
});
});
</script>
</head>
<body>
<iframe id="iframeUpload" src="child.html" />
</body>
</html>
child.html
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<form>
<input name="FileNameHidden" type="hidden" id="FileNameHidden" style="display:none" value="test.jpg">
</form>
</body>
</html>
If this does not help solve your issue, you should provide files or a jsfiddle.
The textbox is defined as html input text type and takes input from the users. This value is needed to pass as key to hashtable defined in aspx.cs file being used in aspx file. But the correct value is not being given and an exception is being thrown. Any help in this regard would be helpful. Thanks
The code is as follows:
<% =Hashtable[document.getElementbyId("Textbox").Value]%>
document.getElementbyId("Textbox").Value is not giving the correct output. If it is replaced by a string value acting as key then it works fine. The only problem is getting the string value from textbox.
First include jquery in head section
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
Try below code to read textbox value :
First read text box value using
var key = $("[id$=Textbox]").val();
and see if value is present in texbox if present use this to read hash value
<% =Hashtable[key]%>
Try this it worked for me :
aspx :
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function GetHashtable() {
alert("<% = hashtable[this.test] %>");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtKey" OnTextChanged="txtKey_TextChanged" AutoPostBack="true" runat="server"></asp:TextBox>
</div>
</form>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
</body>
</html>
Code behind :
public partial class _Default : System.Web.UI.Page
{
protected int test;
public Hashtable hashtable = new Hashtable();
static Hashtable sHashtable = new Hashtable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
sHashtable[1] = "One";
sHashtable[2] = "Two";
sHashtable[13] = "Thirteen";
}
}
protected void txtKey_TextChanged(object sender, EventArgs e)
{
test = Convert.ToInt32("0" + txtKey.Text);
hashtable = sHashtable;
Page.ClientScript.RegisterStartupScript(this.GetType(), "GetHashtable", "GetHashtable();", true);
}
}
Hi I have a problem with a script. I think it has something to do with location... Works fine in all browsers. However when this script runs in a facebook app (Iframe) it doesn't trigger the function postbackhiddenfield in Internet Explorer. It does in all other browsers...
Anybody some tips?
<html xmlns="https://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
// Changing the value of the hidden field forces the page to postback
// and executes the hdnUpdater_ValueChanged() procedure on the code behind
function postBackHiddenField(hiddenFieldID) {
//var hiddenField = $get(hiddenFieldID);
var hiddenfield = document.getElementById(hiddenFieldID)
if (hiddenField) {
hiddenField.value = (new Date()).getTime(); __doPostBack(hiddenFieldID, '');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<!-- The acess_token is stored...//-->
<asp:HiddenField ID="hdnAccessToken" runat="server" />
<!-- The control that postbacks the access_token...//-->
<asp:HiddenField ID="hdnUpdater" runat="server" />
<!-- The script that grabs the access_token in the hash, stores the access_token in the
'hdnAccessToken', and postbacks the value for JSON retrieval and decoding...//
window.frameElement.contentWindow.location.hash
-->
<script type="text/javascript">
if (window.location.hash.length != 0) { // Check if there's a hash on the URI
accessToken = window.location.hash.substring(1); // Retrieve the access_token
document.getElementById('hdnAccessToken').value = accessToken; // Store the access_token
//postBackHiddenField('hdnUpdater'); // Postback the page
__doPostBack('hdnUpdater', '')
}
</script>
</form>
</body>
you changed :document.getElementById('hdnAccessToken').value = accessToken;
document.getElementById('<%= hdnAccessToken.ClientID %>').value = accessToken;
I have a page that is created entirely dynamically in the code behind (oh joy).
On pressing a button on this page, more elements are added to the page - which is fine, however, the refreshed page doesn't appear until the next postback. I would do a redirect, but I want to keep the state of any data entered in the existing controls.
So - I need to force a postback from the server code behind code. I imagine that this involves inserting some js code - but beyond that, I've not had any joy at all.
Here is an example to make things clearer:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</form>
</body>
</html>
A very simple page.
The code behind is simple too:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["data"] = 1;
}
PopulatePage();
}
private void PopulatePage()
{
int data = (int)Session["data"];
for (int n = 0; n < data; ++n)
{
TextBox tb = new TextBox();
tb.ID = n.ToString();
PlaceHolder1.Controls.Add(tb);
Literal lit = new Literal();
lit.Text = "<br/>";
PlaceHolder1.Controls.Add(lit);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int data = (int)Session["data"];
Session["data"] = ++data;
}
}
You click the button and more controls are added. This is way simpler than the page that I am having issues with, but it demonstrates the problems that I am having.
The postback is not needed and is a bit of a hack, you would be better off fixing the problem at the root.
private void PopulatePage()
{
for (int n = 0; n < Counter; n++)
{
CreateSingleControl(n);
}
}
private void CreateSingleControl(int index)
{
TextBox tb = new TextBox();
tb.ID = String.Format("text{0}", index);
PlaceHolder1.Controls.Add(tb);
Literal lit = new Literal();
lit.Text = "<br/>";
PlaceHolder1.Controls.Add(lit);
}
protected override void CreateChildControls()
{
PopulatePage();
base.CreateChildControls();
}
protected void Button1_Click(object sender, EventArgs e) {
CreateSingleControl(Counter);
Counter++;
}
private int Counter
{
get
{
return Counter = (int)(ViewState["data"] ?? 1);
}
set
{
ViewState["data"] = value;
}
}
Use the Request forms list to search unique control ID's
Something like this link below, last post, not sure his code is exactly correct, but you'll get the idea.
Searching for controls in Request.Form / Parsing a NameValueCollection
Give your controls unique IDs' and you can search for them on the postback.
Plus, i use to add other things to the ID to let the postback searching know what type of control it was, like adding txt_firstname, i know that firstname was a txtbox. Use regex to search the returned strings.