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

[Solved] Hide or show fields in a NestedViewTemplate conditionally

2 Answers 287 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gerrit du Preez
Top achievements
Rank 1
Gerrit du Preez asked on 20 Oct 2009, 09:16 PM
Hi there

I want to use a NestedViewTemplate in a RadGrid and then selectively display information depending on the status of a specific field.

I am working on a project where I want to display data, but depending on the type of entry (specified in a specific field) I need to show or hide some fields, or preferably templates in different panels.

e.g.

Type: Car
Engine: 2000cc
Color: Metalic Blue

Type: Bicycle
Color: Blue

Type: Motorcycle
Engine: 1000cc
Color: Red

In the second case (Type: Bicycle), the "Engine" field is not applicable, so I want to be able to hide it.

Any suggestions as to how this can be done?

One possibility is to hide the table row, another is to have different panels with a unique template in each and then show or hide the panels depending on the the value in the "Type" field.  I would prefer this kind of approach, if possible.

How can this be achieved?

Thanks.

Gerrit

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 21 Oct 2009, 07:55 AM
Hello Gerrit,

You can make the table nested in the NestedViewTemplate to render as a server control by setting its 'runat' attribute to 'server' and also specify the ids for various table rows to differentiate between each of the rows. Check out he code to understand better:
aspx:
<nestedviewtemplate> 
    <asp:Panel ID="Panel1" runat="server"
        <table runat="server" id="Table1"
            <tbody> 
             <tr id="Tr1"
                    <td> 
                        Contact Title: 
                    </td> 
                    <td> 
                        <asp:Label ID="Label1" Text='<%#Bind("ContactTitle") %>' runat="server"></asp:Label> 
                    </td> 
                </tr> 
                <tr id="Tr2"
                    <td> 
                        Company Name:
                    </td> 
                    <td> 
                        <asp:Label ID="Label2" Text='<%#Bind("CompanyName") %>' runat="server"></asp:Label> 
                    </td> 
                </tr> 
                  <tr id="Tr3"
                    <td> 
                        Contact Name:
                    </td> 
                    <td> 
                        <asp:Label ID="Label3" Text='<%#Bind("ContactName") %>' runat="server"></asp:Label> 
                    </td> 
                </tr>               
            </tbody> 
        </table> 
    </asp:Panel> 
</nestedviewtemplate> 
 
<columns>    
    <telerik:GridBoundColumn DataField="ContactTitle" UniqueName="ContactTitle" HeaderText="Contact Title"
    </telerik:GridBoundColumn> 
     .... 

c#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridNestedViewItem) 
        { 
            GridNestedViewItem nestedItem = (GridNestedViewItem)e.Item; 
            if (nestedItem.ParentItem["ContactTitle"].Text == "Sales Representative"
            { 
                ((HtmlTableRow)nestedItem.FindControl("Tr2")).Visible = false
            } 
        } 
    } 

Thanks
Princy.
0
Gerrit du Preez
Top achievements
Rank 1
answered on 21 Oct 2009, 09:06 AM
Hi Princy

Thanks for the speedy reply and code sample!

It works nicely.

Greetings

Gerrit
Tags
Grid
Asked by
Gerrit du Preez
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Gerrit du Preez
Top achievements
Rank 1
Share this question
or