Link to home
Start Free TrialLog in
Avatar of Jeff Fillegar
Jeff FillegarFlag 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.
Avatar of MikeMCSD
MikeMCSD
Flag of United States of America image

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.
Avatar of 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?
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
Just change the following line:
<asp:DynamicField DataField="MANUAL" />
to this:
<asp:HyperLink runat="server" ID="ManualLink" NavigateUrl='<%# Eval("MANUAL") %>' Target="_blank">
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
Avatar of CtrlAltDl
CtrlAltDl
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
 <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

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?
@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/questions/27751930/asp-net-gridview-dynamic-hyperlink-item-templates.html

Thanks!