How to access HTML controls in Server side code c#

Posted by in .NET Framework category on for Beginner level | Views : 24997 red flag

This is the simple way of getting or accessing the INPUT HTML controls value in server side coding. This will help you in dynamic programming like create HTML controls at runtime and read their values.

In ASP.NET coding, I don't think it is needed to create or declare only ASP.NET server-side controls for a perticular requirement. Sometimes, to make our page more efficient and faster we can write HTML controls by adding runat="server" to access them on server side code [C#], if they really need it in c# code and use for different purposes.

But, there are some special requirements where we need to create HTML controls dynamically in c# and add them in a string and write the string to a page. And whenever some event raised like button click event, on server side code, we need to retrieve the values of those HTML controls. As it is not declared as runat="server" on the page, we can't take the values very simple by referring it's ID and can't access property like .Value or .Text etc...

In that type of scenarios, this solution works. Please follow the solution below to get the values of the HTML controls which doesn't have runat="server" attribute defined.

HTML declaration:

<input type="text" name="txtName" />

C# Code:

string strValue = Page.Request.Form["name of the control"].ToString();


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

  1. The tag should have an attribute called NAME. Because it is used as key in form[].
  2. The form method should be of type POST.
  3. Form control is of type INPUT OR SELECT only.

That's it!!! 

You are now ready to take the values in your server side code and access the value for further processing. Love to hear your comments.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Praveen Kumar
Member Level: Starter
Member Status: Member
Member Since: 11/10/2009 12:17:11 PM
Country: India
Rare Solutions.
Working as SharePoint and ASP.NET developer since 3 years for RampGroup and like to learn new technologies and share knowledge.

Login to vote for this post.

Comments or Responses

Posted by: Vipinpaliwal on: 11/10/2009
Hi Praveen,

That is very give very good idea.

Login to post response

Comment using Facebook(Author doesn't get notification)