65.9K
CodeProject is changing. Read more.
Home

Find child controls inside a MultiView control in asp.net

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.27/5 (4 votes)

Apr 19, 2006

2 min read

viewsIcon

68171

Finding a Label control inside a FormView which is a child control of MultiView

Find child controls inside a MultiView

[Finding a Label control inside a FormView which is a child control of MultiView]

Article Contents:

·         Overview

·         Section 1: Using MultiView and View in ASP.NET 2.0

·         Section 2: Finding Child Controls inside MultiView

·         Conclusion

Overview:

In ASP.NET 1.x, the Panel control was used to segregate controls into groups. Many developers would use a Panel to hide or show controls for a particular purpose. For instance, if the user clicked on a particular button, a DataGrid may appear. However, if the user clicked on a different button, a graph may appear.

In ASP.NET 2.0, the MultiView and View controls were introduced. The MultiView control acts as a stand-alone Panel-like control, or can be a container for multiple Panel-like controls called Views. Using the new MultiView control with the View control allows developers an even easier way to toggle between "Panels".

Section 1: Using MultiView and View in ASP.NET 2.0

Here is an example of using FormView inside the MultiView and View controls within ASP.NET 2.0:

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

    <asp:MultiView id="MultiView1" runat="server" ActiveViewIndex=0>

     <asp:View id="View1" runat="server">

    Content Here (View 1)...

       <asp:FormView ID="FormView1" runat="server"

         DataSourceID="SqlDataSource1">

         <ItemTemplate>

            Field1:

            <asp:Label ID="Field1Label" runat="server"

         Text='<%# Bind("Field1") %>'></asp:Label><br />

            Field2:

            <asp:Label ID="Field2Label" runat="server"

         Text='<%# Bind("Field2") %>'></asp:Label><br />

         </ItemTemplate>

       </asp:FormView>

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"

     ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

        SelectCommand="SELECT [Field1], [Field2] FROM [TestTable]">

     </asp:SqlDataSource>

   </asp:View>

   <asp:View id="View2" runat="server">      

           Content Here (View 2)...

   </asp:View>

   <asp:View id="View3" runat="server">

           Content Here (View 3)...

   </asp:View>

 </asp:MultiView>

 <asp:Button ID="Button1" runat="server" Text="Button" />

</div>

</form>

</body>

</html>

Section 2: Finding Child Controls inside MultiView

In the above HTML content we have a label control with ID="Field1Label". We are going to find value in this particular label as follows. Following code executes in the button click event of button with ID="Button1".

Sub GetControlValue()

  Section 1: Finding the first child control i.e. FormView

  Dim FormView As FormView=CType(MultiView1.Views(0).FindControl("FormView1"), FormView)

  Section 2: Finding the second child control i.e. Label

   If FormView.CurrentMode = FormViewMode.ReadOnly Then

     Dim Field1_Label As Label = CType(FormView.Row.FindControl("Field1Label"), Label)

Response.Write(Field1_Label.Text.ToString)

  End If

Note: FormView have 3 different type of Templates.

1.    ReadOnly Mode [ItemTemplate]

2.    Edit Mode [Edit ItemTemplate]

3.    Insert Mode [Insert ItemTemplate]

You should specify template of formview in which the control is embeded with formview property “CurrentMode” as above.

Protected Sub Button1_Click(ByVal sender _
    As Object, ByVal e As System.EventArgs)Handles Button1.Click
    GetControlValue()
End Sub

Conclusion

Hope you can find some use for this article.