Sync Javascript with ASP.Net

A

anassar

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
 
D

darrel

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
 
A

anassar

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
 
D

darrel

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
 
A

anassar

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
 
D

darrel

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
 
A

anassar

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
 
H

Hans Kesting

anassar said:
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
 
D

darrel

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
 
D

darrel

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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top