executing a server method after page is rendered

Discussion in 'ASP .Net' started by jphaycock@googlemail.com, Aug 27, 2007.

  1. Guest

    Hi all

    I am calling a method in a Page_Load event that makes use of a value
    from a text box.

    Unfortunately the textbox value is only set after a javascript runs
    and the page is rendered. Therefore the textbox is actually empty when
    the Page_Load event fires.

    Is there a way to execute the method after the javascript has run and
    the page has rendered.

    I can do it by adding a button and placing the call to the method in
    its onClick event but this is not acceptable in terms of user
    interface. It must happen onLoad.

    Can I maybe "fake" a button click?

    Thanks in advance

    John
     
    , Aug 27, 2007
    #1
    1. Advertising

  2. On Aug 27, 9:42 pm, ""
    <> wrote:
    > Hi all
    >
    > I am calling a method in a Page_Load event that makes use of a value
    > from a text box.
    >
    > Unfortunately the textbox value is only set after a javascript runs
    > and the page is rendered. Therefore the textbox is actually empty when
    > the Page_Load event fires.
    >
    > Is there a way to execute the method after the javascript has run and
    > the page has rendered.
    >
    > I can do it by adding a button and placing the call to the method in
    > its onClick event but this is not acceptable in terms of user
    > interface. It must happen onLoad.
    >
    > Can I maybe "fake" a button click?
    >
    > Thanks in advance
    >
    > John


    John,

    you can do a button click, but it requires a postback to the server

    <input type="button" id="bbb" style="display:none" runat="server"
    onserverclick="...">

    in js

    var btn = document.getElementById("bbb");
    btn.click();

    I would suggest either to think about Ajax, or change the behavior of
    the form
     
    Alexey Smirnov, Aug 27, 2007
    #2
    1. Advertising

  3. My understanding says you need following funcationality
    1) before page load some javascript executes
    2) on page load event some server side method uses value of textbox.
    you can try in this way.
    declare html hidden control with runat server side, when javascript executes
    store value of textbox in hidden field.
    Use hidden field value in server side method during page load.
    Bye

    "Alexey Smirnov" wrote:

    > On Aug 27, 9:42 pm, ""
    > <> wrote:
    > > Hi all
    > >
    > > I am calling a method in a Page_Load event that makes use of a value
    > > from a text box.
    > >
    > > Unfortunately the textbox value is only set after a javascript runs
    > > and the page is rendered. Therefore the textbox is actually empty when
    > > the Page_Load event fires.
    > >
    > > Is there a way to execute the method after the javascript has run and
    > > the page has rendered.
    > >
    > > I can do it by adding a button and placing the call to the method in
    > > its onClick event but this is not acceptable in terms of user
    > > interface. It must happen onLoad.
    > >
    > > Can I maybe "fake" a button click?
    > >
    > > Thanks in advance
    > >
    > > John

    >
    > John,
    >
    > you can do a button click, but it requires a postback to the server
    >
    > <input type="button" id="bbb" style="display:none" runat="server"
    > onserverclick="...">
    >
    > in js
    >
    > var btn = document.getElementById("bbb");
    > btn.click();
    >
    > I would suggest either to think about Ajax, or change the behavior of
    > the form
    >
    >
     
    =?Utf-8?B?TXVrZXNoIFphbGE=?=, Aug 28, 2007
    #3
  4. Guest

    Cheers for the answer Alex

    I tried this but it didn't work. it couldn't find the serverside
    method

    John

    > you can do a button click, but it requires a postback to the server
    >
    > <input type="button" id="bbb" style="display:none" runat="server"
    > onserverclick="...">
    >
    > in js
    >
    > var btn = document.getElementById("bbb");
    > btn.click();
    >
    > I would suggest either to think about Ajax, or change the behavior of
    > the form- Hide quoted text -
    >
    > - Show quoted text -
     
    , Aug 28, 2007
    #4
  5. Guest

    Thank you Mukesh

    This gives me the same type of problem. I cannot get the value of the
    hidden field until after the page has completely rendered so i can't
    refer to it in the page_load event.

    John

    On 28 Aug, 10:22, Mukesh Zala <>
    wrote:
    > My understanding says you need following funcationality
    > 1) before page load some javascript executes
    > 2) on page load event some server side method uses value of textbox.
    > you can try in this way.
    > declare html hidden control with runat server side, when javascript executes
    > store value of textbox in hidden field.
    > Use hidden field value in server side method during page load.
    > Bye
     
    , Aug 28, 2007
    #5
  6. marss Guest

    On Aug 27, 10:42 pm, ""
    <> wrote:
    > Hi all
    >
    > I am calling a method in a Page_Load event that makes use of a value
    > from a text box.
    >
    > Unfortunately the textbox value is only set after a javascript runs
    > and the page is rendered. Therefore the textbox is actually empty when
    > the Page_Load event fires.
    >
    > Is there a way to execute the method after the javascript has run and
    > the page has rendered.
    >
    > I can do it by adding a button and placing the call to the method in
    > its onClick event but this is not acceptable in terms of user
    > interface. It must happen onLoad.
    >
    > Can I maybe "fake" a button click?
    >
    > Thanks in advance
    >
    > John


    As I see you have to do something with value in textbox after it was
    changed.
    Try to use OnTextChanged event handler insted of Page_Load:

    <asp:TextBox ID="TextBox1" runat=server AutoPostBack=true
    OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    //call your method here
    }

    Regards,
    Mykola
    http://marss.co.ua
     
    marss, Aug 28, 2007
    #6
  7. Guest

    Thank you Mykola

    Alas, that still requires user interaction after the page has loaded.
    I need this to happen without the user clicking a button or changing
    text. The value of the textbox is populated by a javascript and the
    OnTextChanged event isn't fired by this action.

    John


    > As I see you have to do something with value in textbox after it was
    > changed.
    > Try to use OnTextChanged event handler insted of Page_Load:
    >
    > <asp:TextBox ID="TextBox1" runat=server AutoPostBack=true
    > OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    > protected void TextBox1_TextChanged(object sender, EventArgs e)
    > {
    > //call your method here
    >
    > }
    >
    > Regards,
    > Mykolahttp://marss.co.ua- Hide quoted text -
    >
    > - Show quoted text -
     
    , Aug 28, 2007
    #7
  8. marss Guest

    On 28 , 17:16, ""
    <> wrote:
    > Thank you Mykola
    >
    > Alas, that still requires user interaction after the page has loaded.
    > I need this to happen without the user clicking a button or changing
    > text. The value of the textbox is populated by a javascript and the
    > OnTextChanged event isn't fired by this action.
    >
    > John
    >
    > > As I see you have to do something with value in textbox after it was
    > > changed.
    > > Try to use OnTextChanged event handler insted of Page_Load:

    >
    > > <asp:TextBox ID="TextBox1" runat=server AutoPostBack=true
    > > OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
    > > protected void TextBox1_TextChanged(object sender, EventArgs e)
    > > {
    > > //call your method here

    >
    > > }

    >
    > > Regards,
    > > Mykolahttp://marss.co.ua-Hide quoted text -

    >
    > > - Show quoted text -


    View page source and find how ASP.Net rendered textbox. It have to be
    something like this:
    <input name="TextBox1" type="text"
    onchange="javascript:setTimeout('__doPostBack(\'TextBox1\',\'\')',
    0)" ...

    Call __doPostBack('TextBox1','') in your javascript after you changed
    value..

    Regards,
    Mykola
    http://marss.co.ua
     
    marss, Aug 28, 2007
    #8
  9. Guest

    Hi Mykola

    Thank you

    This worked but it keeps firing repeatedly. Do you know how i can make
    it postback only once?

    John

    View page source and find how ASP.Net rendered textbox. It have to be
    > something like this:
    > <input name="TextBox1" type="text"
    > onchange="javascript:setTimeout('__doPostBack(\'TextBox1\',\'\')',
    > 0)" ...
    >
    > Call __doPostBack('TextBox1','') in your javascript after you changed
    > value..
    >
    > Regards,
    > Mykolahttp://marss.co.ua- Hide quoted text -
    >
    > - Show quoted text -
     
    , Aug 28, 2007
    #9
  10. marss Guest

    On 28 , 19:24, ""
    <> wrote:

    > This worked but it keeps firing repeatedly. Do you know how i can make
    > it postback only once?



    Hi John,
    The method itself does not cause looping. It may be caused by program
    logic. It is hard to me to give more definite advice. Perhaps, if you
    want to call some method in Page_Load (not in textbox's OnTextChanged
    event handler) you have to test whether this postback was caused by
    change in textbox. I mean that if you call __doPostBack('TextBox1','')
    in javascript then you can process this postback in Page_Load
    if (Request.Params["__EVENTTARGET"] == "TextBox1")
    {
    //call some method here
    }

    Regards,
    Mykola
    http://marss.co.ua
     
    marss, Aug 29, 2007
    #10
  11. Guest

    Thanks again Mykola

    The problem seems to be that each time the page loads the javascript
    is executed and this forces a postback so it gets stuck in a loop.

    I may have to rethink how I'm going about this

    Cheers

    John



    On 29 Aug, 09:14, marss <> wrote:
    > Hi John,
    > The method itself does not cause looping. It may be caused by program
    > logic. It is hard to me to give more definite advice. Perhaps, if you
    > want to call some method in Page_Load (not in textbox's OnTextChanged
    > event handler) you have to test whether this postback was caused by
    > change in textbox. I mean that if you call __doPostBack('TextBox1','')
    > in javascript then you can process this postback in Page_Load
    > if (Request.Params["__EVENTTARGET"] == "TextBox1")
    > {
    > //call some method here
    >
    > }
    >
    > Regards,
    > Mykolahttp://marss.co.ua
     
    , Aug 29, 2007
    #11
    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. Robin Dindayal

    Print rendered .aspx page to server's printer

    Robin Dindayal, Jul 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    607
    Robin Dindayal
    Jul 25, 2003
  2. Daniel Walzenbach
    Replies:
    7
    Views:
    611
    Steven Cheng[MSFT]
    Feb 10, 2004
  3. NIan
    Replies:
    2
    Views:
    463
  4. Ed
    Replies:
    1
    Views:
    80
    Da Costa Gomez
    Jul 18, 2003
  5. Replies:
    3
    Views:
    185
Loading...

Share This Page