How to call a javascript function from a server-side function

Hi. I'd like to know if it's possible to call a javascript function from within a server-side function.

I know it's possible to associate a javascript function with a control event but I'd like to call it from within an asp.net function.

 Thanks,

Samir Zattar

0
samirzattar
5/29/2007 7:05:26 PM
asp.net.client-side 24353 articles. 2 followers. Follow

5 Replies
1149 Views

Similar Articles

[PageSpeed] 32

Hey,

There are several ways to do it, all through the Page.ClientScript property.  You can write a script through RegisterClientScriptBlock or RegisterStartupScript, or if you have a script file you can write a reference to that file.


Brian

"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
0
bmains
5/29/2007 7:21:17 PM

Hi,

as server-side code runs before the client-side code (per request), this is usually achieved so that you generate a js call on the server-side code, and by the time page gets rendered in the browser, the generated script is executed.

The world's simplest example to demonstrate this is to call this on Button's click:

 

protected void Button1_Click(object sender, EventArgs e)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "alertcall", "alert('This call was generated from server-side code')", true); 
    }
 
Thanks,

Teemu Keiski
Finland, EU
0
joteke
5/29/2007 7:23:57 PM

Thanks for the reply, but doing what you described would only work with a post-back, right?
I'd like to call the function using AJAX functionality, with no post-backs.

Samir Zattar

0
samirzattar
5/29/2007 7:33:49 PM

In that case, you could register the call with ScriptManager. here's an example

 

<asp:ScriptManager ID="ScriptManager1" runat="server" />
    
           <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />    
            </ContentTemplate> 
           </asp:UpdatePanel>
  
protected void Button1_Click(object sender, EventArgs e)
    {
         ScriptManager.RegisterStartupScript(this,typeof(Page), "alertcall", "alert('This call was generated from server-side code with async postback')",true); 
    }
 
Thanks,

Teemu Keiski
Finland, EU
0
joteke
5/29/2007 8:02:43 PM

That will do it! Thanks

0
samirzattar
5/30/2007 3:45:48 PM
Reply:

Similar Artilces:

