473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sync Javascript with ASP.Net


I have an <asp:label id="lblPrice" runat="server"/> tag that I update
on the client side using Javascript by referring to the object ID
_ctl0_lblPrice.

The Javascript does client-side calculations depending on the user's
selections of choices on the web form.

The problem I have is that ASP.Net does not seem to know anything about
the changes made by Javascript upon postback.

My question is: how do I get ASP.Net to know the latest client-side
updates to lblPrice without having to duplicate the client-side
calculations on the server side ?

Please advise with examples as this is new territory for me. Please
note I can get Javascript to modify ASP.Net objects, but I cannot get
ASP.Net to see any updates Javascript has made to these objects.
Having Javascript post the updates back to ASP.net in a request object
defeats the purpose of what I am looking for. I need to have ASP.Net
detect the latest updates Javascript made to the <asp:label> object.

Aladdin Nassar

Nov 19 '05 #1
10 1697
> I need to have ASP.Net
detect the latest updates Javascript made to the <asp:label> object.


javascript can't manipulate an asp:label. The label doesn't exist on the
client side. AFAIK, the only way to have javascript update server side is to
have the javascript either post the data back to the server, or have
javascript write to a field that can then be sent back upon a form
submission.

-Darrel
Nov 19 '05 #2
javascript can't manipulate an asp:label. The label doesn't
exist on the client side.


Actually, I did. Just prepend a _ctl0_ to asp:label ID and insert the
following in your javascript:

GetObject('_ctl0_lblPrice').innerText='Hello There';

where lblPrice is the asp:label's ID.

And GetObject is a Javascript Function that simply finds that object in
DOM irrespective of the browser like:

-----------------------------------------------------------------------------
<script LANGUAGE="Javascript">
<!--

if (top.location != self.location){top.location = self.location;} //
get out of framed browsing
function onerror(){return(true);} // true turns off error
messages

var isDHTML = 0;
var isLayers = 0;
var isAll = 0;
var isID = 0;

if (document.getElementById){
isID = 1; isDHTML = 1;
}else{
if(document.all){
isAll = 1; isDHTML = 1;
}else{
browserVersion = parseInt(navigator.appVersion);
if((navigator.appName.indexOf('Netscape') != -1) && (browserVersion
== 4)){
isLayers = 1; isDHTML = 1;
}
}
}

function GetObject(objectID){
if(isID){return(document.getElementById(objectID)) ;}
if(isAll){return(document.all[objectID]);}
if(isLayers){return (document.layers[objectID]);}
return null;
}

//-->
</script>
-----------------------------------------------------------------------------

My question still remains please. How do I get ASP.Net to see the
updates that Javascript made to the lblPrice object. In the above
example, Javascript changed lblPrice value to 'Hello There'. How do I
get ASP.Net to see that change on the server side ?

Aladdin Nassar

Nov 19 '05 #3
> Actually, I did. Just prepend a _ctl0_ to asp:label ID and insert the
following in your javascript:

GetObject('_ctl0_lblPrice').innerText='Hello There';

where lblPrice is the asp:label's ID.
There is NO asp:label on the client side. It's not an HTML tag. It's an
ASP.net tag. It gets parsed on the server before it is ever sent to the
browser.
My question still remains please. How do I get ASP.Net to see the
updates that Javascript made to the lblPrice object.
My answer still remains. Have it postback or write to a form field.
In the above
example, Javascript changed lblPrice value to 'Hello There'.


It did nothing of the sort. It simply rewrote some text in the HTML after
the page loaded in the browser.

-Darrel
Nov 19 '05 #4
I think we meant the same thing in different words. I did want to know
how the postback will communicate the updated HTML label _ctl0_lblPrice
for the example I listed.

Can you please give me a specific example of (a) the postback and (b)
writing back to a form field ?

All my URL's are Get Requests. In Scenario (b), can you please show me
how I can hide the updated _ctl0_lblPrice from the GET URL to avoid
having the user spoof the server so easily ?

