Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / August 2007

Tip: Looking for answers? Try searching our database.

executing a server method after page is rendered

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jphaycock@googlemail.com - 27 Aug 2007 20:42 GMT
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
Alexey Smirnov - 27 Aug 2007 21:30 GMT
On Aug 27, 9:42 pm, "jphayc...@googlemail.com"
<jphayc...@googlemail.com> wrote:
> Hi all
>
[quoted text clipped - 17 lines]
>
> 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
Mukesh Zala - 28 Aug 2007 10:22 GMT
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

> On Aug 27, 9:42 pm, "jphayc...@googlemail.com"
> <jphayc...@googlemail.com> wrote:
[quoted text clipped - 34 lines]
> I would suggest either to think about Ajax, or change the behavior of
> the form
jphaycock@googlemail.com - 28 Aug 2007 13:53 GMT
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 <MukeshZ...@discussions.microsoft.com>
wrote:
> My understanding says you need following funcationality
> 1) before page load some javascript executes
[quoted text clipped - 4 lines]
> Use hidden field value in server side method during page load.
> Bye
jphaycock@googlemail.com - 28 Aug 2007 13:47 GMT
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
>
[quoted text clipped - 10 lines]
>
> - Show quoted text -
marss - 28 Aug 2007 14:33 GMT
On Aug 27, 10:42 pm, "jphayc...@googlemail.com"
<jphayc...@googlemail.com> wrote:
> Hi all
>
[quoted text clipped - 17 lines]
>
> 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
jphaycock@googlemail.com - 28 Aug 2007 15:16 GMT
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.
[quoted text clipped - 12 lines]
>
> - Show quoted text -
marss - 28 Aug 2007 16:14 GMT
On 28    , 17:16, "jphayc...@googlemail.com"
<jphayc...@googlemail.com> wrote:
> Thank you Mykola
>
[quoted text clipped - 21 lines]
>
> > - 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
jphaycock@googlemail.com - 28 Aug 2007 17:24 GMT
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"
[quoted text clipped - 8 lines]
>
> - Show quoted text -
marss - 29 Aug 2007 09:14 GMT
On 28    , 19:24, "jphayc...@googlemail.com"
<jphayc...@googlemail.com> 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
jphaycock@googlemail.com - 29 Aug 2007 10:58 GMT
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

> Hi John,
> The method itself does not cause looping. It may be caused by program
[quoted text clipped - 11 lines]
> Regards,
> Mykolahttp://marss.co.ua

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.