Tom Gullen
Tom Gullen

Reputation: 61775

ASP.net basic help with ASCX user control

Trying to build my own user control:

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

<asp:Panel runat="server" CssClass="tabWrapper">
        <div class="tab tabSelected"><a href="artworkHome.aspx">Home</a></div>
        <div class="tab"><a href="#">Create New</a></div>
        <div class="tab"><a href="#">Help!</a></div>
        <div class="clear"></div>
        <asp:Literal runat="server" ID="lol"></asp:Literal>
</asp:Panel>

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

public partial class TabMenu : System.Web.UI.UserControl
{
    public string TabGroup { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {

        if (this.TabGroup == "Artwork")
        {
            lol.Text = "LOL!";
        }

    }
}

This shows fine when used as such:

<CrystalControls:TabMenu runat="server" TabGroup="Artwork" />

Except that "LOL!" isn't showing. I'm probably using properties incorrectly etc, I hope it's clear what I'm trying to do?

Upvotes: 1

Views: 1365

Answers (3)

Dan Diplo
Dan Diplo

Reputation: 25359

Your control is coded correctly and works fine. I suspect the reason you are not seeing the LOL! text is due to something in your HTML or CSS obscuring it. For instance, you might have display:none on one of your tabs. Try looking at the page source and seeing if the LOL! text is in it - I bet it is.

Upvotes: 1

Erik Dekker
Erik Dekker

Reputation: 2433

I tried your example, but its all working fine, i do get LOL! in the literal. If you debug the code, and set a braekpoint on the check if Tabgroup == "ArtWork", you can check the value of TabGroup, maybe it is not set yet ?

Upvotes: 1

Richard Friend
Richard Friend

Reputation: 16018

Your problem is that Page_Load is not an event that is automatically wired up for user controls, see autoeventwireup, personally i hate the idea of subscribing to my own events anyway, you should override the provided virtual methods where possible..

Try this...

protected override void OnLoad(EventArgs e)
{    
    if (this.TabGroup == "Artwork")          
    {              
         lol.Text = "LOL!";          
    } 
    base.OnLoad(e);
}

or even this if its likely that the user of the control could change the TabGroup during the page lifecyle..

protected override void OnPreRender(EventArgs e)
{    
    if (this.TabGroup == "Artwork")          
    {              
         lol.Text = "LOL!";          
    } 
    base.OnPreRender(e);
}

Upvotes: 0

Related Questions