Ticket Q428129
Visible to All Users

ASPxGridView - How to implement conditional DataItemTemplate

created 13 years ago

Hi,

I'm working on a grid for our website and I'm trying to set up the DataItemTemplate for one particular column that can be hyperlinked depending on a value in another column.

<dx:GridViewDataTextColumn Caption="Loc" FieldName="PATH_NAME">
</dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="Loc Ref" FieldName="ITEM_ID">
<DataItemTemplate>
## if (<% Eval("PATH_NAME") %> == 'CYCLOPS") {
'<a href="javascript:View(' + <% Eval("ITEM_ID") %> +');"><% Eval("Item_ID") %></a>'
} else {
<% Eval("ITEM_ID") %>
} ##
</DataItemTemplate>
</dx:GridViewDataTextColumn>So, of course this doesn't work, but the intent is to evaluate the value in the "PATH_NAME" column and if the text is equal to "CYCLOPS", the data template should make the cell in the Loc Ref column for that row into a hyperlink. If the PATH_NAME is anything else, then simply output the item_id

Like I said, it doesn't work as expected, I get the whole bundle (if statements, braces and all) in the cell.

Is there a way to specify that conditional rendering in the ASPx code? If not, can you give an example of how to specify it in the code behind (the documentation on that is a little jumbled)?
I tried adjusting the code to:

<dx:GridViewDataTextColumn Caption="Loc" FieldName="PATH_NAME"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn Caption="Loc Ref" FieldName="ITEM_ID"> <DataItemTemplate> <% if Eval("PATH_NAME") = "CYCLOPS" Then Response.Write("<a href=""javascript:View('") Eval("ITEM_ID") Response.Write("');"">") Eval("ITEM_ID") Response.Write("</a>") Else Eval("ITEM_ID") End If %> </DataItemTemplate> </dx:GridViewDataTextColumn>
But it didn't like that either. Eval needs to be part of a bound data element.

Comments (1)

    Hello,
    Thank you for contacting us. We need some additional time to review this scenario to provide you with a precise answer.
    We appreciate your patience while we are working on finding a solution to your inquiry.

    Answers

    created 10 years ago

    This I love being a devexpress customer - the sky is the limit. Developing user friendly application with devexpress is amazing and I'm still amazed regularly. I implemented the GetRender solution together with this demo sample https://demos.devexpress.com/ASPxGridViewDemos/Templates/Template.aspx and those 2 combined really deliver:
    when a product photo is in the database, a link is displayed to show it in popup on demand, and when no photo is present, user won't get bothered with a link to nothing.
    Printscreen attached (y)

    Thank you very much Mike!

      Comments (1)

        Hello Andy,

        Thank you for sharing your results and your kind words! We are always happy to be of service.

        created 13 years ago (modified 4 years ago)

        Hello Mike,

        I've solved the task with two approaches:

        ASPx
        <dx:ASPxGridView ID="ASPxGridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" KeyFieldName="CustomerID"> <Columns> <dx:GridViewDataTextColumn FieldName="CustomerID" ReadOnly="True" VisibleIndex="0"> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="ContactName" VisibleIndex="1"> <DataItemTemplate> <%# GetRender(Eval("Country")) %> </DataItemTemplate> </dx:GridViewDataTextColumn> <dx:GridViewDataTextColumn FieldName="Country" VisibleIndex="2"> <DataItemTemplate> <%# Eval("ContactName").ToString().Contains("a") ? "<a href="javascript:View('')">" + Eval("ContactName") + "</a>" : "something else" %> </DataItemTemplate> </dx:GridViewDataTextColumn> </Columns> </dx:ASPxGridView>

        The "ContactName" column DataItemTemplate is:
             <%# GetRender(Eval("Country")) %>
        The code here calls the server-side method that returns the rendering.
        The server-side method is:

        C#
        public string GetRender(object data){ string str = data.ToString(); if(str.Contains("a")) return string.Format("<a href="javascript:View('')">{0}</a>", str); return "something else"; }

        The "Country" column DataItemTemplate is:

        ASPx
        <%# Eval("ContactName").ToString().Contains("a") ? "<a href="javascript:View('')">" + Eval("ContactName") + "</a>" : "something else" %>

        The condition operator ?: (http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx) returns one of the strings.

        Please note, all the expressions are data-binding ones (http://msdn.microsoft.com/en-us/library/bda9bbfx(v=vs.71).aspx)

        Please let us know whether either of the options meets your task requirements.

          Comments (2)

            Great stuff, Serge! Thanks for the help. I went with the 'GetRender' solution as it makes for a nice clean markup.
            Cheers!

              Hello,
              Thank you for informing us that the problem has been resolved. We are glad that our assistance was helpful.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.