65.9K
CodeProject 正在变化。 阅读更多。
Home

在 asp.net 的 MultiView 控件中查找子控件

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.27/5 (4投票s)

2006 年 4 月 19 日

2分钟阅读

viewsIcon

68484

查找 MultiView 的子控件 FormView 内的 Label 控件。

在 MultiView 中查找子控件

[在 MultiView 的子控件 FormView 中查找 Label 控件]

文章目录

·         概述

·         第一部分:在 ASP.NET 2.0 中使用 MultiView 和 View

·         第二部分:在 MultiView 中查找子控件

·         结论

概述

在 ASP.NET 1.x 中,Panel 控件用于分隔控件组。许多开发人员会使用 Panel 来隐藏或显示特定用途的控件。例如,如果用户单击某个按钮,可能会出现一个 DataGrid。但是,如果用户单击另一个按钮,可能会出现一个图表。

在 ASP.NET 2.0 中,引入了 MultiView 和 View 控件。MultiView 控件可以作为独立的类似 Panel 的控件,也可以是包含多个名为 View 的类似 Panel 的控件的容器。使用新的 MultiView 控件和 View 控件,开发人员可以更轻松地在“Panel”之间切换。

第一部分:在 ASP.NET 2.0 中使用 MultiView 和 View

以下是在 ASP.NET 2.0 中使用 MultiView 和 View 控件的 FormView 示例。

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

<head runat="server">

    <title>未命名页面</title>

</head>

<body>

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

<div>

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

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

    此处是内容(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">      

           此处是内容(View 2)...

   </asp:View>

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

           此处是内容(View 3)...

   </asp:View>

 </asp:MultiView>

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

</div>

</form>

</body>

</html>

第二部分:在 MultiView 中查找子控件

在上面的 HTML 内容中,我们有一个 ID 为 "Field1Label" 的 Label 控件。我们将按如下方式查找此特定 Label 的值。以下代码在 ID 为 "Button1" 的按钮的单击事件中执行。

Sub GetControlValue()

  第一部分:查找第一个子控件,即 FormView

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

  第二部分:查找第二个子控件,即 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

注意:FormView 有 3 种不同的模板。

1.    只读模式 [ItemTemplate]

2.    编辑模式 [Edit ItemTemplate]

3.    插入模式 [Insert ItemTemplate]

您应该使用 FormView 的“CurrentMode”属性指定控件嵌入在哪个 FormView 模板中,如上所示。

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

结论

希望这篇文章能对您有所帮助。

© . All rights reserved.