Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I call many times server side functions from JavaScript using page methods.

C#
function botaoCarregarNotas() { //server side button
     PageMethods.carregarNotas(verTexto); // função que vai buscar toda a informaçaõ
 }

 // recebe informação das notas da carregarNotas do lado do servidor
function verTexto(resultado){
var num = resultado[0];
if(num==0)

....


Somewhere on Page_Load, I want to execute server side function carregarNotas(vertexto) without pressing a button, so I suppose I have to execute first JS function botaoCarregarNotas()

how could I do that?
Posted

Your question us not very clear. But if yo want call your Javascript function. That can be done by registering your method as.
- If you are using AJAX on your page
ScriptManager.RegisterStartupScript(Page, typeof(System.Web.UI.Page),
                                                    "botaoCarregarNotas", @"<script type='text/javascript'>botaoCarregarNotas();</script>", false);


- If not using AJAX

Page.ClientScript.RegisterStartupScript(typeof(System.Web.UI.Page),
                                                            "botaoCarregarNotas", @"<script type='text/javascript'>botaoCarregarNotas();</script>");
 
Share this answer
 
Comments
Maxdd 7 26-Dec-10 22:55pm    
I had a bug with my solution, yours worked very well, thank you very much.
Brij 26-Dec-10 22:56pm    
Thanks :)
void Page_Load(...)
{
  if(!IsPostBack)
  {
     carregarNotas();
  }
}


You have been told before, spend some time with a book on JavaScript and ASP.NET rather than stumbling through asking questions. Learning it correctly the first time will save you more effort.
 
Share this answer
 
v2
Comments
Maxdd 7 26-Dec-10 22:33pm    
I did that but didnt worked out. I know Mark, I know that's the problem. I'm really trying.

Related to the problem, I solved using a session variable on .aspx and then I use JS if necessary.

Thanks again.
[no name] 26-Dec-10 22:40pm    
Stop saying "I know" and start doing!!

Also if you want continued help then stop replying to an answer with "I already solved it". Take the time to think before posting and stop annoying us!!!

It is also a courtesy to edit your question to show the answer you came up with, it may help others with the same question.
Maxdd 7 26-Dec-10 22:47pm    
Ok i'll start sharing the solution.

I say I know because I know you're right. Honestly I dont like to read and google for asp.net solutions. I always get a lot of different solutions, there are a lot of .NET versions, a lot of controls and many programming languages that work with .NET. That's why sometimes I gave up to easily - it's very difficult to find usefull information when you dont know what and where to look. That's why I post so many questions.

But of course I should at least read a ASP.NET book, just to know how the framework works. I will search for some tutorials soon.
Maxdd 7 26-Dec-10 22:49pm    
By the way I take my time to think, that situation happened two or three times always in situations you helped, but it was coincidence.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900