Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HI C# coders,
I am having one question, how to override the property of the controls placed in .aspx page by inheriting the built in controls in my .cs page.

Suppose if I consider the Label control Text property,
If I drag and drop to any of the .aspx page when I run that should show the text "This is label" with user control and custom control.

Please help me on this, this will be very much helpful.

I tried like this to achive.

What I have tried:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="BatchImportExportApp.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form12" runat="server">
    <div>
    <asp:Label ID="labelBaseClass" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>




Quote:
ing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BatchImportExportApp
{
public partial class Default : System.Web.UI.Page
{
protected virtual void Page_Load(object sender, EventArgs e) { }
}

class labelBaseClass: Label
{
public override string Text
{
get
{
return "This is custom label.";
}

set
{
this.Text = "This is custom label.";
}
}
}
}
Posted
Updated 2-Jun-17 3:01am
Comments
ZurdoDev 2-Jun-17 8:14am    
You want to set the default text for a custom user control?
Member 13158653 2-Jun-17 8:24am    
Yes...when the label if I drag and drop to aspx page text should be "This is custom label.", don't use user control,custom control....I want to achieve it through overriding of the Label class in parent class....Any idea guys..
ZurdoDev 2-Jun-17 8:35am    
So, you don't want to use a custom user control? Why not? I'm not sure if you can override the built-in controls like that.
Member 13158653 2-Jun-17 8:38am    
Ok..
F-ES Sitecore 2-Jun-17 8:58am    
Why don't you just set the text in Page_Load? For what you have now defining the class isn't enough, you'd have to do something like

this.labelBaseClass = new labelBaseClass(); //this.labelBaseClass is defined in the designer file

1 solution

Sounds like you're looking for tag mapping:
Tag mapping in ASP.NET - Mads Kristensen[^]
Tag Mapping in ASP.NET - CodeProject[^]

That will let you automatically replace every instance of <asp:Label> with your own custom label control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900