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

Custom control in template column

4 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 13 Sep 2011, 09:00 AM
Hello,
I create a custom control which inherits CompositeControl and put it in template column. Controls contains textbox and button. Here is how I render it:
writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "width:100%"false);
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding: 0;margin: 0;width:95%"false);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            _textbox.RenderControl(writer);
            writer.RenderEndTag();
            writer.AddAttribute(HtmlTextWriterAttribute.Style, "padding: 0;margin: 0;width: 24px"false);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            _clearButton.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();

And it looks like this: http://ybex.com/d/hftqcnpzn4hf6rel5tzore55a8gw6tulq10aeh67.html
And this also happens when I put your control, i.e. DateTimePicker in template column. When I create column, I set its width manually, so I suppose controls in template should "know" its parent width and fit accordingly.
How can I fit controls into cell? Also I would like to stretch column when filter control doesn't fit its width.
I'm not sure those 2 questions are not mutually exclusive but they're both issues I want to solve.
Thank you.

4 Answers, 1 is accepted

Sort by
0
Dmitry
Top achievements
Rank 1
answered on 15 Sep 2011, 09:45 AM
Hello? 
0
Pavlina
Telerik team
answered on 16 Sep 2011, 02:00 PM
Hi Dmitry,

To achieve your goal you can handle on ItemDataBound/ItemCreated event.Then use the UniqueName property of the column and FindControl method to access the custom control in the ItemTemplate and set the desired width. Check the following demo for more information.
Accessing Cells and Rows

Best wishes,
Pavlina
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
0
Dmitry
Top achievements
Rank 1
answered on 16 Sep 2011, 02:18 PM
Thank you for reply but I'm not sure this would be a good solution. Grid may have many columns (20+), and all of them are of TemplateColumn type. Those columns might have many controls, mostly custom. I just cannot access them by Controls[0] or FindControl() because in this case I would have to make a huge green and brunching if/else tree which I must support and feed it with all controls I'm going to develop in future. I'm not sure this is the trick all developers want to do with ready-to-use controls. Is there another workaround?
Thank you.
0
Pavlina
Telerik team
answered on 21 Sep 2011, 08:21 PM
Hello Dmitry,

Please note that for template columns you must find the control in the grid Cells collection and extract its value. Basically, you can reference the grid item which wraps the controls and then get reference to control using the FindControl(controlId) method. More information is available here:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html
http://www.telerik.com/help/aspnet-ajax/grid-reference-controls-in-edit-form.html

Regards,
Pavlina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Dmitry
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or