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>
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With