Sunday, 5 April 2020

Pass Value From Code Behind To JavaScript Function In Asp.Net

4/05/2020 - By Pranav Singh 0


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.


<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.



About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top