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

  • SEARCH
  • Home
  • 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 226123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:28:50+00:00 2026-05-11T19:28:50+00:00

This is something which has always puzzled me. If I want to make a

  • 0

This is something which has always puzzled me.

If I want to make a custom control, in this case a child of the Datagrid control, but I want to ensure that when I hover my mouse over a specific cell, there is a rollover colour. If the datagrid doesn’t have any provision of doing this (For argument’s sake), do I have to get the co-ordinates of the mouse in relation to the cell and then highlight the closest cell?

Thanks

  • 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-11T19:28:50+00:00Added an answer on May 11, 2026 at 7:28 pm

    Rollover color should be defined using style sheets, or JavaScript. This would not be directly controlled through ASP.NET. You should have your server control generate either CSS classes to be used for defining hover styles, or generate inline JavaScript.

    Using CSS is the best option, however it won’t work in IE6 (and maybe IE7, I can’t remember). This example assumes your control generates the data grid as an HTML table. Since your question says “custom server controls”, this may be different if you’re rendering your control with different markup. Make sure your table has a CSS class of MyDataGridClass.

    .MyDataGridClass td:hover {
        background: #999;
    }
    

    If you want to highlight an entire row, you would change the td to tr in the example above.

    If you want a hover style in IE6 and you only need to highlight a single cell, you can do better by using an <a /> tag inside each cell. IE6 implements the hover pseudo-selector incorrectly so it only works on <a /> tags, which is why this will work, but the above won’t in IE6:

    .MyDataGridClass a {
        display: block;
        width: 100%;
        height: 100%;
        padding: 0px;
        margin: 0px;
    }
    
    .MyDataGridClass a:hover {
        background: #999;
    }
    

    This example assumes, of course, that you have the ability to embed CSS into the page your control will be placed on. If for some reason you don’t have this option, you can go the JavaScript route, but this is not the preferred way to do things. If you’re interested in seeing how you’d go about this using JavaScript, just let me know.

    UPDATE:
    The section below addresses the first comment you left. I’ve kept the original in case someone finds it useful.


    In order to create an event driven control, you should implement the IPostBackEventHandler or IPostBackDataHandler. If you need to understand the difference between these 2 interfaces, you can see this question. I’d rather not include the details here because it would make this answer too long.

    This example uses the IPostBackEventHandler, but the IPostBackDataHandler is pretty similar.

    You need to do a few things to handle PostBack events:

    • Create a key to use as an identifier for your event. This is used for the base.Event key/value collection. In this example, I’m using the _EventClick object.
    • Setup/register your event with base.Events[] so that event subscribers are maintained across postbacks.
    • When rendering your control, call Page.ClientScript.GetPostBackEventReference. This will generate a function call to ASP.NET’s __doPostBack JavaScript method. You can use this script in any JavaScript event attributes you render.

    This is a very minimal re-creation of the ASP.NET LinkButton control. Most of the “magic” happens in the AddAttributesToRender() and the RaisePostBackEvent() methods.

    public class ControlWithEvent : WebControl, IPostBackEventHandler
    {
        private object _EventClick = new object();
    
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.A; }
        }
    
        public event EventHandler Click
        {
            add
            {
                base.Events.AddHandler(_EventClick, value);
            }
            remove
            {
                base.Events.RemoveHandler(_EventClick, value);
            }
        }
    
        public void RaisePostBackEvent(string eventArgument)
        {
            // The value of the 'eventArgument' parameter will be "myOptions" 
            EventHandler handler = (EventHandler)base.Events[_EventClick];
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    
        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
    
            // "myOptions" can be anything, including an empty string.
            // You can use it for your EventArgs object if you need it.
            PostBackOptions options = new PostBackOptions(this, "myOptions");
    
            string script = Page.ClientScript.GetPostBackEventReference(options, false);
    
            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, script);
        }
    
        public string Text { get; set; }
    
        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }
    }
    

    If you want to include additional JavaScript logic with your control, I suggest you read this article on including JavaScript with ASP.NET to see how to register scripts with ASP.NET.

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

Sidebar

Related Questions

No related questions found

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.