簡體   English   中英

在Visual Studio中自動創建處理程序簽名

[英]Automatically create handler signature in Visual Studio

在c#,asp.net 4.0,VS2015中工作,我創建了一個用戶控件,它只是一個下拉列表,其中包含一些用戶可以傳遞給它的其他屬性,以及一個事件和委托。 更改下拉列表時,將觸發OnSelectedIndexChanged,然后檢查是否填充了該事件並調用該委托。

定義如下:

public partial class SiteList : System.Web.UI.UserControl
{
    public event SiteIDChangedHandler SiteIDChanged;
    public delegate void SiteIDChangedHandler(object sender, EventArgs e);
}

我遇到的問題是當使用該用戶控件並附加事件時,在.aspx頁面中工作時,不會在Visual Studio中創建委托的定義。 例如,因為我輸入以下內容

<CW:SiteList runat="server" ID="SiteList" OnSiteIDChanged=""

並點擊OnSiteIDChanged的=符號,然后將“創建新事件”作為OnSiteIDChanged的選項,就像我在.net中使用的任何其他事件一樣。 當我選擇“CreateNewEvent”時,在后面的代碼中創建的方法是:

 protected void SiteList_SiteIDChanged()
    {
    }

並且不包含處理程序的正確簽名。

現在奇怪的是,如果我用后面的代碼綁定事件:SiteList.SiteIDChanged + =

它說“按(TAB)插入”,插入的方法是:

private void SiteList_SiteIDChanged1(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

那么我缺少什么,以便使用發送方創建正確的方法簽名,並且eventargs在第一個實例中自動生成? 我知道可以做到,所有.net控件都可以做到!

我能夠使用與visual studio中的控件相同的名稱來使用它。

這是我的控制代碼。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI;

namespace TestWebFormsAutoComplete
{
    public class SiteList : System.Web.UI.UserControl, IPostBackEventHandler
    {
        public event EventHandler<EventArgs> SiteIDChanged;

        public void RaisePostBackEvent(string eventArgument)
        {
            int i = 0;
        }

        protected virtual void OnSiteIDChanged(object sender, EventArgs e)
        {
            if (SiteIDChanged != null)
                SiteIDChanged(sender, e);
        }
    }
}

這是我的web.config控件/命名空間條目:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2">
      <assemblies>
        <add assembly="TestWebFormsAutoComplete"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.5.2" />      
    <pages>
      <controls>
        <add tagPrefix="test" namespace="TestWebFormsAutoComplete" assembly="TestWebFormsAutoComplete"/>
      </controls>
      <namespaces>
        <add namespace="TestWebFormsAutoComplete"/>
      </namespaces>
    </pages>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

這是我的測試aspx頁面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="TestWebFormsAutoComplete._default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        Test Site List <test:SiteList OnSiteIDChanged="Unnamed_SiteIDChanged" />
    </div>
    </form>
</body>
</html>

在我的測試中,當我填寫SiteList OnSiteIDChanged事件時,我得到一個選項“創建新事件”,我選擇了它,它為我創建了這個,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TestWebFormsAutoComplete
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Unnamed_SiteIDChanged(object sender, EventArgs e)
        {

        }
    }
}

注意,我的用戶控件沒有基於ASCX的前端,它僅在代碼中。 所以我沒有使用帶有src屬性的標記前綴。 我懷疑這就是為什么它不適合你。

雖然這似乎適用於基於代碼的控件,但我認為它不適用於基於源的ASCX用戶控件。

但是,您仍然可以創建沒有ASCX前端的UserControl。 您可以使用“Page.LoadControl”等將src標記從其他位置加載到您的控件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM