redoc01
redoc01

Reputation: 2297

Page_Load not firing in UserControl

I have created a class in c# inside a class library and i have added this control to the default.aspx, but my code doesnt fire the page_load event. Here is the code:

What am i doign wrong?

The page loads but doesn't show the label on the page. I have added the control to the page correctly without any errors. I have event added the register tag in there.

html tag: <RandoIntegerControls:RandomIntegerControl ID="RandomIntegerControl1" runat="server"></RandoIntegerControls:RandomIntegerControl>

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

namespace CSIMedia.WebControls
{

  public class RandomIntegerControl : PlaceHolder
  {

    private Label _Label;


    public RandomIntegerControl()
    {

    }

    private void Page_Load(Object sender, EventArgs e)
    {

        this._Label = new Label();
        this.Controls.Add(this._Label);
        this._Label.Text = "Enter random values:";

    }

  }
}

Default.aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="RandoIntegerControls" Assembly="CSIMedia.WebControls" Namespace="CSIMedia.WebControls"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <RandoIntegerControls:RandomIntegerControl ID="RandomIntegerControl1" runat="server"></RandoIntegerControls:RandomIntegerControl>
    </div>
    </form>
</body>
</html>

Upvotes: 5

Views: 21268

Answers (3)

Andomar
Andomar

Reputation: 238048

It turns out that you can override Page_Load in the aspx markup file. That file inherits from the base file.

<%@ Control Language="C#" AutoEventWireup="true" 
    CodeBehind="TestUserControl.ascx.cs" Inherits="TestUserControl" %>

Example to override:

<script language="C#" runat="server">
    public void Page_Load(object sender, EventArgs e)
    {
        ...
    }
</script>

This function overrides the code-behind version.

Upvotes: 0

Xavier Poinas
Xavier Poinas

Reputation: 19733

Try this:

public RandomIntegerControl()
{
    this.Load += Page_Load;
}

In pages (and User Controls), the Page_Load method is normally attached automatically thanks to this attribute:

<%@ Page ... AutoEventWireup="true" ... %>

If you don't have an aspx/ascx file, it is not going to happen automatically, but you can still hook it up manually with the above code.

Upvotes: 9

Clint
Clint

Reputation: 36

You are inheriting from PlaceHolder, which is a Web Control and will not handle the Page_Load event. Only User Controls (.ascx) will be able to handle that event implicitly.

By the looks of the code you posted you would probably be better off using a UserControl, or if you need it re-useable across multiple projects, create a Web Control from scratch.

Upvotes: 2

Related Questions