how to get html textbox value

Keywords | get html textbox value asp.net, get input textbox value asp net, how to get html textbox value in asp net code behind

Get HTML Textbox Value ASP.Net

The HTML <input> element is used to create interactive controls for web-based forms in order to accept data from the user. Find the simplest way to get HTML textbox value in asp.net code behind below:-

<input type="text" name="_name"  />       
<asp:Button ID="btnShow" Text="Show" onclick="btnShow_Click" />
protected void btnShow_Click(object sender, EventArgs e)
        {
            string strValue = Page.Request.Form["_name"].ToString();
            Response.Write(strValue);
        }

How to get HTML textbox value in asp.net code behind

To get the values of the server-side code of HTML control, we need to follow below points:

  • The tag should have an attribute called NAME
  • The form method should be of type POST
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="UI_Design.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>how to get HTML input value in code behind asp net</title>
</head>
<body>
    <form id="form1" runat="server">
        <h2>Get HTML input text values in asp.net c# code behind</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" Text="Show" OnClick="btnShow_Click" />

            <br />
            <br />
            <asp:Label ID="result" ForeColor="Red" runat="server"></asp:Label>
        </div>
    </form>
</body>
</html>

using System;
using System.Web.UI;
namespace UI_Design
{
    public partial class Default : 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;
        }
    }
}

Download Code

What do you think?

I hope you liked this article on How to get html textbox value in asp net code behind. 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

Please enter your comment!
Please enter your name here