Datepicker not appearing after adding rows to editable gridview

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!

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

$(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' });
    });


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x