Solved

how to make string as link

Posted on 2013-10-26
2
251 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

23 Experts available now in Live!

Get 1:1 Help Now