[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1020
  • Last Modified:

Hyperlink in gridview

I have a webpart that displays a document library to the page.  It works fine but the document name is not linked to the actual document.  It's just static.  How can I make it linked to the actual document?

Below is the code
<%@ Control Language="C#" AutoEventWireup="true" %>
<%@ Register assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls" tagprefix="SharePoint" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint" tagprefix="SharePoint" %>

<script runat="server">

    protected void Button1_Click(object sender, EventArgs e)
    {       
        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList sourceList;
        SPList destList;     
        
        myWeb = SPContext.Current.Web;
        sourceList = (SPDocumentLibrary)myWeb.Lists["Hotline List"];
        SPListItemCollection results = sourceList.Items;

        destList = (SPDocumentLibrary)myWeb.Lists["HOTLINE"];
            foreach (GridViewRow row in GridView1.Rows)
            {                         
                HiddenField hdID = (HiddenField)row.FindControl("hdID");
                CheckBox complete = (CheckBox)row.FindControl("complete");
                DropDownList ddlStatus = (DropDownList)row.FindControl("status");
                TextBox txtCaseNumber = (TextBox)row.FindControl("caseNumber");
                DropDownList ddlPriority = (DropDownList)row.FindControl("priority");                              

                if (complete.Checked)
                {
                        SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value))); 
                        byte[] fileBytes = item.File.OpenBinary();
                        string destUrl = destList.RootFolder.Url + "/" + item.File.Name;
                        SPFile destFile = destList.RootFolder.Files.Add(destUrl, fileBytes, true /*overwrite*/);

                        // add the metadata to File            
                        SPListItem destItem = destFile.Item;
                        destItem["STATUS"] = ddlStatus.SelectedValue.ToString();
                        destItem["CASE NUMBER"] = txtCaseNumber.Text.ToString();
                        destItem["PRIORITY"] = ddlPriority.SelectedValue.ToString();
                        destItem["COMPLETE"] = complete.Checked.ToString();

                        destItem.Update();
                    
                        item.Delete();   
                    
                        //Re-bind the Grid after delete
                        GridView1.DataBind();                                       
                }
        }            
    }      
</script>

<style type="text/css">
    .style1
    {
        width: 100%;
    }
</style>

<SharePoint:SPDataSource ID="HotLineCase" 
                  runat="server"
                  DataSourceMode="List"                  
                  SelectCommand="<Query><OrderBy><FieldRef Name='Status' /></OrderBy></Query>">
      <selectParameters>
            <asp:Parameter Name="WebID" DefaultValue="RootWeb" />
            <asp:Parameter Name="ListName" DefaultValue="Hotline List" />
      </selectParameters>                  
</SharePoint:SPDataSource>

<table class="style1">
        <tr>
            <td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="HotLineCase" Font-Names="Verdana" Font-Size="X-Small" 
    HorizontalAlign="Center" GridLines="Horizontal">
    <RowStyle HorizontalAlign="Center" />
    <Columns>
        <asp:TemplateField HeaderText="Complete">
        <ItemTemplate>
            <asp:CheckBox runat="server" id="complete" Text="" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Type" DataField="Type" />
        <asp:TemplateField HeaderText="Case Number">
            <ItemTemplate>
                <asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Name" DataField="Document Name" />
        <asp:TemplateField HeaderText="STATUS">
        <ItemTemplate>
            <asp:DropDownList ID="status" runat="server">
                <asp:ListItem Value="...."></asp:ListItem>
                <asp:ListItem Value="Reroute">Reroute</asp:ListItem>
                <asp:ListItem Value="Spam">Spam</asp:ListItem>
            </asp:DropDownList>
        </ItemTemplate>        
        </asp:TemplateField>
        <asp:BoundField HeaderText="Modified" DataField="Modified" />
        <asp:TemplateField HeaderText="Priority">
            <ItemTemplate>
               <asp:DropDownList ID="priority" runat="server">
                <asp:ListItem Value="">.....</asp:ListItem>
                <asp:ListItem Value="Yes">Yes</asp:ListItem>
               <asp:ListItem Value="No">No</asp:ListItem>
            </asp:DropDownList>
            </ItemTemplate>        
        </asp:TemplateField>      
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("ID") %>' Visible="True" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <HeaderStyle HorizontalAlign="Center" BackColor="#CCCCCC" />
    <AlternatingRowStyle BackColor="#EEEEEE" />
</asp:GridView>
            </td>
        </tr>
        <tr>
            <td align="center"><asp:Button ID="Button1" runat="server" Text="Submit" 
                    onclick="Button1_Click" /></td>
        </tr>
        <tr>
            <td align="center">
                <asp:Label ID="msg" runat="server" Text=""></asp:Label></td>
        </tr>
    </table>
    <br />

Open in new window

0
Isaac
Asked:
Isaac
  • 16
  • 7
  • 3
  • +2
2 Solutions
 
radcaesarCommented:
what is that static name?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
The boundfield is "Document Name"

<asp:BoundField HeaderText="Name" DataField="Document Name" />

When it is rendered to the page, it's not linked.
0
 
kumarnimavatCommented:
You may use the asp:HyperLinkField and set the Document Name that you fetch from the database.
0
Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
IsaacSharePoint Client Side DeveloperAuthor Commented:
The datasource is a SharePoint document library.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
kumarnimavat, I tried that like so

<asp:HyperLinkField HeaderText="Document Name" ....>

but it does not have a "DataField" property.
0
 
kumarnimavatCommented:
I guess you are using .net GridView and if you may use Bound Field so do you may use HyperlinkField as well
0
 
kumarnimavatCommented:
You may use the NavigateUrl property and set the document name to it
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
If I use HyperlinkField, I can't bound it to the documnt name field in my sharepoint list.  I've tried and it's not working.
0
 
mr_nadgerCommented:
did you just bind it to the document name, or have you included the full filepath to the doc library?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I binded my document library to a gridview.
0
 
mr_nadgerCommented:
what is the actual value in the docunent name field, is it "\\sharepoint\site\library\mydoc.docx", or just plain "mydoc.docx"?
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
just plain "mydoc.docx"
0
 
mr_nadgerCommented:
try adding the document library filepath in as well, so it's a full URL, the web part probably can't work out the implicit path from the page it's displayed on
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
how do I do that and where would I add it?
0
 
mr_nadgerCommented:
I have this as the hyperlink navigate to URL in a gridview I use to display links to files "\\webbox\CDFRef\4.mht"
I've done that in the table itself, you could either amend it in the sql query, or the eval(document name) part of your gridview
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I'm trying to link the 'document name' column to the actual file.



void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
// Retrieve the current row.
GridViewRow row = e.Row;

if (row.RowType == DataControlRowType.DataRow)
{
//Create Link
e.Row.Cells[3].Text == "http://mySite/Hotline%20List/" + e.Row.Cells[3].ToString() + "</i>";
}
}



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="HotLineCase" Font-Names="Verdana" Font-Size="X-Small"
HorizontalAlign="Center" GridLines="Horizontal" OnRowDataBound="GridView1_RowDataBound">
<RowStyle HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="Complete">
<ItemTemplate>
<asp:CheckBox runat="server" id="complete" Text="" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Type" DataField="Type" />
<asp:TemplateField HeaderText="Case Number">
<ItemTemplate>
<asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Document Name" DataField="Name" />
<asp:TemplateField HeaderText="STATUS">
<ItemTemplate>
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Now I get this error:
error CS0103: The name 'hdID' does not exist in the current context  
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList sourceList;

        myWeb = SPContext.Current.Web;
        sourceList = (SPDocumentLibrary)myWeb.Lists["Hotline List"];
        SPListItemCollection results = sourceList.Items;
        SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value)));
        // Retrieve the current row. 
        //GridViewRow row = e.Row;
        foreach (GridViewRow row in GridView1.Rows)
        {
            HiddenField hdID = (HiddenField)row.FindControl("hdID");
            if (row.RowType == DataControlRowType.DataRow)
            {
                //Create Link
                e.Row.Cells[3].Text == "<a href=http:/Hotline%20List" + item.File.Name + ">" + item.File.Name + "</a><br />" + item.File.Name;
            }
        }
    }

Open in new window

0
 
mr_nadgerCommented:
is it line 10
 SPListItem item = sourceList.GetItemById(Convert.ToInt32((hdID.Value)));

where is this hdID control? You seem to be using a findcontrol on it later, is it in the gridview?


0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
It's a hidden field in the gridview.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
it's a hidden field control in gridview.
0
 
mr_nadgerCommented:
why use a hidden field instead of a datakey?

I'll admit I'm a bit confused by your code, you're using the RowDataBound event, and then iterating through all the rows? Either iterate through the rows after the DataBound event (not RowDataBound), or only change the row which has called the RowDataBound.


Change the column with the document name in it to a template field, containing a linkbutton, e.g. linkClickMe.

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridViewRow row = e.Row;

        if (row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lb = (LinkButton)e.Row.FindControl("linkClickMe");
            lb.Text = "change my text";
            lb.PostBackUrl = "build your path here";        }
    }

I'm not a C# coder, but I think that should sort you
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I had a piece of code similar to yours but then from my readings, it seems that it picks just one row and not all rows.  That's why I tried the loop.  I'll try your suggestion.

Anyway, attached is the image of the webpart that displays a document library.  'Document Name' is what I'm trying to link.
hotlineImg.pptx
hotLineImage.gif
0
 
mr_nadgerCommented:
if you're doing it on the rowDataBound event, it should only affect one row, if you're going to iterate through all the rows, do it on the DataBound event instead
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
I ge the following error:
error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement  
 
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
          GridViewRow row = e.Row;

        if (row.RowType == DataControlRowType.DataRow)
        {
            row.Cells[3].Text == "<a href=http:/Hotline%20List/" + item.File.Name + ">" +   item.File.Name + "</a><br />" + item.File.Name;            
        }

    }
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
This actually,

row.Cells[3].Text = "<a href=http:/Hotline%20List/" + item.File.Name + ">" +   item.File.Name + "</a><br />" + item.File.Name;  
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
error CS0103: The name 'hdID' does not exist in the current context
 
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Now I get this error: The name 'myWeb' does not exist in the current context  
 
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="HotLineCase" Font-Names="Verdana" Font-Size="X-Small"
    HorizontalAlign="Center" GridLines="Horizontal" OnDataBound="GridView1_RowDataBound" DATAKEYNAMES="ID">  
...
...
<asp:TemplateField>
            <ItemTemplate>
                <asp:HiddenField ID="hdID" runat="server" Value='<%# Eval("ID") %>' Visible="True" />
            </ItemTemplate>
        </asp:TemplateField>
...
...

 void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        SPSite mySite = SPContext.Current.Site;
        SPWeb myWeb;
        SPList sourceList;
        SPFile file = myWeb.GetFile("http://landis/Hotline%20List/Forms/AllItems.aspx");
        string listUrl = file.Item.ParentList.DefaultViewUrl;

        myWeb = SPContext.Current.Web;
        sourceList = (SPDocumentLibrary)myWeb.Lists["Hotline List"];
        SPLISTITEM ITEM = SOURCELIST.GETITEMBYID(CONVERT.TOINT32(GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString()));

        GridViewRow row = e.Row;

        if (row.RowType == DataControlRowType.DataRow)
        {
            row.Cells[3].Text = "<a href=" + listUrl + item.File.Name + ">" + item.File.Name + "</a><br />" + item.File.Name;
        }

    }
0
 
Carlos VillegasFull Stack .NET DeveloperCommented:
Hi, I think that this question is already solved here:
http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_27023354.html
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 16
  • 7
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now