0

I have a Hello World MVC3 Project. I added codebehind, when I double-click on button in design time, it created the handler. It compiled and run. But when clicking on button at run-time handler is not called when setting debug point in handler. Why ?

<%@ Page Language="C#" AutoEventWireup="True"  Inherits="Index"  CodeBehind="~/Views/Home/Index.aspx.cs"%>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Index</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

code behind

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


public partial class Index : System.Web.Mvc.ViewPage<dynamic>
    {


        private string s = "";

        protected void Page_Load(object sender, EventArgs e)
        {

        }


        protected void Button1_Click(object sender, EventArgs e)
        {
            // handler not called
            s = this.TextBox1.Text;
        }






}

Designer generated by Visual Studio

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated. 
// </auto-generated>
//------------------------------------------------------------------------------



public partial class Index {

    /// <summary>
    /// form1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.HtmlControls.HtmlForm form1;

    /// <summary>
    /// Panel1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Panel Panel1;

    /// <summary>
    /// TextBox1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.TextBox TextBox1;

    /// <summary>
    /// Button1 control.
    /// </summary>
    /// <remarks>
    /// Auto-generated field.
    /// To modify move field declaration from designer file to code-behind file.
    /// </remarks>
    protected global::System.Web.UI.WebControls.Button Button1;
}
5
  • try inherit Index from Page instead of System.Web.Mvc.ViewPage<dynamic> Commented Dec 1, 2013 at 15:33
  • 4
    @user310291 - In MVC you can't use server controls, and there's no postback. The comment above makes sense as this will turn the page to WebForm instead of incorrect mixture. You can mix mvc and webforms in same project, but not in same page. Commented Dec 1, 2013 at 16:50
  • That is not MVC. That is web forms. Commented Dec 1, 2013 at 17:06
  • Grundy but then I can't have MVC at the same time ? @afzalulh you're right thanks. MikeSmithDev I want to mix MVC with something else so yes that's not pure MS MVC. Commented Dec 1, 2013 at 18:03
  • 2
    @user310291 - Yes, you can have a hybrid project. But I wouldn't recommend that. That is hard to maintain , very ugly also, and only makes sense when you are upgrading existing WebForms application to MVC. You have to be careful that you can't mix MVC and Webform in same page. Commented Dec 1, 2013 at 18:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.