Thursday, March 22, 2012

marking phrases inside outputted text

When I output a text from a database table. I want to run though it with a list of other values and make a link out of the values which appear in the given text.
Link an encyclopedia effect. When some value (which has it's own page) appears, it will make a link out of the word.
Is there any ready-to-use script that does so, in ASP.NET 1.1?

Thanks,

Sagi.

Hello again Sagi,

The following is something I have made for you: -

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="DataIntoLinks.aspx.cs" Inherits="DataIntoLinks" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:Repeater ID="rpt1" runat="server" OnItemDataBound="rpt1_ItemDataBound">
<ItemTemplate>
<asp:Label ID="lblSummary" runat="server" />
<hr />

</ItemTemplate>
</asp:Repeater>

</div>
</form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class DataIntoLinks : System.Web.UI.Page
{

DataTable dtLinks;

// this works so that summer is caught if it is in the beginning/middle/end of a sentence
// but ensures that SummerSlam for example does not get turned into a half link word
// just because it has summer in it
string[] strAfterWordChars = { " ", ".", ",", ":"};


protected void Page_Load(object sender, EventArgs e)
{

// 1) generate the main data
DataTable dt = new DataTable();
dt.Columns.Add("Summary");

DataRow dr = dt.NewRow();
dr["Summary"] = "I like to go travelling in summer. I like Japan. ";
dt.Rows.Add(dr);

dr = dt.NewRow();
dr["Summary"] = "The weather is too hot in Japan in summer";
dt.Rows.Add(dr);


// 2) generate the links
dtLinks = new DataTable();
dtLinks.Columns.Add("LinkText");

string[] strLinks = {"summer", "hot", "Japan"};

for (int i = 0; i < strLinks.Length; i++)
{
DataRow drLink = dtLinks.NewRow();
drLink["LinkText"] = strLinks[i];
dtLinks.Rows.Add(drLink);
}


// steps 1 and 2 should be handled by database lookups
rpt1.DataSource = dt;
rpt1.DataBind();
}


protected void rpt1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string strSummary = ((System.Data.DataRowView)e.Item.DataItem)["Summary"].ToString();

foreach (DataRow dr in dtLinks.Rows)
{
string strLink = dr["LinkText"].ToString();

for (int i = 0; i < strAfterWordChars.Length; i++)
{
strSummary = strSummary.Replace(" " + strLink + strAfterWordChars[i],
" <a href='" + strLink + ".htm'>" + strLink + "</a>" + strAfterWordChars[i]);
}
}

Label lbl = (Label)e.Item.FindControl("lblSummary");
lbl.Text = strSummary;
}
}


}

Kind regards

Scotty

0 comments:

Post a Comment