This article will show you how to pass value from code
behind to aspx page. In this I will show you method in which you can pass the
value from code behind to .aspx page and how you can access it inside a
javaScript method.
Do in our .aspx page first we will take an hidden field control as show below.
Do in our .aspx page first we will take an hidden field control as show below.
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
</form>
</body>
|
Now go to your code behind and assign the value to the
hidden field.
protected void Page_Load(object sender, EventArgs e)
{
HiddenField1.Value = "This value is from
code behind.";
}
|
Now in you code behind write the below code.
<script>
function AcccessCodeBehindValue() {
var data = document.getElementById('<%=HiddenField1.ClientID%>').value;
alert(data);
}
</script>
|
After writing the above code just call the code to execute.
<script>
AcccessCodeBehindValue();
</script>
|
Here is your complete code of
.aspx.cs file.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script>
function AcccessCodeBehindValue() {
var data = document.getElementById('<%=HiddenField1.ClientID%>').value;
alert(data);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
</div>
<script>
AcccessCodeBehindValue();
</script>
</form>
</body>
</html>
|
Now run the page to check the output.
0 comments:
Please let me know your view