Whenever we are developing any websites in asp.net then sometimes we need to use html control and its really very challenging that how to get the values of that control from source code or back end .And once we got the value in that html control then we can perform any operations like Insert, Delete, Update into database.
The most important things is that sometime we need to retrieve the values of those html controls as it is not declared as runat="server" on the page. And if we not include runat=”server” then we can’t fetch “Id” of that controls from source code.
So here I had demonstrated a simple demo that how to get html control textbox value from source code without include runat=server.Most of time we fetch problems on getting the selected date from textbox using datepicker Jquery.
Here is the simple way to get values of the html input controls which doesn't have runat="server" attribute defined.
1. First we have to take HTML control on which we can select date.
- <table class="myTable" align="center">
- <tr>
- <td>
- Select Date
- </td>
- <td>
- <input type="text" id="datepicker" name="_datepicker" />
- <asp:Button ID="btnSave" runat="server" Text="Show Date" Style="width: 86px; height: 36px;" OnClick="btnSave_Click" />
- </td>
- </tr>
- <tr>
- <td></td>
- <td>
- <asp:Label ID="lblDate" runat="server" Text=""></asp:Label>
- </td>
- </tr>
- </table>
2. After that we have to include jquery datepicker library in head sections of page.
- <script src="DatePicker/jquery-1.10.2.js" type="text/javascript"></script>
- <link href="DatePicker/jquery-ui.css" rel="stylesheet" type="text/css" />
- <script src="DatePicker/jquery-ui.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(function () {
- $("#datepicker").datepicker();
- });
- </script>
OR
Note: We can directly include it like below:
- <script src="//code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script>
- <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"type="text/javascript"></script>
- <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
3. Finally we can get value of selected date on button click event or wherever we want from Textbox on label from code behind in ASP.NET.
- protected void btnSave_Click(object sender, EventArgs e)
- {
- string strValue = Page.Request.Form["_datepicker"].ToString();
- lblDate.Text = strValue;
- }
Note: To get the values on 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.