开发者

Getting data from an Input control in to code behind

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"  /&g开发者_Go百科t;

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?


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.


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


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;


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