Microsoft KB Archive/822320

= BUG: Data Binding Expression in the Style Attribute Is Deleted When You Switch from Design View to Source View in HTML =

Article ID: 822320

Article Last Modified on 5/23/2007

-

APPLIES TO


 * Microsoft ASP.NET 1.1
 * Microsoft ASP.NET 1.0

-



SYMPTOMS
When you use a data binding expression to assign values to the style attribute of an ASP.NET server control, the style attribute is deleted when you switch from Design view to Source view.



WORKAROUND
To work around this problem, assign the required values to the style attributes in the codebehind file instead of modifying the HTML code. To do this, follow these steps:  Start Microsoft Visual Studio .NET. Use Microsoft Visual Basic .NET or Microsoft Visual C# .NET to create a new ASP.NET Web Application project. By default, WebForm1.aspx is created. Add a TextBox ASP.NET server control to WebForm1.aspx, and then name the control TextBox1 . Add two Button ASP.NET server controls to WebForm1.aspx. Right-click WebForm1.aspx, and then click View Code.  Add the following sample code in the button click events to display and hide TextBox1:

Visual Basic .NET Sample Code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ' To hide the text box TextBox1.Style(&quot;Display&quot;) = &quot;none&quot; End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click ' To display the text box TextBox1.Style(&quot;Display&quot;) = &quot;block&quot; End Sub Visual C# .NET Sample Code private void Button1_Click(object sender, System.EventArgs e)     { // To hide the text box TextBox1.Style.Add(&quot;Display&quot;,&quot;none&quot;); }

private void Button2_Click(object sender, System.EventArgs e)     { // To display the text box TextBox1.Style.Add(&quot;Display&quot;,&quot;block&quot;); } Note You can also assign the values in the codebehind file to other style attributes.  On the Debug menu, click Start to run the application.</li> Click Button1.

Notice that TextBox1 is hidden.</li> Click Button2.

Notice that TextBox1 is visible.</li></ol>

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> Start Visual Studio .NET.</li> Use Visual Basic .NET or Visual C# .NET to create a new ASP.NET Web Application project. By default, WebForm1.aspx is created.</li> Add a TextBox ASP.NET server control to WebForm1.aspx.</li> In Design view, right-click WebForm1.aspx, and then click View HTML Source.</li>  Add DISPLAY to the style attribute, and then set the attribute to the server variable as follows: <asp:TextBox id=&quot;TextBox1&quot; style=&quot;DISPLAY :<%# VisibleVariable%>; Z-INDEX :101; LEFT :338px; POSITION :absolute; TOP :130px&quot; runat=&quot;server&quot;></asp:TextBox> Note You can dynamically assign VisibleVariable to the DISPLAY attribute, to show or hide the text box. </li> Right-click WebForm1.aspx, and then click View Code.</li> Edit the code in the codebehind file. For example, add a comment line in the Page_Load event.</li> Switch to Design view.</li> In Design view, right-click WebForm1.aspx, and then click View HTML Source.

Notice that the style attributes that are defined in server tags (<% %>) are deleted.</li></ol>

<div class="references_section">