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

[Solved] Dynamic Creation of CommandItemTemplate Based on DataItem Values

1 Answer 170 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Billy
Top achievements
Rank 1
Billy asked on 03 Apr 2009, 07:37 PM
Hi,

I am trying to create a dynamic CommandItemTemplate based on the selected item in the Grid.  I have the ClientSettings.EnablePostBackOnRowClick set to true and Selecting-AllowRowSelect also set to true.  I have tried to handle this in two different ways:

First, I tried to handle the ItemCommand event.  I was able to get a reference to the GridDataItem that was selected and read the values, but setting the MasterTableView.CommandItemTemplate to my custom ITemplate object did not have any visual effect on the CommandItem.  I believe this is because the commandItem has already been rendered at this point and doesn't get re-rendered.

My next attempt was to handle the ItemCreated event.  I was now able to catch the creation of the CommandItem and get a reference to it, but now the Grid.SelectedItems collections was empty.  Perhaps this hasn't been set yet at this point in the control lifecycle.

I'm open to other ways of doing this.  Maybe by creating all of the controls that I want declaratively and just enabling or disabling them dynamically, but if the control has already been rendered or if I can't get at the SelectedItems collection, this still won't do me any good.

Any suggestions?  Also, what are my options for doing this using client-side javascript instead?

Thanks,

Billy

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 07 Apr 2009, 11:00 AM
Hello Billy,

To achieve your goal, you need to persist the Selectedindexes collection through PostBacks. As you mentioned earlier, when ItemCreated event is fired, the collection which consists the selected items is still not populated. The other problem is that you should not add controls to the grid after ItemCreated event is raised.

Here is my suggestion how to implement the required functionality:
1. Persist the Selectedindexes collection in the ViewState:
    public List<String> CustomSelectedIndexes 
    { 
        get 
        { 
            return (List<String>)ViewState["CustomSelectedIndexes"] ?? new List<String>(); 
        } 
        set  
        { 
            ViewState["CustomSelectedIndexes"] = value; 
        } 
    } 
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "RowClick")  
        { 
            List<string> list = new List<string>(); 
            for (int i = 0; i < RadGrid1.SelectedIndexes.Count; i++) 
            { 
                list.Add(RadGrid1.SelectedIndexes[i].ToString()); 
            } 
            CustomSelectedIndexes = list; 
            
            RadGrid1.Rebind(); 
        } 
    } 

In the RadGrid's PreRender event you need to repopulate the SelectedIndexes colleciton:
    protected void RadGrid1_PreRender1(object sender, EventArgs e) 
    { 
        foreach (var value in CustomSelectedIndexes) 
        { 
            RadGrid1.Items[value].Selected = true
        } 
    } 

Now you can use the CustomSelectedIndexes collection as you wish. My implementation is like this:
            <CommandItemTemplate> 
                <asp:Button ID="btnTest" runat="server" Text="Test" CommandName="Test" Visible='<%# CustomSelectedIndexes.Count == 0 %>' /> 
            </CommandItemTemplate> 

For your convenience I attached the test project which I created to implement the required functionality.

Kind regards,
Georgi Krustev
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Billy
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or