Search code examples
asp.netsessiondatalist

How to Access Session/Variable Value in DataList Bind item in Asp.net?


I have a DataList which displays the Uploaded files (.jpg,jpeg,gif etc) in a Specific folder say "EmpMainFolder" which contains all Scanned copy of employee Documents. I want to show it in DataList. The Following code works fine when i give Static Folder Name.

Following code Works Fine for Static Folder Name in DataList of .aspx page:

<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" CellPadding="5" OnItemCommand="DataList1_ItemCommand">
            <ItemTemplate>
                <asp:Image Width="100" runat="server" ID="Image1" ImageUrl='<%# Bind("Name", "~/Uploads/Employee/{0}")%>' />
                <br />

                <asp:Label ID="Label1" Text='<%# Bind("Name") %>' runat="server" /><br />
                <asp:LinkButton ID="lbldocnm" runat="server" CommandName="ViewDocument" CommandArgument=**'<%# Bind("Name", "~/Uploads/Employee/{0}") %>'**>View</asp:LinkButton>
            </ItemTemplate>
            <ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"
                VerticalAlign="Bottom" />
        </asp:DataList>

I want to Set the Current Employee Folder Name from Session as:

<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" CellPadding="5" OnItemCommand="DataList1_ItemCommand">
        <ItemTemplate>
            <asp:Image Width="100" runat="server" ID="Image1" ImageUrl=**'<%# Bind("Name", "~/Uploads/Employee/%>'+ '<%#Session["EmpMainFolder"] %>'"+"/"+{0}")%>'** />
            <br />
 <asp:Label ID="Label1" Text='<%# Bind("Name") %>' runat="server" /><br />
 <asp:LinkButton ID="lbldocnm" runat="server" CommandName="ViewDocument" CommandArgument='<%# Bind("Name", "~/Uploads/Employee/{0}") %>'>View</asp:LinkButton>
 </ItemTemplate>
 <ItemStyle BorderColor="Silver" BorderStyle="Dotted" BorderWidth="1px" HorizontalAlign="Center"  VerticalAlign="Bottom" />
</asp:DataList>

But, When the Folder Name is set Dynamically through Session it doesn't show any image.

Any Idea? Help appreciated!


Solution

  • try out something like this

    ImageURL='<%# GetImage((string)Eval("Name")) %>'
    

    write down function like this

    public static string GetImage(string name)
    {
    
            return "~/Uploads/Employee/"+ Session["EmpMainFolder"].ToString() + name;
    
    }