简体   繁体   English

更改其背景色时,将重置文本框的前色,反之亦然(使用PostBack)

[英]The forecolor of textbox is reset when I change its backcolor, vice versa (with PostBack)

I have one main textbox with multiline textmode, and another two textboxes with color textmode, one is to change the text-color and the other one to change the background-color of the main textbox. 我有一个带有多行文本模式的主文本框,另外两个带有颜色文本模式的文本框,一个是更改文本颜色,另一个是更改主文本框的背景颜色。

The problem is that if I change the background color after changing the text color, the text color will be overridden by black color (I guess this is the default color), or if I change the text color after changing the background color, the background color will be overridden by white color (the default color I guess). 问题是,如果在更改文本颜色后更改背景颜色,则文本颜色将被黑色(我猜这是默认颜色)覆盖,或者如果在更改背景颜色后更改文本颜色,则背景颜色将被白色(我猜是默认颜色)覆盖。

I know the textbox value is posted with PostBack request, but I thought ViewState should store the control information for textbox. 我知道文本框值是与PostBack请求一起发布的,但是我认为ViewState应该存储文本框的控件信息。 It still does not if I enable ViewState for the main textbox explicitly. 如果我显式启用主文本框的ViewState,它仍然不会。

Somehow I can fix this problem. 我可以以某种方式解决此问题。 But, why does it behave like this? 但是, 为什么会这样呢?

UPDATE 更新

I have checked this resource: http://www.c-sharpcorner.com/UploadFile/ee01e6/viewstate-for-textbox-in-Asp-Net/ 我已经检查了以下资源: http : //www.c-sharpcorner.com/UploadFile/ee01e6/viewstate-for-textbox-in-Asp-Net/

When I use radiobuttons, it works. 当我使用单选按钮时,它可以工作。 If I use the textbox as the color-picker, then it does not work. 如果我使用文本框作为颜色选择器,则它不起作用。

Is this a bug of textbox when used with TextMode set to Color?? 将TextMode设置为Color时,这是文本框的错误吗?

Here is the main textbox: 这是主要的文本框:

    <asp:TextBox ID="txt_Editor"
        TextMode="MultiLine"
        Columns="50" Rows="10"
        EnableViewState="true"
        runat="server"></asp:TextBox>

Here are the two: 这是两个:

    <div class="text-muted">Color Options:</div>
    Text-color:
    <asp:TextBox ID="txt_TextColor" 
        AutoPostBack="true" TextMode="Color"
        Text="#000000" runat="server" OnTextChanged="txt_TextColor_TextChanged">
    </asp:TextBox>

    Background-color:
    <asp:TextBox ID="txt_BackColor"
        TextMode="Color" AutoPostBack="true"
        Text="#FFFFFF" runat="server" OnTextChanged="txt_BackColor_TextChanged"></asp:TextBox>

Here is the related part in the CodeBehind file, whenever a color is chosen the colors of the main textbox should be updated accordingly: 这是CodeBehind文件中的相关部分,每当选择一种颜色时,应相应地更新主文本框的颜色:

    protected void txt_TextColor_TextChanged(object sender, EventArgs e)
    {
        txt_Editor.ForeColor = Color.FromName(txt_TextColor.Text);
    }

    protected void txt_BackColor_TextChanged(object sender, EventArgs e)
    {
        txt_Editor.BackColor = Color.FromName(txt_BackColor.Text);
    }

Got it, the problem has nothing to do with textboxes nor ViewStates, its the Color.FromName, if you use instead ColorTranslator.FromHtml it works perfectly, so I suppose the color returned from Color.FromName is really invalid. 知道了,问题与文本框或ViewStates无关,它与Color.FromName无关,如果使用ColorTranslator.FromHtml代替,它可以正常工作,所以我认为从Color.FromName返回的颜色确实无效。

Really an strange bug. 真是个奇怪的错误。

EDIT: Ok, so I checked the resulting colors and it's not really a bug, FromName must be used only on named colors "red", "blue", etc, if you use it this way it creates a color which has these properties (values are examples) "{Name=#6bea55, ARGB=(0, 0, 0, 0)}", as you can see the name has a prepended # and the RGB values are all zeros because it can't parse it, but if you use the translator then it creates a color like this "{Name=ff6bea55, ARGB=(255, 107, 234, 85)}", so what's happening under the hood is that HTML renderer is using the Name property to fill the value of the style property but the ViewState is storing the ARGB values, when you do a postback it restores those ARGB values and thus the color is lost. 编辑:好的,所以我检查了生成的颜色,它并不是真正的错误,FromName必须仅用于命名的颜色“红色”,“蓝色”等,如果您以此方式使用它,则会创建具有这些属性的颜色(值是示例)“ {Name =#6bea55,ARGB =(0,0,0,0)}”,如您所见,该名称带有前缀#,并且RGB值全为零,因为它无法解析,但是如果您使用翻译器,则它会创建类似“ {Name = ff6bea55,ARGB =(255,107,234,85)}”的颜色,所以在幕后发生的事情是HTML渲染器正在使用Name属性填充样式属性的值,但是ViewState将存储ARGB值,当您执行回发操作时,它将还原这些ARGB值,因此颜色会丢失。

您将需要在隐藏字段或视图状态中捕获颜色,并在回发后进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 可以在异步任务中更改前景色但不能更改文本框的背景色 - Can change forecolor but not backcolor of textbox in async task 将禁用的TextBox的ForeColor设置为与C#中的BackColor相同 - Set a disabled TextBox's ForeColor to be the same as its BackColor in C# 如何根据输入到文本框的方式更改标签的原色和底色 - How to change the forecolor and backcolor of a label based on input into a textbox 更改文本框中文本的前景色和背景色 - Changing the forecolor and backcolor of text in a textbox 更改RichTextBox高亮显示BackColor和ForeColor - Change RichTextBox highlight BackColor and ForeColor 如何更改禁用的数字上调的前色/背景色? - How can I change the forecolor / backcolor of a disabled numeric updown? 在C#中更改DataGridViewRows的foreColor和BackColor - Change foreColor and BackColor of DataGridViewRows in C# 如何更改 ObjectListView 中单个单元格的 BackColor/ForeColor - how to change BackColor/ForeColor of a single cell in ObjectListView Winform-自定义文本框在只读时更改背景色 - Winform - Custom TextBox change backcolor when Readonly 如何在DataGrid中检测是否单击了行并将其列从true更改为false,反之亦然? - How can I detect in a DataGrid if row is clicked and change its column from true to false and vice versa?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM