Go to DotNetFunda.com
 Online : 156 |  Welcome, Guest!   Login
 Skip Navigation Links Home > Articles > .NET Framework > How to access HTML controls in Server side code c#
  Win at least 8 gifts every month now! >> Top Performers of this month.   


Notice: If page content has been copied from other sources without proper permission, kindly let us know with details from where it has been copied for further action.

All Articles | Submit Article |

How to access HTML controls in Server side code c#

 Posted on: 11/10/2009 12:35:45 PM by Praveenbattula | Views: 627 | Category: .NET Framework | Level: Beginner | Print Article
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.

Webmaster says - If this article content has been copied from other websites without proper permission, kindly let us know with details from where it has been copied for further action.

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.


Example:
HTML declaration:

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

C# Code:

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

Note:

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.


Interesting?   Share and Bookmark this kick it on DotNetKicks.com


Experience:3 year(s)
Home page:http://praveenbattula.blogspot.com
Member since:Tuesday, November 10, 2009
Biography:Working as SharePoint and ASP.NET developer since 3 years for RampGroup and like to learn new technologies and share knowledge.
 Latest post(s) from Praveenbattula

   ◘ How to access HTML controls in Server side code c# posted on 11/10/2009 12:35:45 PM


 Responses
Posted by: Vipinpaliwal | Posted on: 10 Nov 2009 09:17:26 PM

Hi Praveen,

That is very good.you give very good idea.

Submit Article

About Us | The Team | Contact Us | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
All rights reserved to DotNetFunda.Com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks.
(Best viewed in IE 6.0+ or Firefox 2.0+ at 1024 * 768 or higher)