locked
Url Link RRS feed

  • Question

  • User-1499457942 posted

    Hi 

      I have below code and i want that values should appear as link in Gridview.  Currently it is appearing as Label.

    using (SqlDataReader rdr = cmd.ExecuteReader())
                        {
                            if (rdr.HasRows)
                            {
                                DataTable dt = new DataTable();
                                dt.Columns.Add("FileName", typeof(System.String));
                                while (rdr.Read())
                                {
                                    dt.Rows.Add(rdr["Url1"].ToString());
                                    dt.Rows.Add(rdr["Url2"].ToString());
    
                                }
                                this.gvw.DataSource = dt;
                                this.gvw.DataBind();
                            }
                        }
    
    <asp:GridView ID="gvw"  runat="server" 
                                    CssClass="table table-bordered table-striped" AutoGenerateColumns="false"  
                                    
                                    EmptyDataText="No Records Found!" EmptyDataRowStyle-ForeColor="Red" EmptyDataRowStyle-CssClass ="gvEmpty" >
                                    <HeaderStyle ForeColor="White" Font-Bold="True" BackColor="#428bca"></HeaderStyle>
                                    <Columns>
                                        <asp:TemplateField HeaderText="FileName">
                                            <ItemTemplate>
                                                <asp:Label runat="server" ID="lblFileName" Text='<%#Eval("FileName") %>'></asp:Label>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                    </asp:GridView>

    Thanks

    Thursday, December 20, 2018 10:32 AM

Answers

  • User-2054057000 posted

    Change the Label inside ItemTemplate to anchor like:

    <Columns>
      <asp:TemplateField HeaderText="FileName">
        <ItemTemplate>
          <a href='<%#Eval("Link") %>'><%#Eval("FileName") %></a>
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 20, 2018 12:17 PM

All replies

  • User-2054057000 posted

    Change the Label inside ItemTemplate to anchor like:

    <Columns>
      <asp:TemplateField HeaderText="FileName">
        <ItemTemplate>
          <a href='<%#Eval("Link") %>'><%#Eval("FileName") %></a>
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 20, 2018 12:17 PM
  • User-943250815 posted

    For Gridview you can use HyperLinkField

     <asp:HyperLinkField NavigateUrl='<%# Eval("Filename") %> Text="FileLink" />

    Thursday, December 20, 2018 12:29 PM