Solved

Code Behinde Function!

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 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

18 Experts available now in Live!

Get 1:1 Help Now