Solved

how to make string as link

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

770 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