首页 > 解决方案 > ContentPage 和 UpdatePanel vb.net 中的 FindControl

问题描述

被这个难住了,已经搜索了各种答案,没有一个能够解决。继续获取ctl00 is not declared due to protection level

ct100如果我删除MasterPage 并从开始ContentPlaceHolder但我得到空引用,不要得到错误?

还想看看 UpdatePanel 是否也在其中发挥作用?

还想知道为什么Handles TextBox4.TextChanged需要在 MasterPage 中删除?

    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="resetPassword.aspx.vb" Inherits="resetPass" title="reset PW" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    <h2>Password Recovery</h2>

    <div style="border-style: double; background-color: #E7E7E7; width: 375px;">
    <div class="grayBox" 
            style="font-weight: bold; font-size: large; text-align: center;">
        CHANGE / RESET PASSWORD</div>
    <br />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <contenttemplate>
                &nbsp;&nbsp; User Name:
                <asp:TextBox ID="TextBox4" runat="server" AutoPostBack="true" OnTextChanged="TextBox4_TextChanged"></asp:TextBox>
                <br />
                <br />
                &nbsp;&nbsp; Password Question:
                <asp:Label ID="Label1" runat="server" Text="?" Font-Bold="True" Font-Size="14" ForeColor="Red"></asp:Label><br />
                <div id="results" runat="server"></div>
                <br />
                 &nbsp;&nbsp; Password Answer:&nbsp;&nbsp;&nbsp; 
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <br />
                <br />
                &nbsp;&nbsp; New Password:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; 
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <br />
                <br />
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="Button1" runat="server" Text="Change Password" />
                <br />
                <br />
                <div style="font-size: large; text-align: center;"><asp:Label ID="Label2" runat="server" Text="" Text-Align="center"></asp:Label></div>
                <br />
            </contenttemplate>
            <Triggers>

            </Triggers>
    </asp:UpdatePanel>
    </div>

    </asp:Content>

VB:

    Protected Sub TextBox4_TextChanged(sender As Object, e As System.EventArgs) 'Handles TextBox4.TextChanged
        Dim ctl00 As MasterPage = TryCast(FindControl("ctl00"), MasterPage)
        Dim MainContent As ContentPlaceHolder = TryCast(FindControl("MainContent"), ContentPlaceHolder)
        Dim TB4 As TextBox = TryCast(ctl00.MainContent.FindControl("TextBox4"), TextBox)
        Dim uName As String = TB4.Text
        Dim u As MembershipUser = myProvider.GetUser(uName, True)
        Dim L1 As Label = ctl00.MainContent.FindControl("Label1")
        Try
            If u Is Nothing Then
                L1.Text = "No user found"
            Else
                L1.ForeColor = System.Drawing.Color.Green
                L1.Text = u.PasswordQuestion
            End If
        Catch ex As Exception
            'xxx
        Finally
            conn.Close()
        End Try
    End Sub

标签: vb.net

解决方案


从该页面的代码隐藏访问内容页面控件时,您无需通过母版页。

所以,而不是

Dim L1 As Label = ctl00.MainContent.FindControl("Label1")
L1.Text = "No user found"

您可以使用

Label1.Text = "No user found"

等等。


推荐阅读