executing a server method after page is rendered

  • Thread starter Thread starter jphaycock
  • Start date Start date
J

jphaycock

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
 
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
 
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
 
Cheers for the answer Alex

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

John
 
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
 
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
 
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
 
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

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
 
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
 
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
 
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
 
Back
Top