Aladdin Nassar

Nov 19 '05 #5
> Can you please give me a specific example of (a) the postback and (b)
writing back to a form field ?
Probably not, since I avoid using javascript for that type of thing.

But, for example, I suppose you could so something like this:

<input type="text" id="yourVariable" style="postition: absolute;
left: -999px">

That would create a text box off screen that you could change the text of
via javascript. Then, upon postback, you could read that value. I doubt
that's the best way to do it, though.
All my URL's are Get Requests. In Scenario (b), can you please show me
how I can hide the updated _ctl0_lblPrice from the GET URL to avoid
having the user spoof the server so easily ?


Well, my example certainly isn't secure.

I think your best option is to just do the calculations server-side. That
way you avoid all of these issues.

-Darrel
Nov 19 '05 #6

If your input textbox example works, then should it also work for a
hidden <DIV> tag that I can read on postback ? And that is exactly why
I was asking my question: I thought that the <asp:label> tag gets
translated into a pure HTML tag like <DIV> that I should be able to
read on the server side without doing anything special.

I tried using a hidden <DIV> tag like I mentioned above that I set
using Javascript and the problem I found is that the <DIV> gets
re-initialized to its initial state every time you postback. Which I
think is what is going to happen with your input textbox solution.

Where did I go wrong ?

Aladdin Nassar

Nov 19 '05 #7
anassar wrote:
If your input textbox example works, then should it also work for a
hidden <DIV> tag that I can read on postback ? And that is exactly
why I was asking my question: I thought that the <asp:label> tag gets
translated into a pure HTML tag like <DIV> that I should be able to
read on the server side without doing anything special.

I tried using a hidden <DIV> tag like I mentioned above that I set
using Javascript and the problem I found is that the <DIV> gets
re-initialized to its initial state every time you postback. Which I
think is what is going to happen with your input textbox solution.

Where did I go wrong ?

Aladdin Nassar


Only <input>s get posted back to the server, no <div>s, <span>s etc.
So anything that you change client-side in a div or span will not get transferred
to the server.
The only option (apart from building the entire querystring "by hand") is to
copy those changes to inputs (use <input type=hidden>), which *will*
get posted back to the server.

Hans Kesting
Nov 19 '05 #8
Thank you very much Hans & Darrel for your clarification.

Regards,

Aladdin Nassar

Nov 19 '05 #9
> If your input textbox example works, then should it also work for a
hidden <DIV> tag that I can read on postback ?
A DIV has no value that get's sent back to the server. Only a form input
does.
And that is exactly why
I was asking my question: I thought that the <asp:label> tag gets
translated into a pure HTML tag like <DIV> that I should be able to
read on the server side without doing anything special.


Nope...that's just a one way trip for the DIV. Only forms fields can be sent
BACK to the server (or querystrings).

-Darrel
Nov 19 '05 #10
> The only option (apart from building the entire querystring "by hand") is
to
copy those changes to inputs (use <input type=hidden>), which *will*
get posted back to the server.


But can you change the value of a input type=hidden via javascript and have
it sent back with the form? I thought it had to be a tangible, editable text
field to do that?.

-Darrel
Nov 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: kartik | last post by:
I open an fstream in read-only mode, read till the end, then try to sync() before seeking to position 0 & reading again. But the sync fails. I discovered that clear()ing the stream before the sync...
5
by: Mark | last post by:
How do I scroll two textareas in sync with FireFox/Mozilla? The IE solution has been posted before by Martin Honnen.
7
by: Ole | last post by:
Hi, I'm going to develop a socket communication between an instrument (running CE 5.0 with Compact Fraework V2) and a PC. As the instrument should only connect to one PC at a time I believe that...
7
by: john | last post by:
I am reading TC++PL3 and on page 644 it is mentioned: "Flushing an istream is done using sync(). This cannot always be done right. For some kinds of streams, we would have to reread characters...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.