c#依頼入門説明例


ファイルtest 1.aspx
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class test1 : System.Web.UI.Page
{
    /// <summary>
    ///      ,       ,public
    /// </summary>
    /// <param name="strUrl">url  </param>
    /// <param name="strTitle">    </param>
    /// <returns></returns>
    public delegate string  DelegateSampleSum(string strUrl, string strTitle);
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_delegateTest_Click(object sender, EventArgs e)
    {
        //         appendString
        DelegateSampleSum deTest = new DelegateSampleSum(appendString);
        lbl_show.Text = deTest("http://www.my400800.cn", "400  ");
    }

    /// <summary>
    ///          ,            (         ,           )
    /// </summary>
    /// <param name="addNum"></param>
    /// <returns></returns>
    public string appendString(string strUrl, string strTitle)
    {
        string strRet = "";
        strRet = string.Concat("<a href=\"", strUrl, "\">", strTitle, "</a>");
        return strRet;

    }
}

 
ファイルtest 1.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test1.aspx.cs" Inherits="test1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:Label ID="lbl_show" runat="server" Text="      "></asp:Label>
        <br />
        <br />
        <br />
        <asp:Button ID="btn_delegateTest" runat="server" 
            onclick="btn_delegateTest_Click" Text="c#    " />
    
    </div>
    </form>
</body>
</html>
 
 
出力Html結果

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
	Untitled Page
</title></head>
<body>
    <form name="form1" method="post" action="test1.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTE2MjY5MTY1NQ9kFgICAw9kFgICAQ8PFgIeBFRleHQFLjxhIGhyZWY9Imh0dHA6Ly93d3cubXk0MDA4MDAuY24iPjQwMOeUteivnTwvYT5kZGRZZEKD2p9IkYmfo7145J/l+5q8cQ==" />
</div>

<div>

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWAgLxorv8AwK+3u2SA3AjPKDa3B6y3blg0YPBn1vLksJn" />
</div>
    <div>
    
        <span id="lbl_show"><a href="http://www.my400800.cn">400  </a></span>
        <br />
        <br />
        <br />
        <input type="submit" name="btn_delegateTest" value="c#    " id="btn_delegateTest" />
    
    </div>
    </form>
</body>
</html>

 
 
表示結果の出力
 
Untitled Page
400電話
 
注意:
 
 
 public delegate string  DelegateSampleSum(string strUrl, string strTitle);
関数#カンスウ#
public string appendString(string strUrl, string strTitle)     {         string strRet = "";         strRet = string.Concat("", strTitle, "");         return strRet;     }
 
の戻り値変数の数は、変数タイプが一致している必要があります.そうしないと、コンパイルできません.