How to access html control without runat server in C# code

No.of Views1598
Bookmarked1 times
Downloads 
Votes2
By  praveenbattula   On  16 Feb 2010 03:02:49
Tag : CSharp , How to
How to access html control without runat=server in C# code
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

 
In ASP.NET coding, I don't think it is needed to create or declare only ASP.NET server-side controls. Some cases, 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#].
 
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. 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 name="txtName" type="text" id="txtName" />

C# Code:
 
string strValue = Page.Request.Form["txtName"].ToString();


Note:
To get the values in server side code of HTML control, we need to follow below points.
  • The tag should have an attribute called NAME. Because it is used as key in form[].
  • The form method should be of type POST.
That's it!!! Please let me know, if you have any issues with this approach. Hope this helps...
 
Thank you
 
 
Sign Up to vote for this article
 
About Author
 
praveenbattula
Occupation-Not Provided
Company-Not Provided
Member Type-Fresh
Location-Not Provided
Joined date-26 Nov 2009
Home Page-Not Provided
Blog Page-Not Provided
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top