473,545 Members | 937 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Firing JavaScript methods from code behind

Jim
Group,

How would I go about firing a JavaScript method from a C# method in the aspx code behind file? It seems as though this could not be done since JavaScript is executed from within the Web browser and C# is executed on the server.

Thanks for your consideration,
Jim
Nov 18 '05 #1
6 4494
You are correct. But, you can add some conditions to your code-behind that
will output the correct JavaScript calls so that when the page renders, it
can run your code.

"Jim" <Ji*@discussion s.microsoft.com > wrote in message
news:46******** *************** ***********@mic rosoft.com...
Group,

How would I go about firing a JavaScript method from a C# method in the aspx code behind file? It seems as though this could not be done since
JavaScript is executed from within the Web browser and C# is executed on the
server.
Thanks for your consideration,
Jim

Nov 18 '05 #2
One solution is to add an JavaScript onLoad event to the BodyTag of the Page. This gets messy quickly as you have to get rid of the onLoad as the page posts-back or it will keep firing repeatedly.

Best practice is try to not mix the two contexts. Coordinating the form submit, client side JavaScript, .NET's own JavaScript that it renders with certain controls, and server side page processing is dicey at best. Try to keep it simple and use multiple pages or something unless the boss demands that it "pop-up" just so...

Andyz MCSD.NET
"Peter Rilling" wrote:
You are correct. But, you can add some conditions to your code-behind that
will output the correct JavaScript calls so that when the page renders, it
can run your code.

"Jim" <Ji*@discussion s.microsoft.com > wrote in message
news:46******** *************** ***********@mic rosoft.com...
Group,

How would I go about firing a JavaScript method from a C# method in the

aspx code behind file? It seems as though this could not be done since
JavaScript is executed from within the Web browser and C# is executed on the
server.

Thanks for your consideration,
Jim


Nov 18 '05 #3
Strongly disagree with the last paragraph. It's impossible to make a fast
ASP.NET application without good deal of client scripting and client-server
coordination.

Eliyahu

"Andy Z Smith" <An********@dis cussions.micros oft.com> wrote in message
news:8E******** *************** ***********@mic rosoft.com...
One solution is to add an JavaScript onLoad event to the BodyTag of the Page. This gets messy quickly as you have to get rid of the onLoad as the
page posts-back or it will keep firing repeatedly.
Best practice is try to not mix the two contexts. Coordinating the form submit, client side JavaScript, .NET's own JavaScript that it renders with
certain controls, and server side page processing is dicey at best. Try to
keep it simple and use multiple pages or something unless the boss demands
that it "pop-up" just so...
Andyz MCSD.NET
"Peter Rilling" wrote:
You are correct. But, you can add some conditions to your code-behind that will output the correct JavaScript calls so that when the page renders, it can run your code.

"Jim" <Ji*@discussion s.microsoft.com > wrote in message
news:46******** *************** ***********@mic rosoft.com...
Group,

How would I go about firing a JavaScript method from a C# method in
the aspx code behind file? It seems as though this could not be done since
JavaScript is executed from within the Web browser and C# is executed on the server.

Thanks for your consideration,
Jim


Nov 18 '05 #4
I agree with you, but I beg to differ. Take a quick look at a public domain solution to the pop-up calendar problem.

REF: http://www.csharphelp.com/archives3/archive563.html
This author has made a pretty good use of the coordination between contexts. But, as you notice, and not horrendously, but he is already using the __doPostBack functions. I urge you to find the Microsoft documentation that references this function publicly. I think that going deep into those .NET internal functions is just asking for trouble as the framework is enhanced and changed and your code breaks due to "__DoPostBa ck" being deprecated.

I agree the "power" apps are going to push the limits and use all kinds of techniques to achieve the desired effect. I think the compromise must be made between development time, functionality and future compatibility/maintainability .

My bet is on using what Microsoft has provided (namely CodeBehind) which allows for an Object Oriented view of an inherently 'chatty' client-server browser based application.

Andrew
"Eliyahu Goldin" wrote:
Strongly disagree with the last paragraph. It's impossible to make a fast
ASP.NET application without good deal of client scripting and client-server
coordination.

Eliyahu

"Andy Z Smith" <An********@dis cussions.micros oft.com> wrote in message
news:8E******** *************** ***********@mic rosoft.com...
One solution is to add an JavaScript onLoad event to the BodyTag of the

Page. This gets messy quickly as you have to get rid of the onLoad as the
page posts-back or it will keep firing repeatedly.

