I was trying to learn how to make asp:Label detect web address inside texts and when a user clicks on the address it redirects to that address, but I got an error. The asp:Label displays its information from database inside a repeater control.
Here is the code:
public partial class New : System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
DataTable dt = new DataTable();
private SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\spotyearDB.mdf;Integrated Security = True");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterData();
}
lblData.Text = ConvertTextUrlToLink(lblData.Text);
}
public string ConvertTextUrlToLink(string rawString)
{
Regex linParser = new Regex(@"\b(?:http//|www\.)\S+\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
foreach (Match m in linParser.Matches(rawString))
{
if (m.Value.IndexOf("http") != -1 || m.Value.IndexOf("https") != -1)
{
rawString = rawString.Replace(m.Value, string.Format("<a target='_blank' href = '{0}'>{0}</a>", m.Value));
}
else
{
rawString = rawString.Replace(m.Value, string.Format("<a target='_blank' href = 'http://{0}'>{0}</a>", m.Value));
}
}
return rawString;
}
//Bind Data to Repeater Control
private void BindRepeaterData()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from [Table] Order By PostedDate desc", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
RepDetails.DataSource = dt;
RepDetails.DataBind();
con.Close();
}
}
From my C#, the error says:
"lblData.Text does not exist in the current context".
My label is inside a Table Cell, which is in Table Row and inside Repeater Control. And it displays text data from database like this:
<asp:label ID="lblData" runat="server" Text="<%#Eval ("comment")%>"></asp:label>
Could it be because the label is inside a Repeater Control?
HERE IS MY HTML
<body style="background-color: #F5F5F5; font-family: Nunito;">
<form id="form1" runat="server">
<div class="container" style="max-width: 100%;">
<br />
<asp:Repeater ID="RepDetails" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="card shadow p-3 mb5 bg-white rounded" style="border: 1px solid #d1d5d4; margin: 15px; padding: 0px 0px; width: auto; border-radius: 10px;">
<asp:Table runat="server">
<asp:TableRow>
<asp:TableCell>
<span class="time">
<asp:Label ID="lblUser" runat="server" ForeColor="#008000" Font-Size="11pt" Text='<%#Eval("Username") %>' />
<i class="fa fa-clock-o" aria-hidden="true" style="font-size: 10pt; font-weight: 100; color: #838996;"></i>
<asp:Label ID="lblDate" runat="server" ForeColor="#666666" Font-Size="7.5pt" Text='<%# Eval("PostedDate", "{0:MMMM d yyyy}") %>' />
<asp:Label runat="server" ForeColor="#666666" Font-Size="8pt" Text="at"></asp:Label>
<asp:Label ID="Datelabel" runat="server" ForeColor="#666666" Font-Size="7.5pt" Text='<%# Eval("PostedDate", "{0:hh:mm tt}") %>' /></span>
<hr />
<br />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>
<asp:Label ID="lblData" runat="server" Font-Size="9.5pt" Text='<%#Eval("Comment") %>' /><br />
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<br />
</div>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
</div>
</form>
</body>