• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 328
  • Last Modified:

accessing databound property from inside normal ASP.NET script block

im trying to do the following:

<asp:DataList ID="DataList1" runat="server" CellPadding="4" DataSourceID="SqlDataSource3"
            ForeColor="#333333" Width="100%">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderTemplate>
            <asp:Label ID="FilenameHeader" runat="server" Text="Attachments"></asp:Label>
            </HeaderTemplate>
            <ItemTemplate>
                Filename:&nbsp;
                <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("Filename", "{0}") %>' NavigateUrl='<%# "~/images/" + Eval("TicketID", "{0}") + "/"+ Eval("Filename", "{0}") %>' Target="_blank"></asp:HyperLink><br />
                <% string temp = <filename string>
                   
                   if (temp.Substring(temp.Length-3,3).ToLower().Contains("jpg"))
                   {
                   
                    %>
                <asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/images/" + Eval("TicketID", "{0}")+ "/"+Eval("Filename", "{0}") %>' Height="50%" Width="50%" /><br />
                <%} %>
               
            </ItemTemplate>
            <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
            <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        </asp:DataList>
       
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:SupportTickets %>"
            SelectCommand="SELECT [Filename], [TicketID] FROM [Attachments] WHERE ([TicketID] = @TicketID)">
            <SelectParameters>
                <asp:ControlParameter ControlID="GridView1" Name="TicketID" PropertyName="SelectedValue"
                    Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>



i need to set 'temp' to equal the value of 'Filename'. how do i do that? basically i only want to render the image control if the filename is an image.
0
RepriseMIS
Asked:
RepriseMIS
  • 2
1 Solution
 
SammyCommented:
why dont you add both databound controls and show/hide based on your condition
should be easier this way
0
 
RepriseMISAuthor Commented:
that's another way to do it, but it doesnt help me with the condition part. i still dont know how to determine if the filename contains a certain extension. can anyone show me how to do that?
0
 
RepriseMISAuthor Commented:
ok, i figured it out:

added this to asp:Image -
Visible='<%# FileIsImage(DataBinder.Eval(Container.DataItem, "Filename")) %>'

and this to top of file

<script language="C#" runat="server">
    bool FileIsImage(object filename)
    {
        string file = (string)filename;
       
        if (file.Substring(file.Length - 3, 3).ToLower().Contains("jpg"))
        {
            return true;
        }

        return false;
    }

</script>


now it sets the visibility of the of the image control depending on if the filename has that extension. thanks for your suggestion sammy. i'll give you the points.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now