?
Solved

Code Behinde Function!

Posted on 2004-10-13
2
Medium Priority
?
362 Views
Last Modified: 2006-11-17
Hi All,
i need to know how to call a function in the code behinde(server side) from the client side(html part) in asp.net using vb.net???
any comment will be very helpful
thanx in advanced.
samjav
0
Comment
Question by:samjav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 20

Accepted Solution

by:
ihenry earned 150 total points
ID: 12295665

If you mean make a direct method call to a server-side function from client-side javascript, it would be more appropriate to say "force a postback from client javascript". Read this article
How postback works in ASP.NET
http://www.xefteri.com/articles/dec102002/default.aspx

If you mean to call code-behind function from aspx or using inline code you can use old school asp Response.Write shortcut like so
<% CallToMethod1() %>, where CallToMethod1 is a function defined in code-behind.
0
 
LVL 5

Assisted Solution

by:stumpy1
stumpy1 earned 150 total points
ID: 12297248
Here is an example of how you would do it on an aspx page to call a code-behind function to create a file:

Lets say that the function that you need to call in the code behind is for a linkbutton called CreateFile_Click

Then In the header part of the HTML for the aspx page you need to declare the javascript to call you code-behind function (ie. CreateFile)
<script language="javascript">
<!--
function CreateFile()
{
      fileName = prompt('Type the name of the file you want to create:','');
      if ((fileName) && (fileName!=""))
      {
            document.forms['BrowseFiles'].elements['funcParam'].value = fileName;
            __doPostBack('CreateFile', '');
      }
}
// -->
</script>
This uses the javascript __doPostBack function that is created by asp.net to postback to the CreateFile_Click function in the code-behind.

Then in the HTML on the page inside your form you will need additional code such as the following:

<input id="funcParam" type="hidden" runat="server">
...
<A href="javascript:CreateFile();"><IMG height="28" alt="Create a new text file" src="./Images/NewFile.gif" width="28" border="0">
</A>
<asp:linkbutton id="CreateFile" onclick="CreateFile_Click" Runat="server" Visible="False"></asp:linkbutton>

Notice that there is a linkbutton control that is not visible that is the actual control that we are posting back to too do the work. Ie. Clicking on the <A href... link calls the clientside javascript function which sets the value of a hidden field. The Javascript function then uses the automatically generated client side javascript function to perform the postback to the server side function for the linkbutton control (CreateFile_Click)

I hope this helped.
PS. This is paraphrased from the Wrox book "ASP.NET Website Programming, Problem - Design - Solution"
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question