Answered by:
Get the updated value from a span

Question
-
User1897897189 posted
Dears,
Using the below link I have created a counter. The span is getting updated every single second. My question is how can I get the latest or updated value in code behind?
https://codepen.io/TLJens/pen/azedap
Thanks
Nick
Tuesday, June 1, 2021 9:40 AM
Answers
-
User475983607 posted
nicklibee
My question is how can I get the latest or updated value in code behind?AJAX
https://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
Or the UpdatePanel
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 1, 2021 9:51 AM -
User475983607 posted
nicklibee
what i want is just the lblTimeout Value as it is changing every second . I tried to use the lblTimeout.text but it is always returning 1:00 .
Use the Visual Studio debugger to find the bug(s) in your code.
It seems like you are make no effort whatsoever to read the documentation or do an internet search for a solution. The following openly published UpdatePanel walk-through shows how to create a timer which can easily be used as a timeout.
https://docs.microsoft.com/en-us/previous-versions/aspnet/bb386404(v=vs.100)
If you do not wish to use standard Web Forms Server Controls then you must use AJAX and a Web Method. Please make an effort to read the docs and share your code. At this point, it seems you are asking the community to design and write your code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 1, 2021 1:23 PM
All replies
-
User475983607 posted
nicklibee
My question is how can I get the latest or updated value in code behind?AJAX
https://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx
Or the UpdatePanel
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 1, 2021 9:51 AM -
User1897897189 posted
tried this unfortunately i am not getting what i want
Tuesday, June 1, 2021 10:32 AM -
User475983607 posted
nicklibee
tried this unfortunately i am not getting what i want
We can't read your mind or see your latest code.
If you want a community code review then share the source code. Explain how you expect the source code to work and what actually happens.
Tuesday, June 1, 2021 10:58 AM -
User1897897189 posted
Following is my code;
The below is from https://codepen.io/TLJens/pen/azedap site
<script type="text/javascript">
var interval;
function countdown() {
clearInterval(interval);
interval = setInterval(function () {
var timer = $('.clspantimeout').html();
timer = timer.split(':');
var minutes = timer[0];
var seconds = timer[1];
seconds -= 1;
if (minutes < 0) return;
else if (seconds < 0 && minutes != 0) {
minutes = 0;
seconds = 59;}
else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
$('.clspantimeout').html(minutes + ':' + seconds);
if (minutes == 0 && seconds == 0)
clearInterval(interval);}, 1000);
}
window.onload = function () {
countdown();
};
</script>
<asp:Label ID="lblTimeOut" runat="server" class="clspantimeout" Text="1:00" Font-Bold="True"></asp:Label>
protected void btnSubmit_Click(object sender, EventArgs e)
{
string spanto = lblTimeOut.Text;
}What I want is the current value of lblTimeout ?
If I use string spanto = lblTimeOut.Text, it is always showing "1:00". It is not getting updated.
Thanks
Tuesday, June 1, 2021 11:32 AM -
User475983607 posted
You have to actually write code to send the value to the server. Secondly, labels are not posted to a server only inputs are posted. You did not share any code that does a post so it is not clear what approach you are using. Please read the links in my previous post and make an attempt to design and write code to meet your requirements.
Tuesday, June 1, 2021 11:41 AM -
User1897897189 posted
The label value is not getting reflected in code behind because the label value is changed in the client side
tried your code but unfortunately it is not giving me the updated label value.
Tuesday, June 1, 2021 11:54 AM -
User753101303 posted
Hi,
Only form field values are posted back to a web server so the behavior you see is expected. You could; copy the value to an hidden form field for example.
Some more context could help. For example if this is for a quizz another option could be to save the current data/tome on the server side when the quizz is shown and when the response is submitted etc... this counter being just a reminder etc...
Tuesday, June 1, 2021 12:13 PM -
User1897897189 posted
this is for a quiz thats correct, but how can i assign values to hidden variable using js?
Tuesday, June 1, 2021 12:20 PM -
User475983607 posted
nicklibee
this is for a quiz thats correct, but how can i assign values to hidden variable using js?
Use a standard selector. Any beginning level JavaScript tutorial covers the fundamentals.
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
ASP.NET Web Forms ccreates dynamic IDs so you'll need to use a code block to render a server control's ID.
'<%=HiddenField1.ClientUD %>'
You can also use an element selector or class selector. Read the docs.
Also, you're current design most likely will not work. It looks like you are doing a full page post back which will disrupt the quiz. For the third time, use AJAX or an UpdatePanel. If you are using AJAX or an UpdatePanel then share the actual code. Explain the expected behavior and actual behavior.
Tuesday, June 1, 2021 12:41 PM -
User1897897189 posted
what i want is just the lblTimeout Value as it is changing every second . I tried to use the lblTimeout.text but it is always returning 1:00 .
Tuesday, June 1, 2021 1:08 PM -
User475983607 posted
nicklibee
what i want is just the lblTimeout Value as it is changing every second . I tried to use the lblTimeout.text but it is always returning 1:00 .
Use the Visual Studio debugger to find the bug(s) in your code.
It seems like you are make no effort whatsoever to read the documentation or do an internet search for a solution. The following openly published UpdatePanel walk-through shows how to create a timer which can easily be used as a timeout.
https://docs.microsoft.com/en-us/previous-versions/aspnet/bb386404(v=vs.100)
If you do not wish to use standard Web Forms Server Controls then you must use AJAX and a Web Method. Please make an effort to read the docs and share your code. At this point, it seems you are asking the community to design and write your code.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, June 1, 2021 1:23 PM