473,385 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Functions in asp.net

In asp I have the code like this:

<%
sub createUrl(name,link,clas)
if clas=1 then
class="top"
else
class="left"
end if
url="<a class='"&class&"' href='"&link&"'>"&name&"</a>"
response.write url
end sub
%>
....
<tr><td><%=createUrl("page","page.asp",1)</td></tr>
<tr><td><%=createUrl("page1","page1.asp",2)</td></tr>
....

enywhere in page I need something I just call the sub or function with
<%Call sub or function%>.
I don't know how I can create the same in asp.net. I read one book but I
didn't found any example.

Can someone help me?

Thank you,
Simon
Jul 21 '05 #1
1 1170
Of course you can! I have developed an example for you using the familiar
single-page approach. Although, the real power of .net lies in separating
your code and content... using what's called "code-behind" pages... but
that's a little more advanced.

Code sample shown below.

Key concepts:
1. Because you are asking for a return value, you would want to use a
function rather than a sub generally.

2. You must declare (dim) any variables that you are using in your function,
and specify their datatype. By default, "option explicit" is on, meaning
that you must declare all variables. I'm not going to go into the issue of
scope here, but keep in mind that a variable declared in your function is
only available from within the function. You should thoroughly understand
scope and the use of "Public/Private/Shared..." subs and functions, if you
are serious about using .NET.

3. You must put spaces between your concatenation operators - e.g. don't
smush all your code next to your & characters or it will throw an error.

4. You may NEVER use the word "class" as a variable. Class is a reserved
keyword. Recommend appending a three letter prefix to all variables
indicating the datatype. .NET is what's called a "strongly typed" language,
meaning you have to always make sure variables contain data of an
appropriate type.

5. The return line is what will be returned from the function call. This
could be a string, integer, array/collection, or any other type of object.

<CODE>
<%@ Page Language="vb" Debug="true" %>
<script runat="server">
function createUrl(strName,strLink,intClass)
dim strClass as string
dim strUrl as string
if intClass=1 then
strClass="top"
else
strClass="left"
end if

strUrl="<a class='" & strClass & "' href='" & strLink & "'>" & strName
& "</a>"
return strUrl
end function
</script>
<html>
<head>
</head>
<body>
<%=createUrl("MyLink","http://www.mylink.com",1)%>
</body>
</html>
</CODE>
"Simon" <si*********@stud-moderna.si> wrote in message
news:3f********@news.s5.net...
In asp I have the code like this:

<%
sub createUrl(name,link,clas)
if clas=1 then
class="top"
else
class="left"
end if
url="<a class='"&class&"' href='"&link&"'>"&name&"</a>"
response.write url
end sub
%>
...
<tr><td><%=createUrl("page","page.asp",1)</td></tr>
<tr><td><%=createUrl("page1","page1.asp",2)</td></tr>
...

enywhere in page I need something I just call the sub or function with
<%Call sub or function%>.
I don't know how I can create the same in asp.net. I read one book but I
didn't found any example.

Can someone help me?

Thank you,
Simon

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: hokiegal99 | last post by:
A few questions about the following code. How would I "wrap" this in a function, and do I need to? Also, how can I make the code smart enough to realize that when a file has 2 or more bad...
99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
21
by: Rubén Campos | last post by:
I haven't found any previous message related to what I'm going to ask here, but accept my anticipated excuses if I'm wrong. I want to ask about the real usefulness of the 'inline' keyword. I've...
17
by: cwdjrxyz | last post by:
Javascript has a very small math function list. However there is no reason that this list can not be extended greatly. Speed is not an issue, unless you nest complicated calculations several levels...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
7
by: Tim ffitch | last post by:
Hi I have created a VB dll file that contains common functions I use across various projects in VB, Access and Excel. Rather than have to code the functions in each I decided to use the dll...
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.