How to Pass a value from asp.net (.aspx) page to HTML page


How can I pass value from C# web-forms (.aspx) to html page?

 


Asked by:- Aiswarjya
1
: 166 At:- 9/12/2017 11:02:01 AM
asp.net web-forms C# html

I suppose you need to pass some specific value from C# (Controller) to HTML(.cshtml View) page? 0
By : vikas_jk - at :- 9/12/2017 11:38:51 AM
no no i am talking about normal asp.net page 0
By : Aiswarjya - at :- 9/12/2017 11:50:32 AM
are you talking about asp.net web forms? .aspx pages? 0
By : vikas_jk - at :- 9/12/2017 12:32:00 PM





1 Answers
profileImage Answered by:- vikas_jk

Although if not necessary you can render HTML directly in .aspx pages, it can do it, but still, if you need to pass values from .aspx to HTML you can do it using by passing value in Query string, here are the example steps

Step 1: Create a text box in .aspx web-form

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
<asp:Button ID="Button1" runat="server" Text="Passing value to HTML" onclick="Button1_Click" />

Step 2: In the code-behind of the above page, write code to handle on Button1  click using C# and pass value to HTML using Query String

protected void Button1_Click(object sender, EventArgs e)  
{  
  //Passing the value to .htm page using QueryString  
    Response.Redirect("HTMLPage1.htm?username=" + TextBox1.Text);  
   
}  

Step 3: Get data in HTML page using javascript in our project, like HTMLPage1.htm

place input text box code in HTML page using the code

<input id="txtInput" type="text" />  

and then get data in this page using Query string with parameters in url as

http://localhost:24242/HTMLPage1.htm?username=Testing

Using the below javascript/jQuery code:

<script src="Scripts/jquery-1.7.js" type="text/javascript"></script>  
<script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>  
<script>  
    $(document).ready(function() {  
        $("#txtInput").val(getParameterByName("username"));  
  
        function getParameterByName(name) {  
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");  
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),  
                results = regex.exec(location.search);  
            return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));  
        }  
    });  
</script>  

Original Source here

1
At:- 9/13/2017 11:20:12 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use