Hi guys I got answer.These are helpful
RETRIVING VALUES FROM JAVASCRIPT TO .ASPX.CS PAGGE
You can take following steps to read value from javascript.
1. Create html hidden field on page.
2. Set its value on client side using javascript.
3. Read html hidden field using Request.Forms["hiddenfieldname"]
You can set the variable from JavaScript into Hidden control and then you can get it in aspx page.
HTML:
<script type="text/javascript">
function abc()
{
var str="value";
document.getElementById("Hidden1").value=str;
}
</script>
<body>
<form id="form1" runat="server">
<div>
<input id="Hidden1" type="hidden" runat="server" />
<asp:Button ID="Button1" runat="server" OnClientClick="abc()" Text="Button"
onclick="Button1_Click" />
</div>
</form>
</body>
Code Behind:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Hidden1.Value);
}
But you should pay attention on the order of setting the variable to Hidden control in JavaScript and retrieving the value of Hidden control in aspx page. In the same event handle, it needs execute the Client first. For example, OnClientClick="abc()" will be executed before onclick="Button1_Click". Otherwise, you will get the empty.
I TRIED
protected void btn(object sender, EventArgs e)
{
string S = txt1.Value;
Response.Write(S);
}
ASPX:
<form id="form2" runat="server">
<input id="File1" type="file" onchange="LoadImage()" runat="server" />
<img id="img" src="" alt="Image" runat="server" />
<asp:Button ID="b1" Text="UPLOAD" runat="server" OnClick="btn" />
<br />
<input type="text" id="txt1" runat="server" />
<br />
</form>
<script language="javascript" type="text/javascript">
function LoadImage()
{ document.getElementById('img').src=document.getElementById('File1').value; var filename=document.getElementById('File1').value;
// for SHOWING IN A TEXTBOX
document.getElementById('txt1').value=filename;
}
</script>
Access JavaScript variables on PostBack using ASP.NET Code
In this article, we will see how to pass javascript values on postback and then access these values in your server side code. This article will primarily showcase two techniques of doing so. One using Hidden variables and the other using the __doPostBack() javascript method.
Using Hidden Variables – This method is pretty straightforward. All you have to do is declare a hidden field (inpHide) in your web page and then set the value of the hidden field in your JavaScript function as shown below.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Pass Javascript Variables to Server</title>
<script type="text/javascript">
function SetHiddenVariable()
{
var jsVar = "dotnetcurry.com";
// Set the value of the hidden variable to
// the value of the javascript variable
var hiddenControl = '<%= inpHide.ClientID %>';
document.getElementById(hiddenControl).value=jsVar;
}
</script>
</head>
<body onload="SetHiddenVariable()">
<form id="form1" runat="server">
<div>
<input id="inpHide" type="hidden" runat="server" />
<asp:TextBox ID="txtJSValue" runat="server"></asp:TextBox>
<asp:Button ID="btnJSValue" Text="Click to retreive Javascript Variable" runat="server" onclick="btnJSValue_Click"/>
</div>
</form>
</body>
</html>
Then access the value of this field in the code behind on a Button click as shown below:
C#
protected void btnJSValue_Click(object sender, EventArgs e)
{
txtJSValue.Text = inpHide.Value;
}
Note: Observe that the <body> tag has an onload attribute using which the javascript function is being called.
<body onload="SetHiddenVariable()">
Using __doPostBack() – All but two ASP.NET web controls (Button & ImageButton) use the __doPostBack javascript function to cause a postback.
The EVENTTARGET is the ID of the control that caused the postback and the EVENTARGUMENT contains any arguments passed that can be accessed on the server. The __doPostBack method sets the values of the hidden fields and causes the form to be submitted to the server.
I hope this gives you a clear idea of how we can use the __doPostBack function to submit the value of a JavaScript variable to the server. All we have to do is call this JavaScript method explicitly and pass in the JavaScript variable value using the EVENTARGUMENT. Here’s an example:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Pass Javascript Variables to Server</title>
<script type="text/javascript">
function SetHiddenVariable()
{
var jsVar = "dotnetcurry.com";
__doPostBack('callPostBack', jsVar);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtJSValue" runat="server"></asp:TextBox>
<asp:Button ID="btnJSValue" Text="Click to retreive Javascript Variable"
runat="server"/>
</div>
</form>
</body>
</html>
The code behind will look similar to the following:
C#
protected void Page_Load(object sender, EventArgs e)
{
this.ClientScript.GetPostBackEventReference(this, "arg");
if (IsPostBack)
{
string eventTarget = this.Request["__EVENTTARGET"];
string eventArgument = this.Request["__EVENTARGUMENT"];
if (eventTarget != String.Empty && eventTarget == "callPostBack")
{
if (eventArgument != String.Empty)
txtJSValue.Text = eventArgument;
}
}
else
{
btnJSValue.Attributes.Add("onClick", "SetHiddenVariable();");
}
}
The GetPostBackEventReference() emits __doPostBack() and also provides a reference to the control that initiated the postback event. The first time the page is loaded, IsPostBack is false, so we enter the ‘Else’ loop where we register the JavaScript on the button click event, using the Attribute collection of the Button control. When the user clicks the button, the JavaScript method is now called which in turn explicitly calls the __doPostBack thereby passing the JavaScript variable (jsVar) as an EVENTARGUMENT. We then access this hidden variable EVENTARGUMENT using our server side code and set the value of the textbox.