Basic Usage of MultiView and View Controls in ASP.NET - Presenting a user preferred View
(Page 5 of 5 )
One of the advantages of using the MultiView/View pair of controls is the ability to present a view based on user preference without going to another page. The next picture shows a web page with a MultiView Control containing two View controls. Depending on the choice made by the user, one or the other view will be presented to the user. The web page Choices.aspx shown next has two views, one showing an animal, and the other showing a flower.

The radio button group with the GroupName Test offers the user an opportunity to make a choice. The user makes a choice and clicks the What do you want to see? button. The code shown here displays the View based on user choice.
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked Then
'MultiView1.ActiveViewIndex = 0
MultiView1.SetActiveView(View1)
End If
If RadioButton2.Checked Then
MultiView1.SetActiveView(View2)
'MultiView1.ActiveViewIndex = 1
End If
End Sub
Only after clicking the button is one or the other view displayed, otherwise you will see the controls on the page, but not the controls on the views. The next picture shows the display when View1 is chosen.

Moving from View to View from within the MultiView Control
When there are a number of views it is possible to move from one view to another within the views using the properties, methods and events raised of the View Control or controls in the views. The next design shows a web page with two views.

View1 is displayed because the ActiveViewIndex is set to 0 in the MultiView control. When the "Go to View 2(Button2)" is clicked, the view displayed changes from View1 to View2 following the code shown in the next paragraph. In View 2, when the 'Greet' button is clicked the greeting's text is displayed in the textbox shown in View2.
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "Welcome to View2"
End Sub
Protected Sub Button2_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button2.Click
MultiView1.ActiveViewIndex = 1
End Sub
Properties and methods of the MultiView Control
Intellisense support provides support for coding as shown, revealing the available properties and methods of the MultiView Control. However, declarative syntax can also be used. The next picture shows how you may access the properties and methods from the code page. The paragraph that follows the declarative syntax of the MultiView control is copied from Microsoft's documentation.

<asp:MultiView
ActiveViewIndex="integer"
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
OnActiveViewChanged="ActiveViewChanged event handler"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
Visible="True|False"/>
Summary
MultiVew and View controls add functionality to web pages similar to the wizard control discussed in a previous tutorial. Only some of the very basic features are discussed; some of the properties, such as the CommndName property which can automatically switch from one view to the next without writing any code, are not discussed.
While placing controls on the Views some of the functionality needed for placement, such as alignment and spacing of controls from the menu item Format, are not available, but can be set using the property pages for the controls. The functionality provided by these controls may be used for taking surveys, presenting user defined and user based views.
It is possible sometimes that even when the View is added it does not show up in the drop-down in the .vb page. In this case it is necessary to close the application and re-open. Reproducibility of this errant behavior has not been tested.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |