Hi micah,
Refer the below sample.
protected void Page_Load(object sender, EventArgs e)
{
string text = "hello mic! lic.";
// Get DataTable from DataBase.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("UserName"), new DataColumn("Link") });
dt.Rows.Add("mic", "http://www.aspforums.net/Threads");
dt.Rows.Add("jic", "http://www.jqueryfaqs.com/");
dt.Rows.Add("lic", "https://www.aspsnippets.com/");
// Loop each row to check the existance.
// If exist then replace with anchor tag.
for (int j = 0; j < dt.Rows.Count; j++)
{
if (text.Contains(dt.Rows[j]["UserName"].ToString()))
{
text = text.Replace(dt.Rows[j]["UserName"].ToString(), "<a href=" + dt.Rows[j]["Link"].ToString() + ">" + dt.Rows[j]["UserName"] + "</a>");
}
}
Response.Write(text);
}
Input
hello mic! lic.
OutPut
hello mic! lic.
Here i have created sample that fullfill your requirement. You need to fetch the record from database and inplement the logic in your code.
HTML
<asp:DataList ID="GetMergedAll" runat="server" OnItemDataBound="GetMergedAll_ItemDataBound">
<ItemTemplate>
<table>
<tr>
<td>
<b>ID:</b>
<asp:Label ID="lblId" Text='<%# Eval("Id") %>' runat="server" />
</td>
</tr>
<tr>
<td>
<b>Name:</b>
<asp:Label ID="lblUserName" Text='<%# Eval("UserName")%>' runat="server" />
</td>
</tr>
<tr>
<td>
<b>Description:</b><br />
<asp:Label ID="lblDescription" Text='<%# Eval("Sentence")%>' runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateBooks();
}
}
private void PopulateBooks()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("UserName"), new DataColumn("Sentence") });
dt.Rows.Add(1, "mic", "hello andy!");
dt.Rows.Add(2, "mic", "he mic, i am antonio");
dt.Rows.Add(3, "mic", "Welcome andy,<br/> andy now you can ask question.");
dt.Rows.Add(3, "mic", "convert word in a sentence to hyperlink");
GetMergedAll.DataSource = dt;
GetMergedAll.DataBind();
}
private DataTable GetUserNameFromAccount()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("Id"), new DataColumn("Name") });
dt.Rows.Add(1, "mic");
dt.Rows.Add(2, "andy");
dt.Rows.Add(3, "brown");
dt.Rows.Add(3, "antonio");
return dt;
}
protected void GetMergedAll_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataTable dtName = GetUserNameFromAccount();
for (int i = 0; i < dtName.Rows.Count; i++)
{
if ((e.Item.FindControl("lblDescription") as Label).Text.Contains(dtName.Rows[i]["Name"].ToString()))
{
(e.Item.FindControl("lblDescription") as Label).Text = (e.Item.FindControl("lblDescription") as Label).Text.Replace(dtName.Rows[i]["Name"].ToString(), "<a href=#>" + dtName.Rows[i]["Name"] + "</a>");
}
}
}
}
OutPut
ID: 1 |
Name: mic |
Description: hello andy! |
|
|
ID: 3 |
Name: mic |
Description: Welcome andy, andy now you can ask question. |
|
ID: 3 |
Name: mic |
Description: convert word in a sentence to hyperlink |
|