postback issue when using dynamically created datetimepicker using javascript
-
Hi, I am using dynamic datetimepicker that the user can add to any number. But I am having postback problem. Once I submit it, the return page should have all the submitted values including those added dynamic datetimepicker. Right now when i submit and when the page returns, all other static controls with data are there but no dynamically added datetimepicker controls. Below is the code I used to add datatimepicker dynamically using javascript:
<html>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function () {var counter = 1; jQuery("#<%= btnD1Add.ClientID %>").click(function (event) { event.preventDefault(); //this code is added to prevent the default submit functionality of the button jQuery("* ").appendTo(".Date1More"); jQuery("<label id='lblD1StartTime" + counter + "'>StartTime:</label><input type='text' id='txtD1StartTime" +counter + "' name='txtStartTime1'/>").timepicker({ hourGrid: 10, minuteGrid: 10, timeFormat: 'hh:mm tt' }).appendTo(".Date1More"); counter++; }); }); </script>
</html>
Now how to use postback for these controls? I had tried to use following code in code but didn't work. I even tried it using !Ispostback() function under pageLoad but no effect.
protected string[] txtStartTime1;
protected void Page_Load(object sender, EventArgs e)
{txtStartTime1 = Request.Form.GetValues("txtStartTime1"); }
protected void Page_Init(object sender, EventArgs e)
{ -
Hi, I am using dynamic datetimepicker that the user can add to any number. But I am having postback problem. Once I submit it, the return page should have all the submitted values including those added dynamic datetimepicker. Right now when i submit and when the page returns, all other static controls with data are there but no dynamically added datetimepicker controls. Below is the code I used to add datatimepicker dynamically using javascript:
<html>
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function () {var counter = 1; jQuery("#<%= btnD1Add.ClientID %>").click(function (event) { event.preventDefault(); //this code is added to prevent the default submit functionality of the button jQuery("* ").appendTo(".Date1More"); jQuery("<label id='lblD1StartTime" + counter + "'>StartTime:</label><input type='text' id='txtD1StartTime" +counter + "' name='txtStartTime1'/>").timepicker({ hourGrid: 10, minuteGrid: 10, timeFormat: 'hh:mm tt' }).appendTo(".Date1More"); counter++; }); }); </script>
</html>
Now how to use postback for these controls? I had tried to use following code in code but didn't work. I even tried it using !Ispostback() function under pageLoad but no effect.
protected string[] txtStartTime1;
protected void Page_Load(object sender, EventArgs e)
{txtStartTime1 = Request.Form.GetValues("txtStartTime1"); }
protected void Page_Init(object sender, EventArgs e)
{To understand this behaviour you need to understand page life-cycle properly. I can't explain the whole stuffs here but would recommend you to read it.
Dhyanga wrote:
Is there any another way to do it?
Yes, you can very well avoid postback by using ajax or PageMethods. Just see which is best suited in your scenario.
Life is a computer program and everyone is the programmer of his own life.
-
To understand this behaviour you need to understand page life-cycle properly. I can't explain the whole stuffs here but would recommend you to read it.
Dhyanga wrote:
Is there any another way to do it?
Yes, you can very well avoid postback by using ajax or PageMethods. Just see which is best suited in your scenario.
Life is a computer program and everyone is the programmer of his own life.
-
Thank you very much Anurag. I don't know why I didn't think of using Ajax before I spent lots of time thinking on this. :)
Dhyanga
You are welcome. :)
Life is a computer program and everyone is the programmer of his own life.