Check if Value Has Changed

B

BigJohn

Is there a way to check all controls to see if the value has changed without
using JavaScript to maintain a value or a persistant connection to the server
for events?

I always thought the ViewState could be used since it contained the values
at the time of page rendering, but I can't seem to find any way to reference
the old value.

I am trying to keep the client JavaScript out of the picture if possible and
reduce the server round trips to the final Submit if possible.

Thank you
 
S

Steven Cheng[MSFT]

Hello John,

As for detecting value change on ASP.NET web page, if you want to avoid
server round trips, I think client-side script code is not avoidable. Also,
since we need to compare to the original value(so as to determine whether
the certain input has been changed), it is also necessary to maintain the
original value(for each input element you want to monitor) in page's
response so that the client-script can access. For the ViewState, ASP.NET
is rendering it in a encoded format(undocumented), therefore, we can not
directly get value from it at client-side.

I'm wondering what kind of input controls will you need to check the value
changing, Textbox? Currently, I have a suggestion about using the control's
"PreRender" event to add an additional property to its client-side html
tag( to store its old value). e.g.

#Actually you can consider create a custom Textbox class(derived from the
TextBox) and add the additional property, this provide more flexiblility
and reusability(in multiple pages or projects)
===========
Protected Sub TextBox1_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.PreRender
TextBox1.Attributes("oldvalue") = TextBox1.Text
End Sub
==============

then, at client-side, the TexBox is rendered as below html code:

<input name="TextBox1" type="text" id="TextBox1" oldvalue=".............."
/>

And you can use script code to query it, e.g.

===========
var txt = document.getElementById("TextBox1");

alert(txt.oldvalue);
=============

In such cases, the old value will be automatically updated to the latest
value in the Prerender event. What left for us is use client-side code to
check the old value and compare it with lastest value. How do you think of
this?


Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

BigJohn

I like adding the additional attribute. This one can be reused across all my
textbox occurances for the same purpose.

Thank you very much.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top