简体   繁体   中英

ASP.Net Button Click Event Handler not auto generated in code-behind, placed in script tags on page instead

I've been wrestling with this all day. Opening up an old web application trying to add a method to my page (default.aspx) and have my code behind default.aspx.cs which is referenced in the page:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %>
<%@ Import Namespace="HoursWorked" %>

Some of the symptoms I'm having are intellisense not "seeing" any of the controls from the page, when I double click the button control in the Designer, it creates the following:

<script runat="server">

protected void ButtonShowUnderEight_Click(object sender, EventArgs e)
{

}
</script>

Code behind starts off as (after all of my using statements):

namespace HoursWorked
{
    public partial class _Default : Page
    {

Any ideas?

Thanks, Tim

Replace

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %>

with

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="._Default" %>

It does not like when you inherit

Inherits="HoursWorked.Default"

PS, you can also remove

<%@ Import Namespace="HoursWorked" %>

I ended up going back to my original local solution instead of trying to use the ones on my dev and prod servers. No clue why they were having this issue compared to my original local solution.

Not going to help anyone else having this issue, but instead of investing more time trying to solve this... I'm sure if I compared everything in my solution folder, something might stick out.

Tim

In my case (VS2013) what helped was based on this link :

  1. Remove CodeBehind="yourfile.aspx.cs" from the first line of your .aspx file which will cause the c# event handler code to be placed into your .aspx source file instead.

     <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %> 

    So you are left with:

     <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="HoursWorked._Default" %> 
  2. Now double-click the control in the Designer which should now pop the event handler code inside script tags within your aspx.

  3. Next, restore the CodeBehind="yourfile.aspx.cs" you had removed in the first step.

     <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HoursWorked._Default" %> 
  4. Finally, double-click the control which now pops the event handler code inside yourfile.aspx.cs!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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