Best practice is try to not mix the two contexts. Coordinating the form

submit, client side JavaScript, .NET's own JavaScript that it renders with
certain controls, and server side page processing is dicey at best. Try to
keep it simple and use multiple pages or something unless the boss demands
that it "pop-up" just so...

Andyz MCSD.NET
"Peter Rilling" wrote:
You are correct. But, you can add some conditions to your code-behind that will output the correct JavaScript calls so that when the page renders, it can run your code.

"Jim" <Ji*@discussion s.microsoft.com > wrote in message
news:46******** *************** ***********@mic rosoft.com...
> Group,
>
> How would I go about firing a JavaScript method from a C# method in the aspx code behind file? It seems as though this could not be done since
JavaScript is executed from within the Web browser and C# is executed on the server.
>
> Thanks for your consideration,
> Jim


Nov 18 '05 #5
Notice that changes in the framework are making these kinds of tricks unnecessary and true to form, the __DoPostBack was changed in 1.1 causing some kind of problem...
http://weblogs.asp.net/vga/archive/2...InWhidbey.aspx

http://devhawk.net/art_submitfirefixup.aspx

Andrew S.
"Andy Z Smith" wrote:
I agree with you, but I beg to differ. Take a quick look at a public domain solution to the pop-up calendar problem.

REF: http://www.csharphelp.com/archives3/archive563.html
This author has made a pretty good use of the coordination between contexts. But, as you notice, and not horrendously, but he is already using the __doPostBack functions. I urge you to find the Microsoft documentation that references this function publicly. I think that going deep into those .NET internal functions is just asking for trouble as the framework is enhanced and changed and your code breaks due to "__DoPostBa ck" being deprecated.

I agree the "power" apps are going to push the limits and use all kinds of techniques to achieve the desired effect. I think the compromise must be made between development time, functionality and future compatibility/maintainability .

My bet is on using what Microsoft has provided (namely CodeBehind) which allows for an Object Oriented view of an inherently 'chatty' client-server browser based application.

Andrew
"Eliyahu Goldin" wrote:
Strongly disagree with the last paragraph. It's impossible to make a fast
ASP.NET application without good deal of client scripting and client-server
coordination.

Eliyahu

"Andy Z Smith" <An********@dis cussions.micros oft.com> wrote in message
news:8E******** *************** ***********@mic rosoft.com...
One solution is to add an JavaScript onLoad event to the BodyTag of the

Page. This gets messy quickly as you have to get rid of the onLoad as the
page posts-back or it will keep firing repeatedly.

Best practice is try to not mix the two contexts. Coordinating the form

submit, client side JavaScript, .NET's own JavaScript that it renders with
certain controls, and server side page processing is dicey at best. Try to
keep it simple and use multiple pages or something unless the boss demands
that it "pop-up" just so...

Andyz MCSD.NET
"Peter Rilling" wrote:

> You are correct. But, you can add some conditions to your code-behind

that
> will output the correct JavaScript calls so that when the page renders,

it
> can run your code.
>
> "Jim" <Ji*@discussion s.microsoft.com > wrote in message
> news:46******** *************** ***********@mic rosoft.com...
> > Group,
> >
> > How would I go about firing a JavaScript method from a C# method in

the
> aspx code behind file? It seems as though this could not be done since
> JavaScript is executed from within the Web browser and C# is executed on

the
> server.
> >
> > Thanks for your consideration,
> > Jim
>
>
>


Nov 18 '05 #6
Andrew,

I agree with you, one should avoid hacker-style programming. I actually
never use __doPostBack. My ASP.NET experience started from writing a few
heavily server- and code behind-oriented applications. They were slow and
GUI was bad. That was due to lack of experience with client side and clear
understanding client-server coordination. Now I tend to load the client-side
with more and more tasks. My client-side is evolving from just a set of
separate scripts to a design layer with its own infrastructure. The improve
is dramatic. My web apps are comparable to window forms ones in speed and
more attractive in GUI. The development indeed takes longer, but I am sure
the development tools for client side will improve.

Happy programming,

