ASP.Net/Language Basics/Function — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:53, 26 мая 2010
Содержание
- 1 Call function (C#)
- 2 Define and call function (C#)
- 3 Define and call function in a page (VB.net)
- 4 Define function to change "HR" length (C#)
- 5 Function local variable (C#)
- 6 Function Parameter passed by Reference (C#)
- 7 Function Parameter passed by Value (C#)
- 8 Page level procedure (VB.net)
- 9 Simple function without parameters (C#)
- 10 Using out Parameters (C#)
Call function (C#)
<script Language="c#" runat="server">
void Page_Load()
{
DateTime DueDate;
DateTime CheckOutDate;
CheckOutDate = DateTime.Now;
DueDate = FindDueDate(CheckOutDate);
message1.Text = "<br>Checked OUT Date:" + CheckOutDate;
message2.Text = "<br>Due date is " + DueDate.ToString("d");
}
DateTime FindDueDate(DateTime CheckOutDate)
{
return CheckOutDate + TimeSpan.FromDays(14);
}
</script>
<html>
<head>
<title>Sample Function Page</title>
</head>
<body>
<asp:label id="message1" runat="server"/>
<asp:label id="message2" runat="server"/>
</body>
</html>
Define and call function (C#)
<script Language="c#" runat="server">
void Page_Load()
{
message1.Text="";
message1.Text+="<BR />Calling Function 1...";
Function1();
message1.Text+="<BR />Calling Function 2...";
Function2();
message1.Text+="<BR />Calling Function 1...";
Function1();
}
void Function1()
{
string Different = "<i>in Function 1</i>";
message1.Text+= Different;
}
void Function2()
{
string Different = "<b>in Function 2</b>";
message1.Text+= Different;
}
</script>
<html>
<head>
<title>Scope</title>
</head>
<body>
<asp:label id="message1" runat="server"/>
</body>
</html>
Define and call function in a page (VB.net)
<%@ Page Language="VB" %>
<html>
<head>
<title>Simple ASP.NET Page</title>
<script runat="server">
Sub SayHello()
Response.Write("Hello, World!")
End Sub
</script>
</head>
<body>
<% SayHello %>
</body>
</html>
Define function to change "HR" length (C#)
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load()
{
if (IsPostBack)
{
lblMessage.Text = "First Line";
InsertLinebreak(Convert.ToInt32(NumberOptions.SelectedItem.Value),
Convert.ToInt32(WidthOptions.SelectedItem.Value));
lblMessage.Text += "Second Line";
InsertLinebreak(Convert.ToInt32(NumberOptions.SelectedItem.Value),
Convert.ToInt32(WidthOptions.SelectedItem.Value));
}
}
void InsertLinebreak(int NumLines, int Width)
{
for (int i=1; i<=NumLines; i++)
{
lblMessage.Text += "<br><hr width="" + Width.ToString() +
"" align="left">";
}
}
</script>
<html>
<head>
<title>Using Functions with Parameters</title>
</head>
<body>
Choose the number and width of the linebreaks and then press submit
<form runat="server">
<asp:RadioButtonList id="WidthOptions" runat="server">
<asp:ListItem value="100">100 pixels wide</asp:ListItem>
<asp:ListItem value="300">300 pixels wide</asp:ListItem>
<asp:ListItem value="600">600 pixels wide</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList id="NumberOptions" runat="server">
<asp:ListItem value="1">1 Line</asp:ListItem>
<asp:ListItem value="2">2 Lines</asp:ListItem>
<asp:ListItem value="3">3 Lines</asp:ListItem>
</asp:DropDownList>
<asp:Button id="Button1" runat="server" text="Submit"></asp:Button>
<br />
<br />
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
Function local variable (C#)
<script Language="c#" runat="server">
void Page_Load()
{
message1.Text="";
message1.Text+="<BR />Calling Function 1...";
Function1();
message1.Text+="<BR />Calling Function 2...";
Function2();
message1.Text+="<BR />Calling Function 1...";
Function1();
}
void Function1()
{
string Different =
"<i>Hello I"m the variable Different in Function 1</i>";
message1.Text+= Different;
}
void Function2()
{
string Different =
"<b>Hello I"m the variable Different in Function 2</b>";
message1.Text+= Different;
}
</script>
<html>
<head>
<title>Scope</title>
</head>
<body>
<asp:label id="message1" runat="server"/>
</body>
</html>
Function Parameter passed by Reference (C#)
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
int a = 1;
Increment(ref a);
lblMessage.Text = a.ToString();
}
void Increment(ref int Number)
{
Number += 1;
}
</script>
<html>
<head>
<title>Demonstration of Passing a Parameter by Reference</title>
</head>
<body>
<asp:Label id="lblMessage" runat="server"></asp:Label>
</body>
</html>
Function Parameter passed by Value (C#)
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
int a = 1;
Increment(a);
lblMessage.Text = a.ToString();
}
void Increment(int Number)
{
Number += 1;
}
</script>
<html>
<head>
<title>Demonstration of Passing a Parameter by Value</title>
</head>
<body>
<asp:Label id="lblMessage" runat="server"></asp:Label>
</body>
</html>
Page level procedure (VB.net)
<%@ Page Language=VB Debug=true %>
<script runat=server>
Sub Page_Load(ByVal Sender as Object, ByVal E as EventArgs)
If IsPostBack Then
DisplayInstructions("Final")
Else
DisplayInstructions("Initial")
End If
End Sub
Sub SubmitBtn_Click(Sender As Object, E As EventArgs)
lblResult.Text = "Result: " & AddNums( _
txtNumber1.Text, txtNumber2.Text)
End Sub
Sub DisplayInstructions(Mode as String)
If Mode = "Initial" Then
lblInstructions.Text = "Enter two numbers that " _
& "you want to add."
Else
lblInstructions.Text = "The results are " _
& "below."
End If
End Sub
Function AddNums(Num1 as Single, Num2 as Single) as Single
AddNums = Num1 + Num2
End Function
</script>
<HTML>
<HEAD>
<TITLE>Creating Your Own Procedures</TITLE>
</HEAD>
<Body LEFTMARGIN="40">
<form runat="server">
<asp:label
id="lblInstructions"
runat="server"
/>
<BR><BR>
Enter a number:<BR>
<asp:textbox
id="txtNumber1"
runat=server
/>
<BR><BR>
Enter a second number:<BR>
<asp:textbox
id="txtNumber2"
runat=server
/>
<BR><BR>
<asp:button
id="butOK"
text="Add"
onclick="SubmitBtn_Click"
runat="server"
/>
<BR><BR>
<asp:label
id="lblResult"
runat="server"
/>
</Form>
</BODY>
</HTML>
Simple function without parameters (C#)
<%@ Page Language="C#" Debug="true" %>
<script runat="server">
void Page_Load()
{
lblMessage.Text = "First Line";
InsertLinebreak();
lblMessage.Text += "Second Line";
InsertLinebreak();
lblMessage.Text += "Third Line";
InsertLinebreak();
}
void InsertLinebreak()
{
lblMessage.Text += "<br><hr>";
}
</script>
<html>
<head>
<title>Simple Function Example</title>
</head>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server"></asp:Label>
</form>
</body>
</html>
Using out Parameters (C#)
<%@ Page Language="C#" %>
<script runat="server">
void Page_Load()
{
int a = 1;
int b;
Increment(a, out b);
lblMessage.Text = b.ToString();
}
void Increment(int Number, out int Result)
{
Result = Number + 1;
}
</script>
<html>
<head>
<title>Demonstration of using out Parameters</title>
</head>
<body>
<asp:Label id="lblMessage" runat="server"></asp:Label>
</body>
</html>