This is a migrated thread and some comments may be shown as answers.

RadComboBox / Label in GridTemplate

3 Answers 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ManniAT
Top achievements
Rank 2
ManniAT asked on 05 Jul 2009, 07:37 PM

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

3 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 07 Jul 2009, 12:12 PM
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.
0
ManniAT
Top achievements
Rank 2
answered on 07 Jul 2009, 05:59 PM
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
0
Yavor
Telerik team
answered on 08 Jul 2009, 12:00 PM
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.
Tags
Grid
Asked by
ManniAT
Top achievements
Rank 2
Answers by
Yavor
Telerik team
ManniAT
Top achievements
Rank 2
Share this question
or