如何用文本框动态替换部分文本?

发布于 2024-11-08 03:45:35 字数 1955 浏览 0 评论 0原文

TL;DR:

只要我看到像 @token@ 这样的标记,我就需要将 TextBox 插入到占位符中,并且在 @ 上拆分不会削减它。我该怎么做?

设置:

我有一个数据库驱动的 ASP.NET 页面,在多个位置都有合并字段。在其中一个管理页面上,我想通过显示合并字段所在的文本框来使合并字段中的文本可编辑。文本的其余部分需要保持不可编辑。

输入看起来像这样:

“敏捷的棕色狐狸@foo@跳过@bar@懒惰的@baz@狗。”

这里的合并字段为 foobarbaz。我有一个 Dictionary ,其中包含合并字段的值:

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

目标:

对于每个文本块,我都有一个 PlaceHolder。我想要做的是为每个不可编辑的文本块动态插入一个标签,并为每个可编辑的块动态插入一个文本框。因此,上面的结果就好像我硬编码了这样的东西:

<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...

问题:

匹配 @token@ 模式很容易。诀窍是将标签和文本框添加到正确的位置。给定输入中可能有多个标记,因此我必须将其分成可编辑和不可编辑的文本,同时仍然跟踪每个 TextBox 对应的合并变量。出来,这并不像听起来那么容易。我可以使用正则表达式将输入字符串转换为 HTML,用 标记替换 @token@ 模式。然而,这有几个问题:

  • 我已经有大量用于动态服务器控件方法的代码。
  • 我只想匹配特定的标记,因此 /@[a-zA-Z\d]+@/ 不够严格。

之前的策略:

到目前为止,我只是简单地将输入字符串拆分为 @,然后为每个奇数子字符串插入一个 Label,为每个偶数子字符串插入一个 TextBox。效果很好,但要求正在发生变化。现在允许在变量名称之外使用 @ 符号,因此输入字符串可能如下所示:

blah [电子邮件受保护] @foo@废话

...在这种情况下,只有 foo 才是合并字段。

当前策略:

我觉得我需要迭代令牌并构建某种标签和文本框数组。问题是输入中可能存在任意数量的标记,因此一旦我拆分出 @foo@ 的文本框,我需要再次浏览文本以获取以下实例@bar@。我认为我需要某种递归来实现这一点,但我不能完全确定它。

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 [email protected] @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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开半夏魅人心 2024-11-15 03:45:35

你可以用正则表达式来做到这一点。
表达式看起来像这样:

@([a-zA-Z0-9])+@

这意味着它匹配所有以 @ 开头、包含至少一个字符 az 或 AZ 或 0-9 并以 @ 结尾的模式

Regex.Match

you can do it with Regular expressions.
The Expression would look something like:

@([a-zA-Z0-9])+@

this would mean that it matches for all patterns which start with @, contain at least one character a-z or A-Z or 0-9 and end with @

Regex.Match

聽兲甴掵 2024-11-15 03:45:35

在发布 fantasticfix 的答案之前,我开始研究使用字符串操作的递归方法。它似乎工作正常,所以我将其发布在这里供其他人使用:

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));
}

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));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文