Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6347823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:17:54+00:00 2026-05-24T21:17:54+00:00

Is it improper to nest another control within asp:Label? When I have another control

  • 0

Is it improper to nest another control within asp:Label?

When I have another control (e.g., a button) inside my asp:Label, and I try to change CssClass on the label during an event handler, the entire label seems to disappear. I’ll be grateful if someone can help me find what I might be doing wrong.

Bad1.aspx:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Bad1.aspx.cs" Inherits="Bad1" %>
<!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 runat="server">
    <title>Badness?</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:CheckBox ID="CheckBox1" runat="server" Text="check and uncheck me" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" /> 
        <br />
        <br />
        <asp:Label ID="Label1" runat="server" > See me now? <asp:Button runat="server" /> </asp:Label>
    </form>
</body>
</html>

Bad1.aspx.cs:

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Bad1 : System.Web.UI.Page 
{
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        var lab = Page.FindControl("Label1") as Label;
        lab.CssClass = "foo";
    }
}

Symptom: Page displays correctly at first, and after first click on “check and uncheck me”. But when I click on the checkbox a second time, the label (“See me now”) disappears, never to come back. If I remove the button from within the label, there’s no problem. So, is the nesting what’s incorrect?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:17:54+00:00Added an answer on May 24, 2026 at 9:17 pm

    I would look into using a Panel instead of a Label.

    <asp:Panel ID="Panel1" runat="server" CssClass="someStyle">
       <asp:Button ID="Button1" runat="server" Text="Hi"></asp:Button>
    </asp:Panel>
    

    If you’re looking to style the button, then you should apply your CssClass to the button directly. Use a Panel if you’re trying to apply a style to that region/block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service written in C#. It behaves rather strange during pool
I have two edit text control and i want to update the second edit
I have a validation for big file size in a try block, after throwing
I'm interested in calculation of Improper Integral for a function. Particularly it's a Gauss
A there any g++ options which can detect improper initialization of std::string with NULL
Greetings, I'm trying to prevent XSS and improper html from input fields using CKEditor
I have no idea why this is happening; some Google searches have led me
I am hoping to have a multidimensional array of structures, but I can't seem
I have the following struct john { int oldA; int A; } myJohn; DWORD
Preface: I know this is an unusual/improper way to do this. I can do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.