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



The information in this article applies to:

  • Microsoft ASP.NET (included with the .NET Framework 1.1)
  • Microsoft ASP.NET (included with the .NET Framework) 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:
  1. Start Microsoft Visual Studio .NET.
  2. 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.
  3. Add a TextBox ASP.NET server control to WebForm1.aspx, and then name the control TextBox1.
  4. Add two Button ASP.NET server controls to WebForm1.aspx.
  5. Right-click WebForm1.aspx, and then click View Code.
  6. 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("Display") = "none"
       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("Display") = "block"
       End Sub
    Visual C# .NET Sample Code
     private void Button1_Click(object sender, System.EventArgs e)
          { // To hide the text box
             TextBox1.Style.Add("Display","none");      
          }
    
          private void Button2_Click(object sender, System.EventArgs e)
          { // To display the text box
             TextBox1.Style.Add("Display","block");      
          }
    Note You can also assign the values in the codebehind file to other style attributes.
  7. On the Debug menu, click Start to run the application.
  8. Click Button1.

    Notice that TextBox1 is hidden.
  9. Click Button2.

    Notice that TextBox1 is visible.

STATUS

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

MORE INFORMATION

Steps to Reproduce the Behavior

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

REFERENCES

For more information about data binding, visit the following Microsoft Web site: For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

316946 BUG: Visual Studio .NET Editor Malforms HTML Tags

305140 INFO: ASP.NET

302342 HOW TO: Add ASP.NET Server Controls to a Web Form and


Modification Type:MajorLast Reviewed:8/8/2003
Keywords:kbpending kbControl kbstyle kbProperties kbide kbDataBinding kbhtml kbWebForms kbbug KB822320 kbAudDeveloper