Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
Hi there
I am using a post method in form to get data on next page
On next page's page load event I am calling a javascript from code behind like this
protected void Page_Load(object sender, EventArgs e)
       {
       
           string amt = Page.Request.Form["nm"];
           HiddenField1.Value = amt;
       string org= base64Decode(amt);
           Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","myfunction();",true);
       string strs=HiddenField1.Value;
       }

 
and my javascript function is as follow
 
function myfunction() {
 
          var password = 'L0ck it up saf3';
          var ciphertext = document.getElementById('<%=HiddenField1.ClientID%>').value;
        
          var origtext = Aes.Ctr.decrypt(ciphertext, password, 256);
       document.getElementById('<%=HiddenField2.ClientID%>').value= origtext; 
         
          return true;
  }
 
Now the problem is that when javascript is called i want to take the hidden field value
in string on page load event (see line string strs=HiddenField1.Value;)but i am getting old value ( HiddenField1.Value = amt;)
but I want to take hidden field value after javascript called.
pls help me
Posted 13-Dec-12 2:34am
Edited 13-Dec-12 2:35am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

use the following line of code
 
string strs=Request["HiddenField1"].ToString();
 
You will get the value on page postback only.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Javascript is called after executing all of your server side code. so it is not possible to access your controls values which are updated by javascript. Asynchronous post back (Ajax) would be helpful here.
  Permalink  
Comments
nira.parmar at 13-Dec-12 10:52am
   
pls tell me how to use ajax? actually I am using <form method="post" action=""nextpage.aspx"> on the page and used all html control
Sheikh Muhammad Haris at 13-Dec-12 11:07am
   
Kindly provide me the complete scenario. What you actually need to do with variable "strs" after being updated by javascript?
nira.parmar at 13-Dec-12 13:33pm
   
This is my whole code
Page 1---
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="prac1.aspx.cs" Inherits="tampering.prac1" %>
 
<!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>
<script type="text/javascript" src="encrypt-decrypt.js"></script>
<script type="text/javascript" language="javascript">
 
function disableText() {

var amt = document.getElementById('MAmount').value;
var password = 'L0ck it up saf3';
var ciphertext = Aes.Ctr.encrypt(amt, password, 256);

document.getElementById('hdn').value = vb;
document.getElementById('MAmount').disabled = true;
return true;
}



</script>
<body>
<form name="form" action="Default.aspx" method="post" önsubmit="disableText()">

<div>
<table>
<tr>
<td>

<table id="table1" border="10">
<tr>
<th colspan="2" >Detail for Donation </th>
</tr>
<tr>
<td class="style34"> Transaction Amount: </td>
<td><input type="text" id="MAmount" name="MAmount" size="25" style="width: 127px" />
<input type="hidden" id="hdn" name="nm" />
</td>
</tr>
<tr>
<td><br/>
<input type="submit" value="SUBMIT" />
<br /> </td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 

Page 2----
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="tampering._Default" %>
 
<!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>
<script type="text/javascript" language="javascript" src="encrypt-decrypt.js"> </script>
<script type="text/javascript">

 
function myfunction() {

var password = 'L0ck it up saf3';
var ciphertext = document.getElementById('<%=HiddenField1.ClientID%>').value;

var origtext = Aes.Ctr.decrypt(ciphertext, password, 256);
document.getElementById('<%=txt3.ClientID%>').value = origtext;
//this is decrypted value;
document.getElementById('<%=HiddenField1.ClientID%>').value = origtext;
return true;
}
</script>
</head>
<body>
 
<form id="form1" runat="server">
<div>
 
<table>
<tr>
<td>

</td>
<td>

<asp:HiddenField ID="HiddenField1" runat="server" />

</td>
<td>

</td>
</tr>
</table>
</div>
</form>
</body>
</html>
 

Page 2 cs file----
 
public partial class _Default : System.Web.UI.Page
{

protected void Pa
nira.parmar at 13-Dec-12 13:36pm
   
page 2 cs file--
public partial class _Default : System.Web.UI.Page
{
public string abc;
protected void Page_Load(object sender, EventArgs e)
{

string amt = Page.Request.Form["nm"];
HiddenField1.Value = amt;

Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","myfunction();",true);
string strs= HiddenField1.Value;//here i want to get decrypted value which is coming from javascript.
}

}
Sheikh Muhammad Haris at 13-Dec-12 16:59pm
   
I meant to say what you need to do wuth decrepted value ?? Either it is for only viewing purpose or you need to save it in db ?
nira.parmar at 14-Dec-12 0:31am
   
I want to save in db also and for other function.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hello, you have to use IsPostBack checking in your page_load method.
 

protected void Page_Load(object sender, EventArgs e)
       {
            if(!IsPostBack)
{
           string amt = Page.Request.Form["nm"];
           HiddenField1.Value = amt;
       string org= base64Decode(amt);
           Page.ClientScript.RegisterStartupScript(this.GetType(),"alert","myfunction();",true);
       string strs=HiddenField1.Value;
}
       }
 
like this. because, when you go to the server side with post back then your client side code reset and for that you are loosing your data.
 
thanks
Rashed:: Bangladesh.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 13 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100