0

I'm trying put a hyperlink on a label that I get from a database. how can this be done? I have tried something like this, however, it doesn't work at all:

for (int ctr = 0; ctr < 40; ctr++)
    {
        string input = labels[ctr].Text;
        string pattern = @"(http)*";

        Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
        MatchCollection matches = rgx.Matches(input);
        if (matches.Count > 0)
        {
            if (matches.Count > 0)
        {
            Console.WriteLine("match found");
            labels[matches.Count].Text = "<a href='" + labels[matches.Count].Text + "'>" + labels[matches.Count].Text + "</a>";
            break; + "</a>";
        }

    }

However, this code only puts it on one, and it's it on one, and even then, it's not always the right one.

Jareb
  • 19
  • 1
  • 10
  • 1
    Shouldn't you create a proper hyperlink (ie: `HyperLink link = new HyperLink();`) and add it to the label like this: `labels[ctr].Add()` or something similar? It's been a while I don't play with this. – emerson.marini Aug 21 '14 at 21:16
  • I still need it to only match when this is a link. I don't need to add all the words in the label a hyperlink. – Jareb Aug 21 '14 at 21:24

1 Answers1

0

I apologize for making this an answer and not a comment, but I would if I could.

Firstly, your regular expression will only match strings like "", "html" and "htmlhtml" which is probably not what you want if you're making a valid link. You need your regex to match the entire label/link and not just the "html" part of it.

Checking for a valid URL is an interesting problem, and here are some answers that might help:

Secondly, it's not clear how these links will be displayed. However, assuming that they eventually get placed into HTML and you are matching the link properly, then the way you are building the string should generate a valid hyperlink. Also, don't forget to escape any quotation marks in your labels.

Community
  • 1
  • 1
Tonkleton
  • 547
  • 3
  • 14
  • Thanks, I appreciate the help. But, if I check the whole label, then I would never get a valid link, unless the user just adds the link. I'm building a bulletin board, and the user will be able to add text to the label and somewhere in the label, add a link if they wanted to. I edited my code and got it to bind to one of the labels, however, couldn't get it to work for more than one. I'll post the edited code when I get back to work tomorrow. – Jareb Aug 22 '14 at 04:11
  • Also, I have an array of labels that gets data from SQL database that post it on the page. There's another page, where someone enters text in a textbox, and that sends it to the database. If there's a better way, I am all ears! – Jareb Aug 22 '14 at 04:12
  • So if I understand correctly: These labels may or may not contain a URL, and if so then we want to make that part of the label a hyperlink. If that's the case, you still need your regex to match the whole URL (not the whole label and not just the http part that you're currently matching). The links I provided should help with that. – Tonkleton Aug 22 '14 at 18:35
  • Also, be aware that letting users enter input that goes directly into a query is dangerous. SQL injection is something to be taken seriously (more info: http://en.wikipedia.org/wiki/SQL_injection). – Tonkleton Aug 22 '14 at 18:39
  • Correct. I ended up just adding a seperate label for the hyperlink. – Jareb Aug 23 '14 at 01:00