2

I am using asp.net C# I am also using the jQuery UI Calendar control. calendar control

It appears the calendar control wants to work with an input control with an ID of "datepicker".

<input type="text" id="datepicker"  />

I want to use value of the input control in my code behind but seeing how it is not an asp.net control I am not sure how I can reference it in the code behind.

Does anyone know how to get the value of the input control in the code behind?

5 Answers 5

10

use

<input type="text" id="datepicker" name="datepicker" />

and in the code behind:

Request.Form["datepicker"]

In fact, Form property of Request is populated with form values.

Sign up to request clarification or add additional context in comments.

Comments

2

Here you have to mention your form name which contains input tag

<input name="txtemail" id="txtemail" runat="server" type="text" class="cssspa" form="form"/>

in C# string email = txtemail.Value.ToString();

now u can get data from textbox

1 Comment

what if the OP doesn't want a server control?
0

If you want to use basic form pulling, you need to add a name attribute:

 <input type="text" id="datepicker" name="datepicker" />

 if(Page.IsPostBack)
 {
     string value = Request.Form["datepicker"];
 }

Alternatively, you can mark is as an HtmlInputControl

<input type="text" id="datepicker" runat="server" />

System.Web.UI.HtmlControls.HtmlInputControl datepickerControl = this.FindControl("datepicker") as System.Web.UI.HtmlControls.HtmlInputControl;

if(datepickerControl != null)
    string value = datepickerControl.Value;

2 Comments

adding runat=server would not change the id rendered on the page?
Apologies. Yes, it will change the ID rendered on the page. You can use ClientIDMode="Static" if on ASP.NET 4, or you can use $('<$= datepicker.ClientID %>') if you want to select the generated id via jQuery.
0

You can access the value via the Request object like onof wrote, or you can turn every html tag into a server control by adding the attribute runat:

 <input type="text" id="datepicker" runat="server" ClientIdMode="static" />

The attribute ClientIdMode="static" ensures that the tags ID is not changed by the ASP.NET runtime.

Then you can access the input by the autogenerated member datePicker.

Comments

0

Since you explicitly say code-behind, I assume you probably want to stay with the WebForms/Controls model? With clientidmode you can do that while also having a determinite id value for the calendar.

<asp:TextBox id="datepicker" ClientIDMode="Static" runat="server" />

Then you can interact directly with the control from the code-behind as normal.

But, the calendar control does not require a specific ID. You can initialize it with whatever ID you want

<asp:TextBox id="DateTextBox" runat="server" />

<script>
$(function() {
    $( "#<%=DateTextBox.ClientID%>" ).datepicker();
});
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.