Solved

how to make string as link

Posted on 2013-10-26
2
250 Views
Last Modified: 2013-10-30
hi i have one string like this:

Dim vstr as string = "trees, cars, houses"

i want some function that conver this to this.

<a href="/category.aspx?c=trees">trees</a><a href="/category.aspx?c=cars">cars</a><a href="/category.aspx?c=houses">houses</a>

so this way i can read and call the funcion inside aspx page.
0
Comment
Question by:rafaelrgl
2 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39602739
.Net is not my strong area but try this

Dim vstr as string = "trees, cars, houses"

Dim words as string = vstr.Split(',');

Dim item as string

For Each item In words
	Response.write '<a href="/category.aspx?c=" & item & '">' & item & '</a>'
Next

Open in new window

0
 
LVL 15

Expert Comment

by:pateljitu
ID: 39606081
Have provided with 2 different output example, one does response.write to <a href> tab and another one take .NET approach and binds data to repeater.

// CODE BEHIND .cs file
protected void Page_Load(object sender, EventArgs e)
        {
            // Output to <A HREF> tag
            System.Text.StringBuilder abc = createHrefSample1("trees, cars, houses");
            Response.Write(abc.ToString());


            // Output to Repeater
            System.Data.DataTable pDT = new System.Data.DataTable();
            pDT = createHrefSample2("trees, cars, houses");

            Repeater1.DataSource = pDT;
            Repeater1.DataBind();
        }

        private System.Text.StringBuilder createHrefSample1(string sCategory)
        {
            System.Text.StringBuilder sLinkCategory = new System.Text.StringBuilder();
            string[] SCategory = sCategory.Split(',');

            foreach (string element in SCategory)
            {
                sLinkCategory.Append("<a href='/category.aspx?c=" + element.ToString().Trim() + "'>" + element.ToString().Trim() + "</a>");
            }

            return sLinkCategory;
        }

        public System.Data.DataTable createHrefSample2(string sCategory)
        {
            System.Text.StringBuilder sLinkCategory = new System.Text.StringBuilder();
            string[] SCategory = sCategory.Split(',');
            System.Data.DataTable dt = new System.Data.DataTable();
            dt.Columns.Add("sCategory");

            foreach (string element in SCategory)
            {
                dt.Rows.Add(element.ToString().Trim());
            }

            return dt;
        }

Open in new window


.ASPx page
<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
            <asp:HyperLink ID='HLCategory' runat="server" NavigateUrl='<%#Eval("sCategory","/category.aspx?c={0}")%>'><%#Eval("sCategory")%></asp:HyperLink><br />

        </ItemTemplate>
    </asp:Repeater>

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now