Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4621150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:39:33+00:00 2026-05-22T02:39:33+00:00

TL;DR: I need to insert a TextBox into a PlaceHolder wherever I see a

  • 0

TL;DR:

I need to insert a TextBox into a PlaceHolder wherever I see a token like @token@, and splitting on @ won’t cut it. How can I do this?

Setup:

I have a database-driven ASP.NET page with merge fields in several places. On one of the admin pages, I want to make the text in the merge fields editable by showing TextBoxes where the merge fields are. The rest of the text needs to stay non-editable.

The input looks like this:

“the quick brown fox @foo@ jumps over @bar@ the lazy @baz@ dog.”

The merge fields here would be foo, bar, and baz. I have a Dictionary<string, string> with the value of the merge fields:

var Tokens = new Dictionary<string, string>
{
    { "foo", "john" },
    { "bar", "george" },
    //...etc...
};

Goal:

For each of these text blocks, I have a PlaceHolder. What I’d like to do is dynamically insert a Label for each block of uneditable text, and a TextBox for each editable block. So the results of the above would be as if I had hard-coded something like this:

<asp:Label   Text="the quick brown fox" />
<asp:TextBox ID="foo" Text="john" />
<asp:Label   Text="jumps over" />
<asp:TextBox ID="bar" Text="george" />
<asp:Label   Text="the lazy" />
...etc...

Problem:

Matching the @token@ pattern is easy. The trick is adding the Labels and TextBoxes in the right places. There might be more than one token in a given input, so I have to separate it out into editable and non-editable text, while still keeping track of which merge variable each TextBox corresponds to. As it turns out, this is not as easy as it sounds. I could use regular expressions to convert the input string into HTML, replacing the @token@ pattern with <input> tags. However, that has a couple of problems:

  • I already have a ton of code for the dynamic-server-controls approach.
  • I only want to match specific tokens, so /@[a-zA-Z\d]+@/ isn’t strict enough.

Previous Strategy:

Until now, I simply split the input string on @, then inserted a Label for each odd-numbered substring and a TextBox for each even substring. That worked fine, but the requirements are changing. @ symbols will now be allowed outside of a variable name, so the input string might look like:

blah blah@blah.com @foo@ blah

…in which case only foo would be a merge field.

Current Strategy:

I feel like I need to iterate through Tokens and build some sort of array of Labels and TextBoxes. The problem is that any number of tokens might be in the input, so once I split out TextBoxes for @foo@, I need to go through the text again for instances of @bar@. I think I need some sort of recursion for this, but I can’t quite put my finger on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:39:34+00:00Added an answer on May 22, 2026 at 2:39 am

    Before fantasticfix‘s answer was posted, I started working on a recursive method using string manipulation. It seems to be working correctly, so I’m posting it here for anyone else:

    private void tokenize(PlaceHolder ph, string str)
    {
        int index = str.IndexOf('@') + 1;
    
        if (index == 0)
        {
            ph.Controls.Add(new Label { Text = str });
            return;
        }
    
        ph.Controls.Add(new Label { Text = str.Substring(0, index - 1) });
    
        if (Tokens.Keys.Any(k => str.Substring(index).StartsWith(k + "@")))
        {
            int next = str.IndexOf("@", index);
            string key = str.Substring(index, next - index);
    
            ph.Controls.Add(new TextBox
            {
                ID = "txt" + key,
                Text = Tokens[key],
                TextMode = TextBoxMode.MultiLine,
                Rows = 2
            });
    
            index = next + 1;
        }
    
        tokenize(ph, str.Substring(index));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to insert data from table1 into table2. However, I would like to
Need to insert selected text on the page into textarea. There must be some
I need to insert records into a table that has no primary key using
I need to insert an HTML string into the <head> tag of the current
I need to insert about 1.8 million rows from a CSV file into a
I need to insert values in to the db. INSERT INTO tblname(f1,f2,f3) SELECT f1,f2,f3
I'm stuck on this code and can't figure out where I need to go.
I need to insert the text of a selected text box into a hidden
I've managed to run this query using wamp. INSERT INTO guest (guestno,familyname) VALUES(NULL,'Damn'); INSERT
Hi need insert data in table if the record is not already exits Ex:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.