JohnDoe4136
JohnDoe4136

Reputation: 539

Displaying filename in GridView asp.net

I am getting this ~/Data/TestCopy.pdf for my Documents in the GridView but I am trying to get only the filename without the directory from SQL database. Does anyone know how to achieve this?

Thanks!

           <asp:TemplateField HeaderText="Documents " SortExpression="filePath">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("filePath") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>                                
                            <asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Bind("filePath") %>'
                        NavigateUrl='<%# Eval("filePath") %>'>
                    </asp:HyperLink>
                        </ItemTemplate>
          </asp:TemplateField>

Upvotes: 2

Views: 1745

Answers (4)

user2978233
user2978233

Reputation: 55

May be you have stored it this way ~/Data/TestCopy.pdf in the database. You can use split function to split the last '/'

Upvotes: 0

variable
variable

Reputation: 9664

Not sure but you can try something like this:-

Text='<%# Eval("filePath").ToString().Split(Eval("filePath").ToString().LastIndexOf("/"))[1] %>'

Upvotes: 0

Mudassir Hasan
Mudassir Hasan

Reputation: 28751

In Gridview RowDataBound , evaluate filename from filepath.

System.IO.Path.GetFileName(filePath).

Upvotes: 0

ijaz
ijaz

Reputation: 1150


The simplest way is, just add the Reference to System.IO and use Path class to get only the file name, little change in your code,

<asp:TemplateField HeaderText="Documents " SortExpression="filePath">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Path.GetFileName(Bind("filePath")) %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>                                
                        <asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Path.GetFileName(Bind("filePath")) %>'
                    NavigateUrl='<%# Eval("filePath") %>'>
                </asp:HyperLink>
                    </ItemTemplate>
      </asp:TemplateField>

Upvotes: 2

Related Questions