locked
Updating custom compound control from client RRS feed

  • Question

  • User522790094 posted

    I have 2 radio button lists (RBL) that change the look of the control based on the option selected in each RBL.  The issue is that I don't know how to add the events to monitor the selected index of each RBL and then change the display.  I'm working in VB.net, does anyone know of a good example I could study?

    Tuesday, September 10, 2013 5:12 PM

Answers

  • User697462465 posted

    Hi jwbutler,

    I have created a custom control with RadioButtonList, which acrroding to the selected value to change the color.

    Please try to refer to the following code:

    In the custom cs file:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace CustomControl
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:CustomRandioButtonList runat=server></{0}:CustomRandioButtonList>")]
        public class CustomRandioButtonList : CompositeControl
        {
            protected RadioButtonList radioButtonList;
            protected ListItemCollection listItem;
            public ListItemCollection ListItem
            {
                set { listItem = value; }
                get { return listItem; }
            }
            protected override void CreateChildControls()
            {
                radioButtonList = new RadioButtonList();
                radioButtonList.AutoPostBack = true;
                radioButtonList.DataSource = listItem;
                radioButtonList.DataBind();
                radioButtonList.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
                Controls.Add(radioButtonList);
                //base.CreateChildControls();
            }
     
            protected virtual void OnSelectedIndexChanged(object sender, EventArgs e)
            {
     
                RadioButtonList radio = (RadioButtonList)sender;
                radio.ForeColor = System.Drawing.Color.FromName(radio.SelectedValue);
            }
            
        }
    }
    

    In the aspx file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForCustom.aspx.cs" Inherits="CustomControl.TestForCustom" %>
    <%@ Register TagPrefix="apress" Namespace="CustomControl" Assembly="CustomControl" %>
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <apress:CustomRandioButtonList runat="server" ID="CustomRadio1" />
        </div>
        </form>
    </body>
    </html>

    In the aspx.cs file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace CustomControl
    {
        public partial class TestForCustom : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ListItemCollection item = new ListItemCollection(){
                   new ListItem(){Text="Red",Value="Red"},
                    new ListItem(){Text="Blue",Value="Blue"}
                };
                CustomRadio1.ListItem = item;
                CustomRadio1.DataBind();
            }
     
        }
    }
    

    Hope it can help you.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2013 9:28 AM

All replies

  • User697462465 posted

    Hi jwbutler,

    From my understanding, you would like to change the look of the RBL based on selected value.

    If so, please try to refer to the following code:

    In the aspx file:

    <asp:RadioButtonList runat="server" ID="RBL" AutoPostBack="true" OnSelectedIndexChanged="RBL_SelectedIndexChanged">
            <asp:ListItem Text="White" Value="1"></asp:ListItem>
            <asp:ListItem Text="Red" Value="2"></asp:ListItem>
            <asp:ListItem Text="Gray" Value="3"></asp:ListItem>
        </asp:RadioButtonList>
            Selected value:<asp:Label runat="server" ID="LblValue"></asp:Label>
    

    In the aspx.cs file:

    protected void RBL_SelectedIndexChanged(object sender, EventArgs e)
            {
                LblValue.Text = RBL.SelectedItem.Text;
                switch (RBL.SelectedItem.Text)
                {
                    case "Red":
                        RBL.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "White":
                        RBL.ForeColor = System.Drawing.Color.White;
                        break;
                    case "Gray":
                        RBL.ForeColor = System.Drawing.Color.Gray;
                        break;
                }
            }
    

    Best Regards,
    Terry Guo

    Thursday, September 12, 2013 10:06 AM
  • User522790094 posted

    Terry,

    Thanks for the info, and this will work graet in a standard aspx file but I'm looking for a solution that would work in a custom componnd server control.

     

    John

    Thursday, September 12, 2013 11:27 AM
  • User697462465 posted

    Hi jwbutler,

    I have created a custom control with RadioButtonList, which acrroding to the selected value to change the color.

    Please try to refer to the following code:

    In the custom cs file:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace CustomControl
    {
        [DefaultProperty("Text")]
        [ToolboxData("<{0}:CustomRandioButtonList runat=server></{0}:CustomRandioButtonList>")]
        public class CustomRandioButtonList : CompositeControl
        {
            protected RadioButtonList radioButtonList;
            protected ListItemCollection listItem;
            public ListItemCollection ListItem
            {
                set { listItem = value; }
                get { return listItem; }
            }
            protected override void CreateChildControls()
            {
                radioButtonList = new RadioButtonList();
                radioButtonList.AutoPostBack = true;
                radioButtonList.DataSource = listItem;
                radioButtonList.DataBind();
                radioButtonList.SelectedIndexChanged += new EventHandler(OnSelectedIndexChanged);
                Controls.Add(radioButtonList);
                //base.CreateChildControls();
            }
     
            protected virtual void OnSelectedIndexChanged(object sender, EventArgs e)
            {
     
                RadioButtonList radio = (RadioButtonList)sender;
                radio.ForeColor = System.Drawing.Color.FromName(radio.SelectedValue);
            }
            
        }
    }
    

    In the aspx file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestForCustom.aspx.cs" Inherits="CustomControl.TestForCustom" %>
    <%@ Register TagPrefix="apress" Namespace="CustomControl" Assembly="CustomControl" %>
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        <apress:CustomRandioButtonList runat="server" ID="CustomRadio1" />
        </div>
        </form>
    </body>
    </html>

    In the aspx.cs file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
     
    namespace CustomControl
    {
        public partial class TestForCustom : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ListItemCollection item = new ListItemCollection(){
                   new ListItem(){Text="Red",Value="Red"},
                    new ListItem(){Text="Blue",Value="Blue"}
                };
                CustomRadio1.ListItem = item;
                CustomRadio1.DataBind();
            }
     
        }
    }
    

    Hope it can help you.

    Best Regards,
    Terry Guo

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 13, 2013 9:28 AM
  • User522790094 posted

    Thats what I was looking for, thanks so much for the time you spent to help!

     

    John

    Monday, September 16, 2013 11:01 AM