Avatar of Jeff Fillegar
Jeff Fillegar
Flag for United States of America asked on

asp.net - gridview - click link displays new browser

Hello Experts,

Using: c#, asp.net, Oracle, ODP.net, .net 4.0.  Intranet based application.

I have a gridview that has 3 columns.  The third column contains a file path.  For ex:

ID  Product      Manual
1    VIZIO LCD  \\server\products\LCD\vizio_lcd_manual.htm

I need the Manual column content to be clickable.  So, when the user clicks on the file path a new browser is opened with that file.  All users will have access.  

What is the best approach to enable that column markup? Gridview markup or something like jquery?  

Thanks in advance.
C#ASP.NET

Avatar of undefined
Last Comment
Jeff Fillegar

8/22/2022 - Mon
MikeMCSD

Put a "lblLink1" Label in your GridView, then this in code behind :

 protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
  {

        Label link1 = (Label)e.Row.FindControl("lblLink1");

          link1.Text = "<a href='" + (DataBinder.Eval(e.Row.DataItem, "Link1")).ToString() + "' target='_blank'>" + (DataBinder.Eval(e.Row.DataItem, "Link1")).ToString() + "</a>";
}


Note :  (e.Row.DataItem, "Link1")   Link1 would be the name of your field that has the URL.
Jeff Fillegar

ASKER
I am using asp.net dynamic data and have modified the List.aspx page template:

            <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="true"
                AllowPaging="True" AllowSorting="True" CssClass="DDGridView"
                RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:DynamicField DataField="ID" />
                    <asp:DynamicField DataField="PRODUCT" />
                    <asp:DynamicField DataField="MANUAL" />
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:DynamicHyperLink runat="server" Action="Edit" Text="Edit"
                            />&nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                OnClientClick='return confirm("Are you sure you want to delete this item?");'
                            />&nbsp;<asp:DynamicHyperLink runat="server" Text="Details" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <PagerStyle CssClass="DDFooter"/>        
                <PagerTemplate>
                    <asp:GridViewPager runat="server" />
                </PagerTemplate>
                <EmptyDataTemplate>
                    There are currently no items in this table.
                </EmptyDataTemplate>
            </asp:GridView>
          <asp:EntityDataSource ID="GridDataSource" runat="server" EnableDelete="true" />

Open in new window


I am new to dynamic data templates and the asp:DynamicField.  Currently, the entity template for the MANUAL column is a text field.  How would I modify the markup to implement your suggestion?
MikeMCSD

You can try this, . . I never used Dynamic before, so I'm not sure :

<asp:TemplateField>
<ItemTemplate>

<asp:Label runat="server" ID="lblLink1"></asp:Label>

</ItemTemplate>
</asp:TemplateField>

Can try  <asp:DynamicLabel ??  , not sure
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
CtrlAltDl

Just change the following line:
<asp:DynamicField DataField="MANUAL" />
to this:
<asp:HyperLink runat="server" ID="ManualLink" NavigateUrl='<%# Eval("MANUAL") %>' Target="_blank">
Jeff Fillegar

ASKER
Added this:
                    <asp:TemplateField>
                    <ItemTemplate>
                    <asp:HyperLink runat="server" ID="MANUAL" NavigateUrl='<%# Eval("MANUAL") %>' Target="_blank" />
                    </ItemTemplate>
                    </asp:TemplateField>

Open in new window


However, nothing is showing up in the column.
ASKER CERTIFIED SOLUTION
CtrlAltDl

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jeff Fillegar

ASKER
I new page is displayed when clicked for the first row only.  Reuslt however is, a 400 error.

All other rows are not clickable.

Any ideas?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CtrlAltDl

As far as the 400 error, I think there is a problem with the MANUAL link.  I believe IE is the only browser that handles UNC paths (\\server\).  Try changing the link to something like this: "http://server/directory/file.doc"

Another way to do is map a drive on the server to where your Manuals are and make that a Virtual Directory in IIS.  Then update your links to use the files from the virtual directory.

The example I gave should have a link for everything in the third column.  Can you show me the code to your GridView now?
Jeff Fillegar

ASKER
 <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" EnablePersistedSelection="true"
                AllowPaging="True" AllowSorting="True" CssClass="DDGridView"
                RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6"
                AutoGenerateColumns="false">
                <Columns>
                    <asp:DynamicField DataField="SCENARIO" />
                    <asp:DynamicField DataField="SERVICE" />
                    <asp:DynamicField DataField="NAME" />
                    <asp:TemplateField>
                    <ItemTemplate>
                    <asp:HyperLink runat="server" ID="MANUAL" NavigateUrl='<%# Eval("MANUAL") %>' Target="_blank" >MANUAL</asp:HyperLink> 
                    </ItemTemplate>
                    </asp:TemplateField>                    
                    <asp:TemplateField>
                        <ItemTemplate>                        
                            <asp:DynamicHyperLink runat="server" Action="Edit" Text="Edit"
                            />&nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                                OnClientClick='return confirm("Are you sure you want to delete this item?");'
                            />&nbsp;<asp:DynamicHyperLink runat="server" Text="Details" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>

                <PagerStyle CssClass="DDFooter"/>        
                <PagerTemplate>
                    <asp:GridViewPager runat="server" />
                </PagerTemplate>
                <EmptyDataTemplate>
                    There are currently no items in this table.
                </EmptyDataTemplate>
            </asp:GridView>

            <asp:EntityDataSource ID="GridDataSource" runat="server" EnableDelete="true" />
            

Open in new window

Jeff Fillegar

ASKER
Found the issue with the implementation...

If the MANUAL column does not have any data the column still displays the work MANUAL as a hyperlink.  Any idea on how to add a bit logic to remove the hyperlink if no data?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Jeff Fillegar

ASKER
@CtrlAltDl - I awarded you the points since I believe you answered the orginial question.  I asked the additional question in a new post:



https://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27751930.html

Thanks!