In this blog post, you will learn about to get the value of input javascript and how to get Html textbox value in ASP.Net C# code behind aspx.cs page. Previously I have explained How to check if dropdown list contains value in C#, Facebook Style Homepage and Login Page Design using Html and CSS.
There are two ways we can access the HTML TextBox value in aspx code-behind:-
- Using Request Form collection and name property.
- Using runat = “server” property.
How to get Html textbox value in ASP.Net C#
The HTML <input> element is used to create interactive controls for web-based forms in order to accept data from the user. To get the value of textbox in javascript from Html the tag should have an attribute called [NAME]. Find the below source code:-
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>get value of input javascript</title> </head> <body> <form id="form1" runat="server"> <h2>How to get html textbox value in asp.net</h2> <div> <input type="text" placeholder="First Name" name="_firstname" /><br /> <input type="text" placeholder="Last Name" name="_lastname" /><br /> <br /> <asp:Button ID="btnShow" runat="server" Text="Show" OnClick="btnShow_Click" /> <br /> <br /> <asp:Label ID="result" ForeColor="Red" runat="server"></asp:Label> </div> </form> </body> </html>
Default.aspx.cs
using System; using System.Web.UI; public partial class index : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnShow_Click(object sender, EventArgs e) { string firstName = Page.Request.Form["_firstname"].ToString(); string lastname = Page.Request.Form["_lastname"].ToString(); result.Text = firstName + " " + lastname; } }
Conclusion
I hope you liked this article on how to get html textbox value in asp.net code behind page. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave a Reply