Monday, April 27, 2009

To access html control without runat=”server” in C# code.

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.

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.
  • 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...


  1. Nice tip.
    Wil look for more articles.

  2. But this will work just with input elements, because most of other HTML elements haven't standard name attribute. am i right?

  3. Hi Elgarhy,
    Yes, you are right. It will work with the input elements and the name attribute is must for them. Because standard HTTP protocol sends the data to server as key, value parameters. Key is the name of the element [not ID] and value is the value of the form control.

  4. I find that the fact .net developers need to be told this - any other web language takes this for granted. Web forms are a magical MS creation, and the rest of the web does it this way. People need to step outside the webforms world more often - there is alot out there and their knowledge of the web is severly limited by not knowing these things :)

    Good post though, keep up the goog work.
    Doug (An developer by trade)

  5. <input type='text' name='forename' />

    Works fine, but doesn't maintain it's value after the user submits the form.

  6. Yes, Most of the devs are in the idea of after submission of the form the values should stay there without disappear. But, actually when you submit the form, The whole page is loading again with postback. So, all values gets clear if and only if it is not a ASP.NET server side control. If it is a ASP.NET server side control then there is a concept called ViewState which maintains the values of the controls in encrypted format on the page. [You can see this by view source on the browser. like input with type="hidden" and some encrypted text.] So, before page sent back from server the view state cycle runs and all the values set and renders on the page. So the values remains same for the .NET controls. Where as just input control without any server side tag, for them no view state maintain. So, values won't be there after submission of the form. Is it clear? Please let me know, if you have any more questions on it.

  7. I think what people need to understand is that the web is a stateless environment. NO form element will maintain state after a postback. Microsoft has "faked" state by adding viewstate to the mix, so that windows developers can develop for the web without needing to know anything about it. People need to leave this behind as its failed thinking. Web forms as a framework will save you time, but you need to understand what is going on in the background to achieve this - it will make you a better developer, and open your eyes to other things you can do with web services and ajax.

  8. Thanks for the nice comment Doug.

  9. Thank you for this tip. While using ckeditor, writing runat="server" was a programatically porblem. But this tip, solved my issue. Thanks again.

  10. Hi,
    this code is not working for me.
    i have just placed your code for test but it gives a exception of null value.

  11. Then you didn't follow my rules....
    Please check them one more time and let me know.

    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.

  12. I am getting the same null error and I did follow your rules

  13. I had same problem with null, so I added name and id attributes to the input tag and it worked!

  14. How Can Access select list selected value in code behind withou runat=server

  15. sir its not working for me

  16. Hi there,
    It should work as the object Request.Form will load all key value pairs on the page. Try correct and make sure the input control has name attribute and the form method type should be POST.

  17. Thanks alot..It really helped..I changed 'ID' to 'name' in my input control and it worked for me...Thanks alot

  18. I am able to get value using request.form[] but I want to display value on textarea . and I don't want to use runat server. its urgent .Please tell me with example. its so urgent