Kjensen
Kjensen

Reputation: 12384

Replacing text in html with a linkbutton

I have a variable that contains some html. I currently just put that in a literal on a page, and it works fine.

Now lets say every time the word "FISH" occurs, I want to replace the word with a linkbutton that does something.

I can find all occurrences of the word FISH with regex and replace it with something (html, text whatever). I also know how to make the linkutton with commandname etc.

But how can I put the linkbutton in the html and display it?

Is there another approach to this problem, I should be looking into?

Upvotes: 0

Views: 326

Answers (1)

LorenVS
LorenVS

Reputation: 12867

You'll have to parse your text into a set of controls.

Here's a simple solution (IndexOf instead of Regex)

string text = "some html FISH some html";
string fish = "FISH";

List<Control> controls = new List<Control>();
int lastIndex = 0;
int index = 0;
while((index = text.IndexOf(fish, index)) != -1)
{
    string s = text.Substring(lastIndex, index);
    LiteralControl literal = new LiteralControl(s);
    controls.Add(literal);

    LinkButton lb = new LinkButton();
    lb.CommandName = "whatever";
    controls.Add(lb);
}

foreach(Control c in controls)
{
    somePanel.Controls.Add(c);
}

The list is not necessary, as you could add directly to the panels control collection

Upvotes: 1

Related Questions