Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codebehind not working

Tags:

asp.net

This is probably the most simple question ever asked here. I am learning asp.net on my own using Visual Studio 2010. But I can't get past the very first example of my tutorial, and have no idea why. I just can't get the Page_Load event to fire up in the codebehind. The very simple example is as follows:

default.aspx:

<@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master"                AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>


<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="MyLabel" runat="server"></asp:Label>
</asp:Content>

default.aspx.vb

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender as Object, by Val e As System.Eventargs)
    MyLabel.Text = "Hello, World!"
End Sub

End Class

When I add <% MyLabel.Text = "Hello, World!" %> to default.aspx, the label is populated. I can't get ANYTHING (response.writes, etc) to work within the Page_Load event in the codebehind.

Sorry to bother with something like this, but I can't move forward until I figure this simple problem out. And I'm stumped.

like image 336
Rob Avatar asked Mar 24 '26 12:03

Rob


1 Answers

It's not working because you didn't tell it to use code behind. The first line in your aspx file should be:

<%@ Page Title="Home Page" Language="vb" MasterPageFile="~/Site.Master"  AutoEventWireup="true" CodeBehind="Default.aspx.vb" Inherits="YourAppName._Default" %>

MSDN ASP.NET Web Page Code Model

Also, like others mentioned, you'll either need AutoEventWireup="true" or add the Handles Me.Load on your Page_Load.

Also, if you are just learning.... ASP.NET MVC is a better way to go over Webforms.

like image 168
MikeSmithDev Avatar answered Mar 26 '26 15:03

MikeSmithDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!