Solved

Code Behinde Function!

Posted on 2004-10-13
2
361 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 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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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