0

I have a VisualWebPart with a AspTemplateField that should bind a field but it doesn't

The code is the following:

<SharePoint:SPGridView
 ID="GridView1" runat="server"
    AutoGenerateColumns="False"
    AllowSorting="True"
    OnSorting="GridView1_Sorting"
    >
    <Columns>
        <asp:BoundField HeaderText="MyType" DataField="MyType" SortExpression="MyType"/>
         <asp:TemplateField  HeaderText="MyType" SortExpression="MyType">
            <ItemTemplate>
                <asp:Image id="icon" runat="server" ImageUrl='/_layouts/15/MyProject/images/icons/<%# Bind("MyType") %>' />
                <asp:Label ID="tstlbl" runat="server" Text='/_layouts/15/MyProject/images/icons/<%# Bind("MyType") %>' />
            </ItemTemplate>

        </asp:TemplateField>

</SharePoint:SPGridView>

Binding is made in c# BehindCode. What I get is: -BoundField prints MyType correctly -asp:Image doesn't print the icon out -asp:Label prints a string without any translation :

/_layouts/15/MyProject/images/icons/<%# Bind("MyType") %>

so Bind inside <%# %> is not evaluated.

Could you tell me why?

2 Answers 2

2

This is the code from my project

<asp:DataList ID="dlPaging_Top" runat="server" RepeatDirection="Horizontal" OnItemCommand="dlPaging_ItemCommand"
                            OnItemDataBound="dlPaging_ItemDataBound">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkbtnPaging" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
                                    CommandName="Paging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:DataList>

so try '<%# Eval("PageText") %>' and let me know if you have any questions.

1
  • Thanks but with Eval or Bind the output is the same
    – Gyonder
    Jun 5, 2015 at 10:35
1

You should try as follow. It will work.

<SharePoint:SPGridView
ID="GridView1" runat="server"
AutoGenerateColumns="False"
AllowSorting="True">
<Columns>       
     <asp:ImageField DataImageUrlFormatString="~/_layouts/15/MyProject/images/icons/{0}" DataImageUrlField="MyType"></asp:ImageField>    
</Columns>

Hope it will solve your issue.

4
  • Unfortunately I need to use asp.net components and not html
    – Gyonder
    Jun 5, 2015 at 10:36
  • if you want to use only asp.net components then you need to use asp:ImageField. See my updated answer Jun 5, 2015 at 10:46
  • Is you issue resolved? Jun 5, 2015 at 11:04
  • Actually, it's not. Because my final intent is that have an image button. Following your advice I tried with ButtonField but it doesn't have DataImageUrlFormatString so I cannot do as you suggest. At the moment I've passed all path into MyType. This way Bind works. Anyway, if you have another piece of advice I'm all ears
    – Gyonder
    Jun 5, 2015 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.