accessing databound property from inside normal ASP.NET script block

Posted on 2006-06-07
Last Modified: 2008-02-01
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" />
            <asp:Label ID="FilenameHeader" runat="server" Text="Attachments"></asp:Label>
                <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 />
                <%} %>
            <AlternatingItemStyle BackColor="White" ForeColor="#284775" />
            <ItemStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:SupportTickets %>"
            SelectCommand="SELECT [Filename], [TicketID] FROM [Attachments] WHERE ([TicketID] = @TicketID)">
                <asp:ControlParameter ControlID="GridView1" Name="TicketID" PropertyName="SelectedValue"
                    Type="Int32" />

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.
Question by:RepriseMIS
    LVL 27

    Accepted Solution

    why dont you add both databound controls and show/hide based on your condition
    should be easier this way

    Author Comment

    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?

    Author Comment

    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;


    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.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now