Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page_Load not firing in UserControl

Tags:

c#

asp.net

c#-3.0

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>
like image 798
redoc01 Avatar asked Feb 22 '11 21:02

redoc01


People also ask

What is the purpose of onload event in a usercontrol?

From MSDN: The OnLoad method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class. Show activity on this post. There wouldn't be any special properties you need to set for a UserControl's events to fire.

How to set event properties for a usercontrol?

There wouldn't be any special properties you need to set for a UserControl's events to fire. You have one of 2 ways to subscribe to the event. In the Properties (property grid) select the events list...double-click at the Load property.

How to handle a usercontrol event in derived class?

The OnLoad method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class. Show activity on this post. There wouldn't be any special properties you need to set for a UserControl's events to fire. You have one of 2 ways to subscribe to the event.

How to handle event in usercontrol without delegate?

Try overriding the OnLoad () method in your UserControl. From MSDN: The OnLoad method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class. Show activity on this post.


2 Answers

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.

like image 79
Xavier Poinas Avatar answered Oct 04 '22 07:10

Xavier Poinas


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.

like image 25
Clint Avatar answered Oct 04 '22 08:10

Clint