Go to DotNetFunda.com
 Online : 689 |  Welcome, Guest!   Login
 
Home > Articles > .NET Framework > How to access HTML controls in Server side code c#

Submit Article | Articles Home | Search Articles |

How to access HTML controls in Server side code c#

red flag  Posted on: 11/10/2009 12:35:45 PM by Praveenbattula | Views: 3155 | Category: .NET Framework | Level: Beginner


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.


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.


If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Found interesting? Add this to:

| More



Please Sign In to vote for this post.

 
Latest post(s) from Praveenbattula

Latest Articles
Experience:3 year(s)
Home page:http://praveenbattula.blogspot.com
Member since:Tuesday, November 10, 2009
Level:Starter
Status: [Member]
Biography:Working as SharePoint and ASP.NET developer since 3 years for RampGroup and like to learn new technologies and share knowledge.
 Response(s)
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 | Advertise | Contact Us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you found plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 9/3/2010 3:59:08 AM