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!