Problem: How to implement datetimepicker(jquery) in dynamically created rows' in gridview.
I have created a Gridview,that add' rows dynamically.I have a Year column in gridview where i want to implement the datetimepicker.But failed to do so.
Help will be highly appriciated. I am attaching the code : below codes are working perfectly and rendering the datetimepicker for only ist row.but for dynamically row's its not working. If any one have
done such kind please help me out.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data;
protectedvoid GridViewDiploma_RowCreated(object sender, GridViewRowEventArgs e) { //hide the remove button at the last row of the gridview if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = (DataTable)ViewState["CurrentTable"]; LinkButton lb = (LinkButton)e.Row.FindControl("lnkDiplomaRemove"); if (lb != null) { if (dt.Rows.Count > 1) { if (e.Row.RowIndex == dt.Rows.Count - 1) { lb.Visible = false; } } else { lb.Visible = false; } } } }
protectedvoid lnkDiplomaRemove_Click(object sender, EventArgs e) { LinkButton lb = (LinkButton)sender; GridViewRow gvRow = (GridViewRow)lb.NamingContainer; int rowID = gvRow.RowIndex + 1; if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count > 1) { if (gvRow.RowIndex < dt.Rows.Count - 1) { //Remove the Selected Row data dt.Rows.Remove(dt.Rows[rowID]); } } //store the current data in ViewState for future reference ViewState["CurrentTable"] = dt; //rebind the GridView for the updated data GridViewDiploma.DataSource = dt; GridViewDiploma.DataBind(); }
//set previous Data on Postbacks SetPreviousData(); }
privatevoid SetPreviousData() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count > 0) { for (int i = 1; i < dt.Rows.Count; i++) { TextBox box1 = (TextBox)GridViewDiploma.Rows[rowIndex].Cells[1].FindControl("txtSubject"); TextBox box2 = (TextBox)GridViewDiploma.Rows[rowIndex].Cells[2].FindControl("txtYearPassed"); TextBox box3 = (TextBox)GridViewDiploma.Rows[rowIndex].Cells[3].FindControl("txtUniversity"); TextBox box4 = (TextBox)GridViewDiploma.Rows[rowIndex].Cells[4].FindControl("txtAddress"); TextBox box5 = (TextBox)GridViewDiploma.Rows[rowIndex].Cells[5].FindControl("txtCountry");
It would be nice if you would mark the post that helped you as answer.
And yes, you need to use prerequestmanager and endrequest event
<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequest);
function EndRequest(sender, args) {
$(".MyDatePicker").datepicker();
}
</script>
basicly what this does is calls that function again after update panel updates.
For clean solution you should put datepicker "initialize" in function and call that function on document.ready and also on endrequest, something like this:
<script type="text/javascript">
function MyDatePickerInit() {
$('.MyDatePicker').datepicker();
}
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_endRequest(EndRequest);
function EndRequest(sender, args) {
MyDatePickerInit();
}
$(function () {
MyDatePickerInit();
});
</script>
Please "Mark As Answer" if the post helped you.
mitja.gti | www.mitjagti.com
gopalchettri
Member
39 Points
39 Posts
DateTime picker in dynamically created Rows in Grid View
Oct 19, 2010 09:45 AM|LINK
Hi thanx's for checking the problem.
Problem: How to implement datetimepicker(jquery) in dynamically created rows' in gridview.
I have created a Gridview,that add' rows dynamically.I have a Year column in gridview where i want to implement the datetimepicker.But failed to do so.
Help will be highly appriciated. I am attaching the code : below codes are working perfectly and rendering the datetimepicker for only ist row.but for dynamically row's its not working. If any one have done such kind please help me out.
.aspx page
<div></div>dymically created row datetime picker
GopalChettri
(MCP)
mitja.GTI
Star
11157 Points
2094 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Oct 19, 2010 11:55 AM|LINK
Hi,
is your GridView in UpdatePanel?
mitja.gti | www.mitjagti.com
gopalchettri
Member
39 Points
39 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Oct 19, 2010 11:58 AM|LINK
No, i m not using update panel
GopalChettri
(MCP)
mitja.GTI
Star
11157 Points
2094 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Oct 19, 2010 12:07 PM|LINK
Ok,
then please try to use Class instead of ID
<script type="text/javascript"> $(function () { $('.MyDatePicker').datepicker(); }); </script>GridView datepicker UI jquery
mitja.gti | www.mitjagti.com
gopalchettri
Member
39 Points
39 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Oct 19, 2010 01:04 PM|LINK
Perfect.
Thanks' its working the way i wanted.
Well one more thing, if i use update panel. datepicker is not coming. So can you tell me how can i solve this issue. !
GopalChettri
(MCP)
mitja.GTI
Star
11157 Points
2094 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Oct 19, 2010 02:04 PM|LINK
Hi,
It would be nice if you would mark the post that helped you as answer.
And yes, you need to use prerequestmanager and endrequest event
<script type="text/javascript"> var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_endRequest(EndRequest); function EndRequest(sender, args) { $(".MyDatePicker").datepicker(); } </script>basicly what this does is calls that function again after update panel updates.
For clean solution you should put datepicker "initialize" in function and call that function on document.ready and also on endrequest, something like this:
<script type="text/javascript"> function MyDatePickerInit() { $('.MyDatePicker').datepicker(); } var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_endRequest(EndRequest); function EndRequest(sender, args) { MyDatePickerInit(); } $(function () { MyDatePickerInit(); }); </script>mitja.gti | www.mitjagti.com
mani153197
Member
6 Points
3 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Nov 02, 2012 06:13 AM|LINK
Hi,
im new to Jquery libaries,
Im try to implement the above code with jquery-1.7.js,jquery-1.7-vsdoc.js versions,
Please mention which version has to be used.
Try always to reduce no of lines in Coding
mani153197
Member
6 Points
3 Posts
Re: DateTime picker in dynamically created Rows in Grid View
Nov 02, 2012 12:22 PM|LINK
thnx For both of You.... Who had raised the issue and gave the solution... :-)
Try always to reduce no of lines in Coding