ASP.NET Questions

T

Tony

Hi,

I hope I can find answers to my questions here.

1. How to execute a Private Sub in VB, from a Javascript function event.
2. How to get a JavaScript Var value from the html page and return to the VB
Codebehind.
3. If there is a way to execute a JavaScript function before the VB Code
behind page_load event.

Waiting anxiously for your favourable reply.

TIA

Tony
 
L

Lau Lei Cheong

Tony said:
Hi,

I hope I can find answers to my questions here.

1. How to execute a Private Sub in VB, from a Javascript function event.
1) Use javascript to link event to a button, then call the Sub from that
event handler(can be added by "AddHandler" statement); or
2) make use of "__dopostback()" javascript call.
(Both methods are too long to describe here, try Google them using keywords
in double quotes.)
2. How to get a JavaScript Var value from the html page and return to the VB
Codebehind.
On the onclick property of submit button, put something like onclick="return
addValue()".
Then define a function like:
function addValue() {
document.getElementById("hidden1").value = var1;
return true;
}
Where "hidden1" is a server-side input hidden control, and var1 is the
variable.
3. If there is a way to execute a JavaScript function before the VB Code
behind page_load event.
No. There's only one round-trip - client to server, then back to client when
finished.
 
K

Kevin Spencer

Hi Tony,

Let me give you the principles, and you should be able to solve any
client-side/server-side problem from those.

The client and the server are disconnected. HTTP is stateless. IOW, HTTP is
simply a Request/Response messaging protocol. Every Request is sent isolated
from any other Request, and every Response is sent isolated from any other
Response. In essence, you have a series of Request/Response pairs that are
isolated from each other.

So, how does the client send data to the server, and how does the server
send data to the client? The answer should be obvous: The only way to pass
data from the client is via a Request. The only way to pass data from the
server to the client is via a Response. This is why ASP.Net uses the
PostBack model to handle client-side events on the server. The PostBack is a
way of the client "reminding" the server of what the status of the last Page
instance WAS.

More specifically, you have JavaScript functions on the client which can
interact with the client browser and the HTML document in the browser. So,
how does a JavaScript function pass data to the server? It adds the data to
the form, which is POSTed to the server via PostBack or simple form POST.

Now to your questions, in light of the principles I have outlined (hopefully
this should appear obvious at this point):
1. How to execute a Private Sub in VB, from a Javascript function event.

The JavaScript function adds data to the form, which is POSTed to the
server. The server Page class handles the Request. It looks for the data
that the JavaScript function has passed, and deals with it if it is present,
according to the data, and how the class is designed to handle such data.
BTW, in essence, this is how PostBacks work. A client-side HTML form element
(which represents a server-side Control) passes a value to the client-side
JavaScript _doPostBack() function, which adds the value to a hidden form
field, and then Posts the data (with all the form data) to the server. On
the server, an event handler method receives the data from the _doPostBack()
function during the LoadPostData event.
2. How to get a JavaScript Var value from the html page and return to the
VB Codebehind.

A JavaScript function adds the variable value to the form, which is POSTed
to the server.
3. If there is a way to execute a JavaScript function before the VB Code
behind page_load event.

Well, that one is a little tricky to explain. It is possible, but you have
to understand that in the lifetime of a Page, there can be many PostBacks.
Technically, as HTTP is stateless, we are not talking about the lifetime of
A Page, but the lifetimes of many instances of a Page. Obviously, if the
page was initially called via a URL or Response.Redirect, there would be no
JavaScript present on the client to execute. Once the Page is loaded, the
JavaScript could be part of the Page, and do what we've already discussed,
adding data to the next Request. In that case, there are 3 different events
in the Page class that occur before Load (Init, LoadViewState,
LoadPostData). So, in that case only, yes, the JavaScript function would
happen (in time) before the Page_Load event, and the data from the function
could be handled prior to the Page_Load event.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 
T

Tony

Thanks Kevin and Lau,
I really appriciate your help, I am really thankfull to you for that.
I learned a lot from your postings.

Keep it up you really are great peoples.

Thanks

Tony
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top