简体   繁体   English

在Visual Studio中自动创建处理程序签名

[英]Automatically create handler signature in Visual Studio

Working in c#, asp.net 4.0, VS2015, I've created a user control which is just a dropdown list with a couple of other properties that the user can pass to it, as well as an event and delegate. 在c#,asp.net 4.0,VS2015中工作,我创建了一个用户控件,它只是一个下拉列表,其中包含一些用户可以传递给它的其他属性,以及一个事件和委托。 When the dropdown is changed, the OnSelectedIndexChanged is triggered, which then checks if the event is populated and calls the delegate. 更改下拉列表时,将触发OnSelectedIndexChanged,然后检查是否填充了该事件并调用该委托。

The definition look like this: 定义如下:

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

The problem I'm having is when that user control is consumed, and the event is attached, the definition of the delegate is not created in Visual Studio when working in the .aspx page. 我遇到的问题是当使用该用户控件并附加事件时,在.aspx页面中工作时,不会在Visual Studio中创建委托的定义。 For example as I'm typing the following 例如,因为我输入以下内容

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

and hit the = sign for OnSiteIDChanged, it then says "Create New Event" as an option for OnSiteIDChanged, just like any other event I've used in .net. 并点击OnSiteIDChanged的=符号,然后将“创建新事件”作为OnSiteIDChanged的选项,就像我在.net中使用的任何其他事件一样。 When I selected "CreateNewEvent", the method which is created in the code behind is: 当我选择“CreateNewEvent”时,在后面的代码中创建的方法是:

 protected void SiteList_SiteIDChanged()
    {
    }

and doesn't contain the proper signature of the handler. 并且不包含处理程序的正确签名。

Now what's strange is if I bind the event in the code behind with this: SiteList.SiteIDChanged += 现在奇怪的是,如果我用后面的代码绑定事件:SiteList.SiteIDChanged + =

It says "Press (TAB) to insert" and the method which is inserted is: 它说“按(TAB)插入”,插入的方法是:

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

So what am I missing so that the proper method signature is created with the sender and eventargs is automatically in the first instance? 那么我缺少什么,以便使用发送方创建正确的方法签名,并且eventargs在第一个实例中自动生成? I know it can be done, all of the .net controls do it! 我知道可以做到,所有.net控件都可以做到!

I was able to get this to work using the same name as your control in visual studio. 我能够使用与visual studio中的控件相同的名称来使用它。

Here is my control code. 这是我的控制代码。

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);
        }
    }
}

Here is my web.config control/namespace entries: 这是我的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>

And here is my test aspx page: 这是我的测试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>

In my test, when I filled out the SiteList OnSiteIDChanged event, I got a drop with the option to "Create New Event", I chose that, and it created this for me, 在我的测试中,当我填写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)
        {

        }
    }
}

Note, I do not have an ASCX based front end for my user control, it's in code only. 注意,我的用户控件没有基于ASCX的前端,它仅在代码中。 So I am not using a tag prefix with a src attribute. 所以我没有使用带有src属性的标记前缀。 I suspect that's why it doesn't work for you. 我怀疑这就是为什么它不适合你。

While this does seem to work with Code based controls, I do not think it will work with source based ASCX user controls. 虽然这似乎适用于基于代码的控件,但我认为它不适用于基于源的ASCX用户控件。

However you can still make a UserControl with no ASCX front end. 但是,您仍然可以创建没有ASCX前端的UserControl。 You can use "Page.LoadControl" etc to load src markup into your control from someplace else. 您可以使用“Page.LoadControl”等将src标记从其他位置加载到您的控件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM