在asp.net(使用vb.net)中,如何在位于网页上的动态创建的用户控件中引用标签控件?

时间:2014-12-30 04:09:08

标签: asp.net vb.net

我有一个网页(使用母版页和2个内容部分),上面有一个初始用户控件。我将动态创建的用户控件放在该初始用户控件上某个位置(PlaceHolder内)的初始用户控件中。

在其Page_Init事件中动态构建用户控件后,在动态创建的用户控件Page_PreRender事件中,我试图使用FindControl方法引用一个名为strExistingAssignementsLabel的标签控件,这样我就可以设置它的" .text& #34;但它找不到它。

我检查"什么都不是"这就是我得到的。

当我查看页面源代码(此处未显示所有内容。)时,我看到我正在寻找的ID就在那里。它是strExistingAssignementsLabel。如下图所示:

<div id="ExistingAssignmentsBreakout"> 
    <table style="margin-left: 30px;"><tr><td>Existing Assignments:</td><td><asp:Label
     runat="server" ID=strExistingAssignementsLabel style="font-family: 'Trebuchet MS','Tahoma';
      font-size: 18px;"></asp:Label></td></tr><tr><td>&nbsp;</td><td><table><tr><td>IP:</td><td>
     <asp:Label runat="server" ID=strIPAddressLabelID style="font-family:

我确实看到源中没有占位符 - phAssignment。应该在那吗?

有关如何解决此问题的任何想法?

为简单起见......这里是包含初始用户控件的网页。

 <%@ Page Title="Account Preferences - Control Panel" Language="VB" 
  MasterPageFile="Layout.master" AutoEventWireup="false" CodeFile="createipjustification.aspx.vb" 
  Inherits="v2_preferences" ValidateRequest="false" %>
  <%@ Register TagPrefix="RSNET" TagName="IPJustificationAdd"
  Src="~/UserControls/User/IPJustification-Add.ascx" %>

 <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> </asp:Content>
 <asp:Content ID="Content2" ContentPlaceHolderID="content" Runat="Server">
 <div id="IpJustification">
    <RSNET:IpJustificationAdd ID="ipjaIPJustificationAdd" runat="server" />
 </div>
 </asp:Content>

这是我在初始用户控件中放置的动态创建的用户控件。 (我在代码隐藏中构建了用户控件HTML - 在Page_Init事件中。):

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="ExistingAssignmentsBreakout.ascx.vb"
Inherits="UserControls_User_ExistingAssignmentsBreakout" %>

<div id="ExistingAssignmentsBreakout"> 
  <asp:PlaceHolder ID="phAssignment" runat="server"></asp:PlaceHolder>
</div>

这是我将动态创建的用户控件放入的初始用户控件。我将动态创建的用户控件插入到我想要的位置。它位于PlaceHolder内。

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="IPJustification-Add.ascx.vb"
Inherits="UserControls_User_IPJustification_Add" %>
<%@ Register TagPrefix="NET" TagName="ExistingAssignments"
Src="~/UserControls/User/ExistingAssignmentsBreakout.ascx" %>

<div style="width: 600px; display: block;>
   <table style="margin-left: 30px;">
       <tr>
           <td>Customer:</td>
           <td style="width: 420px;">
               <asp:Label ID="lblCustomerName" runat="server" style="font-family: 'Trebuchet MS'>
           </td>
       </tr>
       <asp:PlaceHolder ID="iaphExistingAssignments" runat="server">
          <NET:ExistingAssignments ID="ExistingAssignments" runat="server" />
       </asp:PlaceHolder>
       <tr>
           <td>Requested Subnet Size:</td>
           <td>
               <asp:DropDownList runat="server" id="ddlRequestedSubnetSize" style="font-family:
               'Trebuchet MS'>
               </asp:DropDownList>
           </td>
       </tr>
   </table>
</div>

在Page_Init中(按比例缩小......):

    Dim litHtml As Literal

    Dim labelHtml As Label
    Dim labelExistingAssignments As Label
    Dim labelIP As Label

    Dim strExistingAssignementsLabelID As String
    Dim strIPAddressLabelID As String

    ' Start building the HTML.
    litHtml = New Literal
    litHtml.Text = "<table style=""margin-left: 30px;"">"
    phAssignment.Controls.Add(litHtml)

    litHtml = New Literal
    litHtml.Text = "<tr><td>Existing Assignments:</td><td><asp:Label runat=""server"" 
    ID=""strExistingAssignementsLabel"" style=""font-family: 'Trebuchet MS','Tahoma'; font-size: 
    18px;""></asp:Label></td></tr>"
    phAssignment.Controls.Add(litHtml)

    ' End building the HTML.
    litHtml = New Literal
    litHtml.Text = "</table>"
    phAssignment.Controls.Add(litHtml)

在Page_PreRender中(按比例缩小......):

    Dim labelExistingAssignments As Label
    Dim strExistingAssignmentsID As String

     ' Find the ID in the page. 
     labelExistingAssignments = CType(phAssignment.FindControl("strExistingAssignementsLabel"), Label) 

     If labelExistingAssignments Is Nothing Then
        lblMessage.Text = "Is Nothing"
     End If

根据建议,我添加了一个执行FindControl的函数。我称之为:

    Dim labelExistingAssignments As Label
    labelExistingAssignments = FindChildControl(phAssignment, "strExistingAssignementsLabel")

功能:

    Public Function FindChildControl(start As Control, id As String) As Control

       If start IsNot Nothing Then
           Dim foundControl As Control
           fcoundControl = start.FindControl(id)

           If foundControl IsNot Nothing Then
              Return foundControl
           End If

           For Each c As Control In start.Controls
               foundControl = FindChildControl(c, id)
               If foundControl IsNot Nothing Then
                   Return foundControl
               End If
           Next
       End If

       Return Nothing
   End Function

0 个答案:

没有答案