asp.net - gridview - click link displays new browser

j420exe1
j420exe1 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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.

Author

Commented:
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?

Commented:
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
Announcing the Winners!

The results are in for the 15th Annual Expert Awards! Congratulations to the winners, and thank you to everyone who participated in the nominations. We are so grateful for the valuable contributions experts make on a daily basis. Click to read more about this year’s recipients!

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

Author

Commented:
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.
Oops, try this:

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

Author

Commented:
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?
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?

Author

Commented:
 <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

Author

Commented:
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?

Author

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



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

Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial