1

I have created a column which is of type Hyperlink or Picture.

I want to display the list of links in web part, in such a manner that title should appear as text and the Url should be bound in that text i.e. href=Url.

My code is:

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = null;                

        SPSite site = SPContext.Current.Site;
        SPWeb web = site.RootWeb;
        SPList list = web.Lists.TryGetList("QMS Links");

        SPQuery query = new SPQuery();
        query.Query = "<Where><Eq><FieldRef Name='oiplbActiveLink' /><Value Type='Boolean'>1</Value></Eq></Where>";

        query.ViewFields = "<FieldRef Name='oplLink'/>";
        query.ViewFieldsOnly = true;

        SPListItemCollection coll = list.GetItems(query);

        dt = coll.GetDataTable();


        lstLinks.DataSource = dt;
        lstLinks.DataBind();



    }

oplLink is a column of type Hyperlink.

My declarative code is:

<asp:ListView ID="lstLinks" runat="server">

<ItemTemplate>
    <table>
        <tr>
             <td><a href='#'><%#Eval("oplLink")%></a></td>
        </tr>   
    </table>
</ItemTemplate>   
</asp:ListView>

What should I do to view the text as title and bind the Url as href?

3 Answers 3

2

If oplLink is a hyperlink fieldtype, then you can implement what I explain in this answer: Duplicate response

Since you are already using code to get the items you want, you might as well take it one step further and alter the DataTable by creating a new DataTable

Example:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = null;                

    SPSite site = SPContext.Current.Site;
    SPWeb web = site.RootWeb;
    SPList list = web.Lists.TryGetList("QMS Links");

    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name='oiplbActiveLink' /><Value Type='Boolean'>1</Value></Eq></Where>";
    query.ViewFields = "<FieldRef Name='oplLink'/>";
    query.ViewFieldsOnly = true;

    SPListItemCollection coll = list.GetItems(query);

    lstLinks.DataSource = ProcessedDataSource(coll);
    lstLinks.DataBind();
}

private DataTable ProcessedDataSource(SPListItemCollection spListColl)
{
    var dt = new DataTable();
    dt.Columns.Add("LinkTitle");
    dt.Columns.Add("LinkUrl");

    foreach (SPListItem spItem in spListColl.Items)
    {
        var linkItem = new SPFieldUrlValue(spItem["oplLink"].ToString());
        var row = dt.NewRow();
        row["LinkTitle"] = linkItem.Name;
        row["LinkUrl"] = linkItem.Url;
        dt.Rows.Add(row);
    }

    return dt;
}

And then in your aspx page you can call the values like so:

<asp:ListView ID="lstLinks" runat="server">
    <ItemTemplate>
        <table>
            <tr>
                <td><a href='<%# Eval("LinkUrl")%>'><%# Eval("LinkTitle")%></a></td>
            </tr>   
        </table>
    </ItemTemplate>   
</asp:ListView>
0
0

wrap your aspx code in a repeater and use databinder to get value from column name

<a class="full" href="<%# DataBinder.Eval(Container.DataItem, "NavigationUrl").ToString().Split(',')[0] %>">

</a>

NavigationUrl is the column name that contains the url/text

so like this:

<asp:Repeater ID="Repeater1" runat="server">             
    <ItemTemplate>                 
       <li>   
            <a class="full" href="<%# DataBinder.Eval(Container.DataItem, "NavigationUrl").ToString().Split(',')[0] %>">

            </a>
       </li>             
    </ItemTemplate>         
</asp:Repeater> 

for you it will look like this:

<ItemTemplate>
    <table>                
        <tr>
             <td>
                    <a class="full" href="<%# DataBinder.Eval(Container.DataItem, "oplLink").ToString().Split(',')[0] %>">

                    </a>
             </td>
        </tr>   
    </table>
</ItemTemplate>  

in you aspx.cs codebehind add the databinding to the list:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
            BindData();
    }

    private void BindData()
    {
        SPWeb web = SPContext.Current.Web;

        SPList list = web.Lists["QMS Links"];
        DataTable Dt = list.Items.GetDataTable();
        Repeater1.DataSource = Dt; 
        Repeater1.DataBind();
    }

that should do the trick!

if codebehind only do this within createchildcontrols:

                        Repeater Repeater1 = new Repeater1();
                        web = SPContext.Current.Web;
                        list = web.Lists[_ListName];
                        Dt = list.Items.GetDataTable();
                        Repeater1 = new Repeater();
                        Repeater1.ItemDataBound += new RepeaterItemEventHandler(Repeater1_ItemDataBound);

                        Repeater1.DataSource = Dt;
                        Repeater1.DataBind();
                        Controls.Add(Repeater1);

now make the event for repeater:

    private void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        try
        {
            string urlImageClick = DataBinder.Eval(e.Item.DataItem, "NavigationUrl").ToString().Split(',')[0];

            Repeater1.Controls.Add(new LiteralControl("<a target='_parent' href='" + urlImageClick + "'>"));
        }
        catch (Exception a)
        {
            reportError.WriteError("Control ID: " + a.Source, a.Message, " Repeater1_ItemDataBound: at " + a.StackTrace, SPContext.Current.Site.Url);
        }
   }

above will give you a list of urls (href links)

so to clarify:

DataBinder.Eval(e.Item.DataItem, "NavigationUrl").ToString().Split(',')[0];

the above gives you the url part

DataBinder.Eval(e.Item.DataItem, "NavigationUrl").ToString().Split(',')[1];

above gives you the text, you need to change the NavigationUrl part to what ever the column is called that holds that value!

the repeater repeats for each item within the list that is passed into the datatable and iterated over and over again untill all items are done! (less code)

now for image its the same thing just change the html:

                    <a class="full" href="<%# DataBinder.Eval(Container.DataItem, "NavigationUrl").ToString().Split(',')[0]>">                             
                        <img src="<%# DataBinder.Eval(Container.DataItem, "ImageUrl").ToString().Split(',')[0] %>" class="full" alt="Tile" width="150px" height="160px"/>                        
                    </a>  

the above code will display an image, if you click on the image it will direct you to where ever the link is set ;) , so within your list you could have a hyperlink and an image within the same row as two columns, set the link first and set the image within the link. Same consept for anything else!

same goes for codebehind ;) , to have a hyperlink text, do as above, this time instead of adding an image within the "a" tag you can add a lable or just bind directly as iv shown you above within the databind part!

0
var linkItem = new SPFieldUrlValue(listItem["URL"].ToString());

SPFieldUrlValue value = new SPFieldUrlValue(linkItem.Description.ToString().Trim());

string URL = value.Url;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.