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:
C# Code:< input type="text" name="txtName" />
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.
- The tag should have an attribute called NAME. Because it is used as key in form[].
- The form method should be of type POST.
- Form control is of type INPUT OR SELECT only.
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.
No comments:
Post a Comment