How to get the selected color title on the server side

2 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    134 posts
    Member since:
    Sep 2007

    Posted 28 Jan 2011 Link to this post

    Hello,

    I setup a custom color picker in a radgrid column like so:

    <telerik:GridTemplateColumn 
        DataField="Color"
        UniqueName="Color"
        SortExpression="Color"
        HeaderText="Color"
        HeaderStyle-Width="100" 
        ItemStyle-HorizontalAlign="center"                                        
        AllowFiltering="false">
        <ItemTemplate>
            <asp:Label id="lblColor" runat="server" ></asp:Label>
        </ItemTemplate>
        <EditItemTemplate>
            <telerik:RadColorPicker ID="RadColorPicker1" Runat="server"  Skin="WebBlue" Preset="None" >
               <telerik:ColorPickerItem Title="rsCategoryDarkBlue" Value="#202B3F" />
               <telerik:ColorPickerItem Title="rsCategoryBlue" Value="#CCCCFF" />
               <telerik:ColorPickerItem Title="rsCategoryDarkGreen" Value="#2B3F20" />
               <telerik:ColorPickerItem Title="rsCategoryGreen" Value="#D0ECBB" />
               <telerik:ColorPickerItem Title="rsCategoryDarkRed" Value="#3F2020" />
               <telerik:ColorPickerItem Title="rsCategoryOrange" Value="#EDD5B7" />
               <telerik:ColorPickerItem Title="rsCategoryPink" Value="#F1DCFF" />
               <telerik:ColorPickerItem Title="rsCategoryRed" Value="#ECBBBB" />
               <telerik:ColorPickerItem Title="rsCategoryViolet" Value="#540042" />
               <telerik:ColorPickerItem Title="rsCategoryYellow" Value="#FFFAC5" />
            </telerik:RadColorPicker>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

    I'd like to be able to grab the title of the selected color on the server side.
    protected void radGridStages_InsertCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        //Get the GridEditFormInsertItem of the RadGrid     
        GridDataInsertItem insertedItem = (GridDataInsertItem)e.Item;
        //string EmployeeID = (insertedItem["EmployeeID"].Controls[0] as TextBox).Text;   
        try
        {
                //load the lookup
                Stage _stage = new Stage();
                //change the name
                _stage.Name = (insertedItem["Name"].Controls[0] as TextBox).Text;
                _stage.Code = (insertedItem["Code"].Controls[0] as TextBox).Text;
                _stage.Color = (insertedItem["Color"].Controls[1] as RadColorPicker).SelectedColor.ToArgb();
                //_stage.CssStyle = (insertedItem["Color"].Controls[1] as RadColorPicker).SelectedColorTitle;
                _stage.Active = (insertedItem["Active"].Controls[0] as CheckBox).Checked;
                StageService.Save(_stage);
                radGridStages.Focus();
        }
        catch (Exception ex)
        {
            lblMessage.Text = "Unable to insert Stage. Reason: " + ex.Message;
            e.Canceled = true;
        }
    }

    The problem is that there is no attribute server side to grab the title.  I could put in a long-winded select case statement but it seems like it should be easier.

    Thanks
    Jonathan
  2. Tsvetie
    Admin
    Tsvetie avatar
    1517 posts

    Posted 02 Feb 2011 Link to this post

    Hi Jonathan,
    The RadColorPicker control does not have a property that returns the title of the selected color picker item. You can use, however, the following method to get that title:
    private string GetSelectedTitle(RadColorPicker picker)
    {
        var selectedColor = picker.SelectedColor;
        foreach (ColorPickerItem item in picker.Items)
        {
            if (item.Value == selectedColor)
                return item.Title;
        }
     
        return string.Empty;
    }


    Kind regards,
    Tsvetie
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top