RadComboBox / Label in GridTemplate

4 posts, 0 answers
  1. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 05 Jul 2009 Link to this post

    Requirements

    RadControls version

    2009.Q2

    .NET version

    3.5

    Visual Studio version

    2008

    programming language

    C#

    browser support

    all browsers supported by RadControls


    PROJECT DESCRIPTION
    @Telerik: I used the dll from RadControls_for_ASP.NET_AJAX_2009_2_701_trial_hotfix.zip - but I get no trial messages. Could you please check if the DLL is really the trial version.

    Assume a (common) scenario where a column of a GridRow references a different table - the "master table" holds an ID and the related table this ID and some "Name" for it.
    A typical use for a GridDropDownColumn.

    But sometimes I came to a point where I couldn't use a GridDropDownColum - this happened when I had to do "very special customizations".
    At this time I had to use a GridTemplateColumn. For the EditItemTemplate this is no problem - simply use a RadComboBox.
    But how about the ItemTemplate?
    One solution is to use a RadComboBox again - but the User should not be able to change a selection (this could confuse him).
    To set the RadComboBox to Enabled="false" is also not really a solution - the thing is almost impossible to read.
    Another approach is to define a datasource which allows to select the "Name" by giving an ID or to get the master data from some kind of join - which could make problems if you want to update the data.
    Often I use "cached data" for things like "Country, Status, Region, Role...) - so the data is there - and I can access it very fast.
    And last not least - I want to do it "the easy way" - no code behind for every grid which holds such a column.

    The idea - I want to have a kind of Label which acts exactly like a ComboBox - I have a datasource(id), a selected value and so on.
    As a result I get the same as with a ComboBox - but not a "unreadable" disabled thing - I just get a label (some kind of static text).
    In simple words - I get the same result as with a GridDropDownColumn - but with full customization capabilities.

    I built such a control (rough done -- Css class and other attributes are not rendered) and here it is.
    The use is almost identical to a RadComboBox - here an example for a templated column using this control.
    <telerik:GridTemplateColumn DataField="FunctionID" DataType="System.Int32" HeaderText="Function" SortExpression="FunctionID" UniqueName="FunctionID">  
        <ItemTemplate> 
            <cc1:ComboLabel runat="server" DataTextField="FunctionName" DataValueField="FunctionID" DataSourceID="odsTestFunctions" SelectedValue='<%# Eval("FunctionID") %>' /> 
            &nbsp;&nbsp;(<%# (string)((int)Eval("FunctionID")<2 ? "<style='color:red;'>Handle with care</b>" : "Teasing enabled") %>)  
        </ItemTemplate> 
        <EditItemTemplate> 
            <telerik:RadComboBox ID="cbFunctionID" runat="server" DataTextField="FunctionName" DataValueField="FunctionID" DataSourceID="odsTestFunctions" SelectedValue='<%# Bind("FunctionID") %>' /> 
        </EditItemTemplate> 
    </telerik:GridTemplateColumn> 
     
    You can see - both controls are almost identical - I set exactly the same values for
    DataTextField, DataValueField, DataSourceID and SelectedValue

    I use this control very often - and hope it is useful for you also.
    To enhance the thing (more render capabilities) just change the function Render in ComboLabel (last function at the end).

    Kind Regards

    Manfred
  2. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 07 Jul 2009 Link to this post

    Hello ManniAT,

    Since the use of the controls and the approach is somewhat specific, it is not very suitable for a code library project. Nevertheless, I have posted this sample as a forum post, so that other users can benefit from it as well.
    Additionally, I have updated your Telerik points for your involvement!

    Best wishes,
    Yavor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 07 Jul 2009 Link to this post

    Hi Yavor,

    if you think this solution is "to specific" - no problem.
    If the code is here or at the library makes no big difference - if someone has the same need as I - he can get it from here also.

    A little followup for the code.
    The rendering of extra attributes (back-color, CssClass and so on) works like this:
    AFTER THIS LINE ADD THE CODE: Label lblOut = new Label();  
    foreach (string key in Attributes.Keys) {  
        lblOut.Attributes[key] = Attributes[key];  
    }  
    if (ControlStyleCreated) {  
        lblOut.ApplyStyle(ControlStyle);  
    }  
     
    Just add this code in the render function of the control - that's it.

    Regards

    Manfred
  5. Yavor
    Admin
    Yavor avatar
    11 posts

    Posted 08 Jul 2009 Link to this post

    Hello Manfred ,

    Thank you for the additional code.
    I am sure it will be useful for the community.

    Kind regards,
    Yavor
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top