Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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

C#
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
Updated 13-Dec-12 2:35am
v2

use the following line of code

C#
string strs=Request["HiddenField1"].ToString();


You will get the value on page postback only.
 
Share this answer
 
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.
 
Share this answer
 
Comments
nira.parmar 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
[no name] 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 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 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.
}

}
[no name] 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 ?
hello, you have to use IsPostBack checking in your page_load method.


C#
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900