How can I pass value from C# web-forms (.aspx) to html page?
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
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly