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 5992099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:28:58+00:00 2026-05-22T23:28:58+00:00

I have built the following custom control that I have built and I set

  • 0

I have built the following custom control that I have built and I set the selected value when a bullet list item is clicked.

My problem is that I need to set the css class (SetSelected()) after postback, but it always picks up the previous entry in the viewstate rather than picking up the new value.

I don’t think I should be using PageLoad for setting the CSS class but am unsure where best to do this.

Can anyone help please?

[DefaultProperty("SelectedValue"),
ToolboxData("<{0}:GlossaryList runat=\"server\" />")]
public class GlossaryList : WebControl
{
    const string MANAGEDMETADATASERVICE = "Managed Metadata Service";
    const string TOYOTA = "Toyota";

    BulletedList _bulletList = new BulletedList();

    public String SelectedText
    {
        get
        {
            object selectedText = ViewState["SelectedText"];
            return (selectedText == null) ? String.Empty : (string)selectedText;
        }
        set
        {
            ViewState["SelectedText"] = value;
        }
    }

    public String SelectedValue
    {
        get
        {
            object selectedValue = ViewState["SelectedValue"];
            return (selectedValue == null) ? String.Empty : (string)selectedValue;
        }
        set
        {
            ViewState["SelectedValue"] = value;
        }
    }

    protected override void OnInit(EventArgs e)
    {
        CreateBulletedList();    

        base.OnInit(e);
    }

    protected override void OnLoad(EventArgs e)
    {
        if (this.Page.IsPostBack)
        {
            SetSelected();
        }

        base.OnLoad(e);
    }

    private void SetSelected()
    {
        //if(this.Controls != null && this.Controls.Count > 0)
        //{
        //    foreach (ListItem listItem in ((BulletedList)this.Controls[0]).Items)
        //    {
        //        if (listItem.Value == SelectedValue)
        //        {
        //            listItem.Attributes.Add("class", "active");
        //        }
        //    }
        //}
    }

    protected void CreateBulletedList()
    {
        _bulletList.Click += new BulletedListEventHandler(BulletListItem_Click);
        _bulletList.DisplayMode = BulletedListDisplayMode.LinkButton;
        _bulletList.CssClass = "letter-selector";

        for (char c = 'A'; c <= 'Z'; c++)
        {
            ListItem listItem = new ListItem();

            listItem.Text = c.ToString();
            listItem.Value = c.ToString();

            if (SelectedValue == c.ToString())
            {
                listItem.Attributes.Add("class", "active");
            }

            _bulletList.Items.Add(listItem);
        }

        if (!this.Page.IsPostBack)
        {
            _bulletList.Items[0].Selected = true;
            _bulletList.Items[0].Attributes.Add("class", "active");
        }

        this.Controls.Add(_bulletList);
    }

    private void BulletListItem_Click(object sender, BulletedListEventArgs e)
    {
        SelectedValue = SelectedText = _bulletList.Items[e.Index].Value;

        SetActive(e.Index);
    }

    private void SetActive(int index)
    {
        if (this.Controls != null && ((BulletedList)this.Controls[0]).Items.Count > 0)
        {
            foreach (ListItem listItem in ((BulletedList)this.Controls[0]).Items)
            {
                listItem.Attributes.Clear();
            }
        }

        ((BulletedList)this.Controls[0]).Items[index].Attributes.Add("class", "active");
    }
}
  • 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-22T23:28:59+00:00Added an answer on May 22, 2026 at 11:28 pm

    Add following code:

    protected override void CreateChildControls()
    {
        Controls.Clear();
        CreateBulletedList();
    }
    
    public override ControlCollection Controls
    {
        get
        {
            EnsureChildControls();
            return base.Controls;
        }
    }
    
    
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        EnsureChildControls();
    }
    

    Change the SetSelected method:

    private void SetSelected()
    {
            foreach (ListItem listItem in _bulletList.Items)
            {
                if (listItem.Value == SelectedValue)
                {
                    listItem.Attributes.Add("class", "active");
                }
            }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom control that inherits from .NET's CompositeControl class. This control overrides
I have the following situation: I built an Access form with a subform (which
I have a web app built against asp.net 2.0, but keep getting the following
I have built an MSI that I would like to deploy, and update frequently.
I have built an application in C# that I would like to be optimized
I have built a .dll under WinXP that claims it can't find DWMAPI.DLL when
I have created a custom data grid control. I dragged it on windows form
I currently have a file abc.htm in my Custom Server Control Project and it's
I am building a CustomControl that must unfortunately contain a poorly built sub-control to
I'm pretty new to powershell. I've built a custom cmdlet. I already have it

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.