How to call server side(C#) function from Client side(Javascript) function?
Hi i have used below functions,  function HandleOnClose() {// this function will be called after clicking browsers close button in Internet explorer if (event.clientY < 0) { event.returnValue = 'Are you sure you want to leave the page?'; } // if he clicks ok then i need to call one server side function in this instance.. else not. } <body onbeforeunload="HandleOnClose()" leftmargin="0" rightmargin="0">other controls in page..</div>  How to achieve this? Thanks MS AJAX Extensions would be the option of choice here. Combined with webservices you can...

how to call server side function from client side javascript?
Hi all, I have a image button in the update panel. I would like to call a method from server side (not from webservice) to get the variable when Onclick on the image button is invoked. Is there any way that I can able to access this function from the client script? In using the UpdatePanel, in essence, you're already doing this because there will not be a full postback.  You can make an asynchronous call to your Click method of your ImageButton.  So, what do you want to do with this "variable"?Christopher Reed"The oxen are slow, but the earth is patient."...

How to call server side function from client side JavaScript?
I want to call a server-side function, like a button's click event or just another private or public function , from a client-side JavaScript function. How do I do that? thanX AnzIf this post was useful to you, please mark it as answer.ClientSideAsp.Net | Blog Maybe alittle more that you need, but....... 1) Put a button on the page. Lets say its id is Button1. Make sure that its has causesValidation=false 2) Write the code for the button. 3) Write the following in the PAge_Load event Button1.Attributes.Add("onClick", "myJSFunction(); return false") 4) Copy this javascript...

Call server side function from client side javascript
I have a javascript where I either need to call an onclick even of a server link button or I need to be able to call a server side function.   The only thing is I cant use AJAX.  Is there any way I can do this or am I spinning my wheels. Thanks!-------------Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue. I have a similar problem.  In my case, I would like to set a server-side session object from JavaScript.  Any suggestions?  Thanks.  Dan Sure, y...

Can you call a c# on the server side from javascript functions on client side
Hello guys This is something intresting I have come across during development. I have a javascript written in a .JS file , to my knowledge the .js files are executed on the server side. But security concerns with java script that these .js files can be access by connecting to port 80 of the server . So an ultimate alternative reminas to call c# code from a java script function. Can anyone Help me on this matter Regards Nihar Hi Nihar, Usually .js files executed on client side. Please explain your configuration.Leon LangleybenMCSD, ASP.NET MVPBlog...

How to call server side function on client side
Hi,   How to call server side function on client side? here i want to call change() function on textbox onchange event.   <script language="javascript">    function serverfunction()    {    }    </script><input id="Text1" onchange="javascript:serverfunction();"  type="text" /> code behind:  public void change()    {        Response.Write("hello");    } <script type=&quo...

how to call server side function in javascript function
right now I have a question. I have a javascript function which created a textfield. what I want to do is when the user type in the value into the textfield, it will call the function in the server side and search the data in the database. I wonder is it possible to do that? And how to do it, can anyone some me some example..thx    look into ajax autocomplete Liming XuJumptree ASP.NET 2.0 Project Management - With Source Code, Free to Non Profit Org and Effectively Manage Projects/Tasks/Milestones Superwong-You definitely want to use Ajax (as was suggested). Since you're u...

call a server side function from javascript function
hello i have apage that is contained in MasterPage (contents) i have a HyperLink and i want to call a serverside function is it possible? i am writing C# nirnironIf i helped you please mark as answered.. in your page_load event, you can reference controls on the masterpage by MasterPage.FindControl("....")  Peter Kellnerhttp://73rdstreet.com and blogging athttp://PeterKellner.netMVP, ASP.NET  Use the "LinkButton" control...  Looks the same as a hyperlink, acts the same, and you can catch events from it. no... ok this is the scenario... i have ...

Calling a client side function from the server side
Is it possible to call a client side function from the server side. I dont want to invoke that function through button click. I just want to call that to do some operations.  Regards, Aish Use the below code ASP.NET 2.0 Page.ClientScript.RegisterStartUpScript(this.GetType(),"callfunction","<script language=javascript>Test();</script>"); ASP.NET 1.X Page.RegisterStartUpScript("callfunction","<script language=javascript>Test();</script>"); Where Test() is the javascript function. HCHaissam Abdul MalakMCAD.NET| Blog | No, it is not possible.  The cl...

Is it possible to call client side function from server side code without initial client request?
Atlas is great. I'm using timer to periodically check database changes on server to populate client side controls with new data. Is it possible to do it without a lot of client requests to server which takes a huge useless network traffic? Server should call client side function on all connected clients when, for example, 'database updated' event occurs on server. Thanks. Hi~ Just for your interest, I think you could have a look at Comet, many web IM like Gmail's embeded Gtalk and meebo.com are built on this architecture你好! Just FYI o_O...

calling server side function from client side script
i have a web form that includes a function named "koko()" in its server side class i want to write a script code in in its HTML script that will be run at the client side, and this client side script will call the function "koko()" from the server side. in brief : i want to call a server side function from a client side script... can you help me in this You can't call server-side code directly from client-side code. You can, however, trigger a postback that your server-side code watches for and triggers the server-side code appropriately. You can do some ...

Call client side function from server side code
My problem is that in some point of the VB.Net code I need to call a function defined in vbScript, for example: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   'There's some code here....   ' ...   If (SomeCondition) Then   'HERE'S WHERE I NEED TO CALL THE JAVASCRIPT FUNCTION... SO HOW CAN I DO THAT?   Else     '...   End If   ' ... End Sub The asp.net code runs to generate the html and when it is done the html is rendered in your browser. So you can't access cli...

Client side script calling Server side functions
Hi, I'm currently developing an ajax.net enabled aspx page that contains a usercontrol (which is based on the listbox but extends it to include icons to represent invalid data). http://www.djcharlie.co.uk/WeeeCareResolutionCentre.jpg I want the onClick event of the selected TR to call an asp.net server function which then populates the controls to the right hand side in the page. Obviously because the row is just rendered HTML overriden from the base class I can only assign a javascript function to the onClick event. So I need a way of calling a server side function that will populate th...

How to Call server side and client side function on same LinkButton
hello,         i have one linkbutton when i click on linkbutton i insert record in DB using onclick=Linkbutton_click. if successfully insert then returnvalue is 1. after return 1 i want to call javascript function. how i can do this? Reply ASAP. Is It Possible? e.g. <asp:LinkButton ID="Operation_Link" runat="server" OnClick="Operation_Link_Click"></asp:LinkButton>protected void Operation_Link_Click(object sender, EventArgs e) {if (Operation_Link.Text == "SUBMIT") {ReturnValue = company.InsertCompany...

Web resources about - How to call a javascript function from a server-side function - asp.net.client-side

Server Side Includes - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

Google Cloud Platform simplifies server-side development for iOS w/ ‘Mobile Backend Starter’
... Mobile Backend Starter that first debuted for Android developers back in June. Google says the application helps to simplify the server-side ...

Free Cooling: the Server Side of the Story
Servers that support higher inlet temperatures could significantly reduce or even eliminate the cooling costs of the data center. We received ...


[Update: Check Now] Play Store For Android Gets Search Bar With Hamburger Flip In Server-Side Update
Google is pushing out an update to the Play Store's Android interface that replaces the action bar with a standard search bar. Inside are the ...

Server-side update makes Siri a stickler for succinctness
Siri is already well known for her wit and willingness to joke, but a recent server-side update has given Apple's digital assistant the ability ...

Japanese RIAA wants server-side music DRM for mobile phones
The Recording Industry Association of Japan—the Japanese version of the RIAA— …

New BaaS platform aims to eliminate writing server-side code
... offering for desktop and mobile developers that's now leaving its beta phase. By removing the need for writing code on the server-side, Backendless ...

Dart improves async and server-side performance
... Team Today's release of the Dart SDK version 1.3 includes a 2x performance improvement for asynchronous Dart code combined with server-side ...

Customize Google Using Server-Side Scripts
Google's services have a lot of powerful features, but they can't offer all the options requested by users. A popular way to customize Google ...

Resources last updated: 11/23/2015 4:17:04 AM