views:

117

answers:

5

Hey everyone,

I have a string with special characters insert in different places. For example:

string myString = "This is a textbox: ##";

I would like to replace the ## with a control (namely, a textbox).

The Replace method only allows the string to be replaced with another string or character (understandably). But what would be the best way to dynamically replace the ## with a control in its position?

I was thinking maybe I could replace it with HTML markup which would be executed, but not quite sure how that would be achieved.

Thanks

EDIT: To clarify some details. The strings are being retrieved from a database, so I can't use the PlaceHolder control. The user selects a string from a drop-down list. The value of the item is the string with special characters. When the postback occurs from the item selection, I would like to display the string on the site, but replace the special characters with a fully working control (in this case, a textbox)

A: 
myString = string.Replace("##", "<input type='text' />");

Note that this is not a control: it will just be an html element that won't be wired up on the server side later. And depending on what you do with the string maybe not even that much, as some controls (like label) will automatically escape your < and > characters.

If you really want a fully-working asp.net control there we need to know more about how you are adding that string to the page.

Joel Coehoorn
Hey, same question as above. Would this method work with ASP.NET textboxes?
Skoder
Thanks for the reply. I'll update my question.
Skoder
The string can be added in any way that would enable this to work. For the time being, it's simply a literal control, but that can be changed.
Skoder
+1  A: 

Consider leveraging the TextBox's Render() method. That'll get you the HTML that would be output from that TextBox.

You can then use that string to be the replacement text to replace the ## portion of your string.

TextBox Render() on MSDN

var myTxtBox = new TextBox();
myTxtBox.Text = "Hello World";

//implement the Render code in here
string myRenderedTextBoxHTML = RenderIt(myTxtBox); 

string myString = "This is a textbox: " + myRenderedTextBoxHTML;

I'm unsure ViewState would be available for this control or not.

p.campbell
Hey, thanks, I'll have a read of that.
Skoder
I've had a read through, but I don't quite understand. What HTMLWriter do I pass through into the Render method? Or do I have to create my own custom control that extends the textbox?Thanks
Skoder
A: 

You could indeed replace it with markup:

string mystring = "This is a textbox: ##".Replace("##", "<input type='text'/>");
Response.Write(mystring);

I'm not sure why you would want to do this, though. Why not use a PlaceHolder control and just stick a TextBox in it in the code behind?

Steve Danner
Hey, the strings are being retrieved from the database. I'll try the replace method, but would that work with ASP.NET controls (and not just standard HTML controls?)
Skoder
Probably not. I like pcampbell's method of utilizing the Render method instead.
Steve Danner
+1  A: 

Something like this:

Panel panel = new Panel();
string myString = "This is a textbox: ##";
// some parsing logic
string[] arr = { "This is a textBox", "##" };

foreach(var item in arr)
{
  if (item == "##"){
    TextBox tb = new TextBox();
    panel.Controls.Add(tb);
  }
  else{
    Label l = new Label();
    l.Text = item;
    panel.Controls.Add(l);
  }
}

your_plaaceholder.Controls.Add(panel);
sashaeve
A: 

What Sash said, BUT make sure you put that in the Page.Init() every time if you wish to take advantage of viewstate.

Gene