Firing JavaScript methods from code behind

Discussion in 'ASP .Net' started by =?Utf-8?B?Smlt?=, Jun 15, 2004.

  1. 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
     
    =?Utf-8?B?Smlt?=, Jun 15, 2004
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
     
    Peter Rilling, Jun 15, 2004
    #2
    1. Advertising

  3. 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" <> wrote in message
    > news:...
    > > 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

    >
    >
    >
     
    =?Utf-8?B?QW5keSBaIFNtaXRo?=, Jun 16, 2004
    #3
  4. 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" <> wrote in message
    news:...
    > 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" <> wrote in message
    > > news:...
    > > > 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

    > >
    > >
    > >
     
    Eliyahu Goldin, Jun 16, 2004
    #4
  5. 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 "__DoPostBack" 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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > > > news:...
    > > > > 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
    > > >
    > > >
    > > >

    >
    >
    >
     
    =?Utf-8?B?QW5keSBaIFNtaXRo?=, Jun 16, 2004
    #5
  6. FOLLOWUP: Re: Firing JavaScript methods from code behind

    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/2004/03/01/NoMoreHijackingOfDoPostBackInWhidbey.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 "__DoPostBack" 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" <> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > > news:...
    > > > > > 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
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >
     
    =?Utf-8?B?QW5keSBaIFNtaXRo?=, Jun 16, 2004
    #6
  7. 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" <> wrote in message
    news:...
    > 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 "__DoPostBack" 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" <> wrote in message
    > > news:...
    > > > 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" <> wrote in message
    > > > > news:...
    > > > > > 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
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >
     
    Eliyahu Goldin, Jun 17, 2004
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    593
    Alphonse Giambrone
    Jun 28, 2003
  2. Ken Tucker
    Replies:
    2
    Views:
    2,564
    Ken Tucker
    Jul 6, 2003
  3. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,245
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  4. John

    Non-code behind to code behind

    John, Feb 19, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    491
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Feb 19, 2007
  5. segue

    code behind not firing

    segue, Mar 11, 2009, in forum: ASP .Net
    Replies:
    4
    Views:
    462
    segue
    Mar 18, 2009
Loading...

Share This Page