ASP.Net/Language Basics/Function

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Call function (C#)

   <source lang="csharp">

<script Language="c#" runat="server">

 void Page_Load() 
 {
   DateTime DueDate;
   DateTime CheckOutDate;
   CheckOutDate = DateTime.Now;
   DueDate = FindDueDate(CheckOutDate);
   message1.Text = "
Checked OUT Date:" + CheckOutDate; message2.Text = "
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>

      </source>
   
  


Define and call function (C#)

   <source lang="csharp">

<script Language="c#" runat="server">

 void Page_Load()
 {
   message1.Text="";
   message1.Text+="
Calling Function 1..."; Function1(); message1.Text+="
Calling Function 2..."; Function2(); message1.Text+="
Calling Function 1..."; Function1(); } void Function1() { string Different = "in Function 1"; message1.Text+= Different; } void Function2() { string Different = "in Function 2"; message1.Text+= Different; }

</script> <html> <head> <title>Scope</title> </head> <body> <asp:label id="message1" runat="server"/> </body> </html>

      </source>
   
  


Define and call function in a page (VB.net)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Define function to change "HR" length (C#)

   <source lang="csharp">

<%@ 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 += "

";
       }
   }

</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>
       

<asp:Label id="lblMessage" runat="server"></asp:Label> </form>

</body> </html>

      </source>
   
  


Function local variable (C#)

   <source lang="csharp">

<script Language="c#" runat="server">

 void Page_Load()
 {
   message1.Text="";
   message1.Text+="
Calling Function 1..."; Function1(); message1.Text+="
Calling Function 2..."; Function2(); message1.Text+="
Calling Function 1..."; Function1(); } void Function1() { string Different = "Hello I"m the variable Different in Function 1"; message1.Text+= Different; } void Function2() { string Different = "Hello I"m the variable Different in Function 2"; message1.Text+= Different; }

</script> <html> <head> <title>Scope</title> </head> <body> <asp:label id="message1" runat="server"/> </body> </html>

      </source>
   
  


Function Parameter passed by Reference (C#)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Function Parameter passed by Value (C#)

   <source lang="csharp">

<%@ 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>

      </source>
   
  


Page level procedure (VB.net)

   <source lang="csharp">

<%@ 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"

/>

Enter a number:
<asp:textbox

   id="txtNumber1" 
   runat=server 

/>

Enter a second number:
<asp:textbox

   id="txtNumber2" 
   runat=server 

/>

<asp:button

   id="butOK"
   text="Add"
   onclick="SubmitBtn_Click" 
   runat="server"

/>

<asp:label

   id="lblResult" 
   runat="server"

/> </Form> </BODY> </HTML>

      </source>
   
  


Simple function without parameters (C#)

   <source lang="csharp">

<%@ 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 += "

";
   }

</script> <html> <head>

   <title>Simple Function Example</title>

</head> <body>

   <form runat="server">
       <asp:Label id="lblMessage" runat="server"></asp:Label>
   </form>

</body> </html>

      </source>
   
  


Using out Parameters (C#)

   <source lang="csharp">

<%@ 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>

      </source>