Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTML
<input type="text" id="datepicker_value"  style="border:none; "  />


Above input get populated when i click on following inline date picker
HTML
<div id="datepicker"   ></div>


Up till now its work fine. But i need to get value of input to the label, when i click click button, which is in different div but same page.

ASP.NET
<asp:Label ID="update_lbl" runat="server" ></asp:Label>

ASP.NET
<asp:Button ID="BTN_confirmation"  runat="server" Text="Proceed to Testing Confirmation Page" />
Posted

1 solution

I think I understand what you're trying to do... You'll have to call this on the client side, but if you're button is doing a postback, I believe the text on the label will be reset anyway.

JavaScript
function setText() {
   $('#<%=update_lbl.ClientID%>').text($('#datepicker_value').val())
}


And in your button, you can TEST like this. This will STOP the postback.
OnClientClick="setText(); return false;"
 
Share this answer
 
v2
Comments
anjumnavid 13-Jul-12 9:10am    
Thanks for reply. This does solve my problem but i need post back on button. is any way i can save this datepicker value in var or page var , so i can get this value later ?
PopeDarren 13-Jul-12 9:47am    
Is the button causing a full page post? Or are you using an update panel?
anjumnavid 13-Jul-12 11:27am    
button causing a full page post ... i need full page post for the rest of the value.
After i click on button , The div i am working on will hide and 2 more hidden div become visible , which make a a call to database to update grid and my print div. in my print div i need selected date
PopeDarren 13-Jul-12 12:13pm    
As you know text that is set using JavaScript isn't preserved through full page posts. So if you're set on using JavaScript, you're going to have to do the following:

1. Change the update_lbl to an asp:TextBox.
2. Set the readonly attribute to true.
3. Change the client click code on the button: OnClientClick="setText();"
3. Change your jQuery to this:
function setText() {
$('#<%=update_lbl.ClientID%>').val($('#datepicker_value').val());
}
4. On the server side, in the button click event request the labels text. The text has been set by JavaScript, but we have to make the ViewState know that it needs to be a different value:
Private Sub BTN_confirmation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_confirmation.Click
Me.update_lbl.Text = Request.Form(update_lbl.UniqueID)
End Sub

And that will preserve the value that has been changed in JavaScript through a postback. You'll just have to make that textbox look like a label, which isn't difficult.

(OPTION 2) Of course, it would be much simpler if you could get the date from the datepicker_value input. Then you could cut out all of the client side code, leave your label a label, and do this in your button click event on the server side:

Private Sub BTN_confirmation_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BTN_confirmation.Click
Me.update_lbl.Text = Request.Form(datepicker_value.UniqueID)
End Sub

This is much cleaner. It may not be possible, however, because your date picker needs to have the following attribute added: runat="server", and that attribute may cause problems for the rest of the page. Or it might be a third party object and you don't have the option to change it.

Hope that helps!
PopeDarren 13-Jul-12 12:31pm    
Whoops! What I said about Option 2 was wrong! You don't have to add the runat="server" attribute, you just need to add a name.

<input type="text" id="datepicker_value" name="datepicker_value" style="border:none; " />

Then in your button click:

Me.update_lbl.Text = Request.Form("datepicker_value")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900