问题
I have a page with a number of dynamic controls.
For some of the dynamically created textboxes, I'd like to add calendar control using jQuery (Keith Wood)
Normally, if the textbox controls are not dynamic, I would have the following javascript function to call on the calendar popup for the txtBoxDate textbox:
$(function () {
$('#<%=txtBoxDate.ClientID%>').datepick({ dateFormat: 'dd MM yyyy' });
});
Since I don't now the IDs of my dynamically created textboxes, how do I call on the jQuery calendar function?
Any ideas?
Much appreciated!
EIDT: I create the controls as follows with some loop (to create multiple):
TableRow tr = new TableRow();
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();
TextBox txtValue = new TextBox();
txtValue.Width = 250;
txtValue.ID = "textbox_" + dt.Rows[j][2].ToString();
回答1:
You can add a css Class name to your editor that hold the Calendar and attach the datepicker base on that, eg:
TextBox txtValue = new TextBox();
txtValue.Width = 250;
txtValue.CssClass = "TheDateTimePicker";
and on script:
$(function () {
$('.TheDateTimePicker').datepick({ dateFormat: 'dd MM yyyy' });
});
You can use the same css class name for all the editors that keep this date control, without change anything else. This $('.TheDateTimePicker') selector will apply the "datepick" to all controls that have that css class.
来源:https://stackoverflow.com/questions/16226774/datepicker-for-dynamically-created-controls