在具有面板id的asp页面中查找面板

本文关键字:查找 asp id | 更新日期: 2023-09-27 18:25:49

在我的应用程序中,我从数据库中获取面板id,在网页中,我需要找到具有该id的面板,以使其可见false。后面的代码是

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
            Dim kioskxml As String = "pnlfindid"
            Dim myControl1 As Control = Page.FindControl(kioskxml)
            If (Not myControl1 Is Nothing) Then
                myControl1.Visible = False
            End If

        Catch ex As Exception
        End Try
    End Sub

但根据上面的代码片段,我无法获取面板,但它可以很好地与textbox和其他控件配合使用。我需要一种在页面加载上找到id的面板的方法

我的Html页面

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </ajaxToolkit:ToolkitScriptManager>
    <div onload="disableBackButton();">
    <table align="center" width="100%" cellpadding="0" cellspacing="0">
    <tr>
    <td>
        <asp:Panel ID="pnlfindId" runat="server">
          <table align="center" width="100%" cellpadding="0" cellspacing="0">
             <tr>
                <td> 
                <asp:Panel ID="pane3" runat="server">
                </asp:Panel>
                     </td>
                    </tr>

                 </table>
                   </asp:Panel>
             </td>
       </tr>

  </table>

     </asp:Content>

感谢

在具有面板id的asp页面中查找面板

也许您需要

Dim myControl1 As Control = Master.FindControl(kioskxml)

Dim myControl1 As Control = ContentPlaceHolder1.FindControl(kioskxml)

其他一切看起来都很好。。。

尝试此代码

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
            Dim myControl1 As Control =directcast(Page.FindControl("pnlfindid"),Control)
            If (Not myControl1 Is Nothing) Then
                myControl1.Visible = False
            End If

        Catch ex As Exception
        End Try
    End Sub

我假设您试图在内容页的加载事件中找到控件,而不是主页面的加载事件。

您需要首先找到内容占位符,然后在内容占位符中找到面板由于我不熟悉VB.NET语法,我提供C#语法:

ContentPlaceHolder cont = (ContentPlaceHolder)this.Master.FindControl("ContentPlaceHolder1");
Panel myPanel = cont.FindControl(kioskxml); 

当然,否则,如果你在母版页的页面加载事件上这样做

 ContentPlaceHolder cont = (ContentPlaceHolder)this.FindControl("ContentPlaceHolder1");
 Panel myPanel = cont.FindControl(kioskxml);