如何在运行时动态突出显示或更改标签中某些单词的颜色 [英] How to highlight or change the color of some words in a label dynamically at runtime

查看:96
本文介绍了如何在运行时动态突出显示或更改标签中某些单词的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在asp.net中的输入字符串

ABCDEFGHIJKL。我想强调来自字符串的错误输入



假设用户在 A BCDEFGHIJKL中输入错误的输入A在这种情况下我想设置颜色对于那个特定的文本。

The following is my Input string in asp.net
ABCDEFGHIJKL. i want to highlight wrong input from string

suppose user enter wrong input "A" in ABCDEFGHIJKL in that case i wnt to set color to that particular text.

推荐答案

使用asp:Literal控件并设置为通过模式。

然后在代码中构建标记为要使用样式标签突出显示的文本:



标记:

Use a asp:Literal control and set to pass through mode.
Then in code construct the markup for the text you want to highlight using style tags:

Markup:
<asp:literal runat="server" id="literal1" mode="Passthrough" xmlns:asp="#unknown" />




private void YourInputMethod(string inputText)
{
    // Add your logic to validate correct or incorrect value
    // bool valid = Validate(...);
    // ...
    // string invalidValue = "" <-- set the invalid value
    // Replace the invalid text in the input with a placeholder:
    // inputText.Replace(invalidValue,"[invalid]");

    if (!valid)
    {
        string invalidStyle = Highlighter(invalidValue, "InvalidInputCSSStyle")
        inputText.Replace("[Invalid]",invalidStyle);
    }
    Literal1.Text = inputText;
}




private string Highlighter(string valueToStyle, string styleName)
{
    return "<label style="" + styleName + "">" + valueToStyle + "</label>";
}


这篇关于如何在运行时动态突出显示或更改标签中某些单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