Eliyahu
"Andy Z Smith" <An********@dis cussions.micros oft.com> wrote in message
news:B4******** *************** ***********@mic rosoft.com...
I agree with you, but I beg to differ. Take a quick look at a public domain solution to the pop-up calendar problem.
REF: http://www.csharphelp.com/archives3/archive563.html
This author has made a pretty good use of the coordination between contexts. But, as you notice, and not horrendously, but he is already using
the __doPostBack functions. I urge you to find the Microsoft documentation
that references this function publicly. I think that going deep into those
..NET internal functions is just asking for trouble as the framework is
enhanced and changed and your code breaks due to "__DoPostBa ck" being
deprecated.
I agree the "power" apps are going to push the limits and use all kinds of techniques to achieve the desired effect. I think the compromise must be
made between development time, functionality and future
compatibility/maintainability .
My bet is on using what Microsoft has provided (namely CodeBehind) which allows for an Object Oriented view of an inherently 'chatty' client-server
browser based application.
Andrew
"Eliyahu Goldin" wrote:
Strongly disagree with the last paragraph. It's impossible to make a fast ASP.NET application without good deal of client scripting and client-server coordination.

Eliyahu

"Andy Z Smith" <An********@dis cussions.micros oft.com> wrote in message
news:8E******** *************** ***********@mic rosoft.com...
One solution is to add an JavaScript onLoad event to the BodyTag of the
Page. This gets messy quickly as you have to get rid of the onLoad as the page posts-back or it will keep firing repeatedly.

Best practice is try to not mix the two contexts. Coordinating the
form submit, client side JavaScript, .NET's own JavaScript that it renders with certain controls, and server side page processing is dicey at best. Try to keep it simple and use multiple pages or something unless the boss demands that it "pop-up" just so...

Andyz MCSD.NET
"Peter Rilling" wrote:

> You are correct. But, you can add some conditions to your
code-behind that
> will output the correct JavaScript calls so that when the page
renders, it
> can run your code.
>
> "Jim" <Ji*@discussion s.microsoft.com > wrote in message
> news:46******** *************** ***********@mic rosoft.com...
> > Group,
> >
> > How would I go about firing a JavaScript method from a C# method
in the
> aspx code behind file? It seems as though this could not be done
since > JavaScript is executed from within the Web browser and C# is

executed on the
> server.
> >
> > Thanks for your consideration,
> > Jim
>
>
>


Nov 18 '05 #7

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

Similar topics

6
3849
by: Rod Snyder | last post by:
I'm trying to set up a page with an asp.net link button that would send a user to a certain page and on page load execute a specific stored procedure tied to the button on the previous page. The link would be in a nav area and be something like "update". It would send the user to the main page and execute the stored procedure associated with...
1
4291
by: Rick | last post by:
Hello all, I hope all is well with you. I am having a seriously difficult time with this problem. Allow me to set up the problem. I have a System.Web.UI.Page with the following controls (watch the layout, some have child controls):
1
8357
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's...
13
5635
by: Chris | last post by:
I can create Javascript confirm message boxes during page creation, etc adding them to the button attributes (many good posts on this!). But how can I add this event after the button is pressed? I have created an Is_Dirty routine checking for field changes on my page then if the user clicks on the "exit" button I check for Is_Dirty = true and...
1
4409
by: Paul L | last post by:
Hi, I have an issue with the OnSelectedIndexChanged event not firing for a DropDownList control which is in the ItemTemplate of a DataList. I have made an exact copy of the DropDownList control, and placed it outside of the DataList and it fires the event just fine. So it's definitely to do with it being in a template. I've managed to...
8
2599
by: Frank | last post by:
Hi, I am working with VS.NET 2005 Ultimately, I wish to call a JavaScript function from a .js file
3
5492
by: Jay | last post by:
I am on the 2.0 framework and have run the c:\windows\microsoft.net \framework\v1.1.4322\aspnet_regiis.exe -c and had no success. About half of the buttons on my webforms are firing and the other half are not, primarily anything on the Master is firing but those in the content pane are not. This was working fine yesterday!! I've reviewed...
2
1700
by: NvrBst | last post by:
Hello. I posted this problem in the microsoft.public.dotnet.languages.csharp origionally but moving it here since I think it probably belongs here now. I have a blank ASP.NET/C# page with a code behind file. I made the following function in the code behind file public string GetFlower(){TEST.Flower myFlower = new TEST.Flower(); return...
6
3542
Frinavale
by: Frinavale | last post by:
Apparently I have a lot of questions today regarding JavaScript security. I've implemented a JavaScript Object that intercepts page submits (postbacks) and then displays a UI prompting the user to confirm(yes)/deny(no)/cancel(close UI/cancel submit) their action. There may be additional JavaScript methods to execute before displaying the UI...
0
7467
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7656
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7419
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7756
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3442
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1014
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
703
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.