I am trying to determine the backcolor of a textbox and have an event happen. I've been using:
"If txtbox.backcolor = System.Drawing.Color.Red Then
End if"
But this dont work. Can somone help me?
ml
I am trying to determine the backcolor of a textbox and have an event happen. I've been using:
"If txtbox.backcolor = System.Drawing.Color.Red Then
End if"
But this dont work. Can somone help me?
ml
noted you solved this yourself. For others who hit the same snag: there are two common causes and simple fixes.
First, ASP.NET server controls only expose a BackColor value if you set it server‑side (BackColor property). If the textbox color is coming from a CSS class, theme, or client script the server property will be Color.Empty and a server-side equality test will never match. See the WebControl.BackColor property for details: WebControl.BackColor.
Second, when comparing System.Drawing.Color values use a robust comparison. Comparing ARGB integers avoids subtle differences from named colors:
If TextBox1.BackColor.IsEmpty Then
' BackColor not set server-side (likely CSS). Handle accordingly.
ElseIf TextBox1.BackColor.ToArgb() = System.Drawing.Color.Red.ToArgb() Then
' matched red
End If If the page sets background via CSS or client code, read the computed style on the client and post it back (hidden field or AJAX). Example JavaScript approach:
var bg = window.getComputedStyle(document.getElementById('<%= TextBox1.ClientID %>')).backgroundColor;
document.getElementById('<%= hiddenField.ClientID %>').value = bg; To translate hex/HTML color strings to a Color on the server, use ColorTranslator.FromHtml. See the API for conversion details: ColorTranslator.FromHtml.
— encouraging OPs to paste solutions is helpful; sharing the exact approach that fixed the issue (server vs client, or a specific comparison method) saves others time.
Sorry about that. I found the solution on the web. Thanks anyway!
U can paste ur solution so that it would be helpful for someone else who need the same....
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.