Solved

Code Behinde Function!

Posted on 2004-10-13
2
355 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
2 Comments
 
LVL 20

Accepted Solution

by:
ihenry earned 50 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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 …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now