简体   繁体   English

无法生成ASP.net事件

[英]ASP.net Event not Generating

Why does it happen (only some times) that when I add an event handler to a control it doesn't give the intellisense option to generate a new event handler. 为什么会发生(仅几次),当我向控件添加事件处理程序时却没有提供智能提示选项来生成新的事件处理程序。 This results in Visual studio 2012 not creating the code in the background to associate the control with the event. 这导致Visual Studio 2012无法在后台创建将控件与事件相关联的代码。

I can go an manually create the event, but like I said, it creates other background code to associate the control with that event handler. 我可以手动创建事件,但是就像我说的,它会创建其他背景代码来将控件与该事件处理程序相关联。

asp.net asp.net

 <asp:DropDownList ID="drpdwnRecordId" runat="server" Visible="false" OnSelectedIndexChanged="MyOwnEventHandler_OnSelectedIndexChanged">
        <asp:ListItem Text="Please Select a Record ID" Value="nothing"></asp:ListItem>
    </asp:DropDownList>

c# C#

  protected void MyOwnEventHandler_OnSelectedIndexChanged(object sender, EventArgs e)
        {

        }

Why is this happening? 为什么会这样呢? How can I fix this, or is there a way that I can go and write that background code myself (and is it a good idea?) 我该如何解决此问题,或者有办法自己编写该后台代码(这是个好主意吗?)

If you put it in the ui page it will generate all the code needed to call the function at compile time. 如果将其放在ui页面中,它将生成在编译时调用该函数所需的所有代码。 You have to compile your project for that code to be created. 您必须编译项目才能创建该代码。

There should be no additional code needed (written by you) besides what you show here. 除了此处显示的内容外,不需要其他任何代码(由您编写)。

You can go to Code Behind by Right Clicking page (.aspx,.ascx) and select View code. 您可以转到“通过右键单击隐藏代码”页面(.aspx,.ascx),然后选择“查看代码”。 You can assosiate your event in OnInit or OnLoad method. 您可以在OnInit或OnLoad方法中关联事件。

But you must have definition of your control in that page! 但是您必须在该页面中定义控件!

DropDownList dropdwnRecordId;

protected void override OnLoad(...)
{
    dropdwnRecordId.SelectedIndexChanged += YourHandler;
}

Regards, Dmitry. 问候,德米特里。

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

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