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("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. - On the Debug menu, click
Start to run the application.
- Click Button1.
Notice that
TextBox1 is hidden. - Click Button2.
Notice that
TextBox1 is visible.