Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. When the dropdown is changed, the OnSelectedIndexChanged is triggered, which then checks if the event is populated and calls the delegate.

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. 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. When I selected "CreateNewEvent", the method which is created in the code behind is:

 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 +=

It says "Press (TAB) to insert" and the method which is inserted is:

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? I know it can be done, all of the .net controls do it!

like image 547
LarryBud Avatar asked Nov 08 '22 15:11

LarryBud


1 Answers

I was able to get this to work using the same name as your control in 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:

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

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

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. So I am not using a tag prefix with a src attribute. 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.

However you can still make a UserControl with no ASCX front end. You can use "Page.LoadControl" etc to load src markup into your control from someplace else.

like image 158
Ryan Mann Avatar answered Nov 14 '22 23:11

Ryan Mann