Reputation: 2489
I have an editable Gridview with columns as below:
<asp:TemplateField HeaderText="Date" >
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Width="160" CssClass="DateTimePicker" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amt $">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Width="160"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add Stage" onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
As you can see, this gridview as a button at the bottom which allows user to add rows to the gridview.
I want to have a calendar pop up for my Date column so I used jQuery (Keith Wood) and call on the JS function like this:
$(function () {
$('.DateTimePicker').datepick({ dateFormat: 'dd MM yyyy' });
});
In my gridview, the first time when I click on the Date textbox (on first row), the calendar pops up. However, once I add a row to the gridview, the calendar function no longer appears.
This is the way I add a new row to gridview from code-behind:
private void AddNewRowToGrid()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
Any ideas why my calendar does not pop up once I add new rows to the gridviews?
Cheers!
Upvotes: 3
Views: 2487
Reputation: 66641
The only case that this happens is when you use UpdatePanel.
Now when you use UpdatePanel, on each UpdatePanel the Dom is change and the javascript need re-initializations. Now in your case you add new lines, and you make ajax update, so you need to re-initialize the date picker.
This can be done from the functions that come with UpdatePanel as:
<script type="text/javascript">
// when dom is ready we initialize the UpdatePanel requests
$(document).ready(function () {
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
// Place here the first init of the DatePicker
$('.DateTimePicker').datepick({ dateFormat: 'dd MM yyyy' });
});
function InitializeRequest(sender, args) {
// make unbind before update the dom, to avoid memory leaks.
$('.DateTimePicker').unbind();
}
function EndRequest(sender, args) {
// after update occur on UpdatePanel re-init the DatePicker
$('.DateTimePicker').datepick({ dateFormat: 'dd MM yyyy' });
}
</script>
Similar:
Asp.Net UpdatePanel in Gridview Jquery DatePicker
Datepicker for dynamically created controls
Upvotes: 3
Reputation: 583
$(function () {
$('.DateTimePicker').datepick({ dateFormat: 'dd MM yyyy' });
});
It means it will hit once time. Try to use forEach function Like this:
$(function () {
$('.DateTimePicker').each(function () {
$(this).datepick({ dateFormat: 'dd MM yyyy' });
});
//$('.DateTimePicker').datepick({ dateFormat: 'dd MM yyyy' });
});
Upvotes: 0