无法从登录视图中的登录模板访问控件

本文关键字:登录 访问 控件 视图 | 更新日期: 2023-09-27 17:56:50

aspx 页中的代码

<asp:LoginView ID="loginView1" runat="server" EnableViewState="false">
          <LoggedInTemplate>
                <asp:Button ID="btnWatchlist" runat="server" Text="+ Watchlist"  />
                <asp:Button ID="btnPM" runat="server" Text="PM" />
                <asp:Button ID="btnEdit" runat="server" Text="Edit" />
                <asp:Button ID="btnReport" runat="server" Text="Report" />
          </LoggedInTemplate>
</asp:LoginView>

aspx.cs(Codeback) 中的代码来访问按钮 ID 的

Button watchList = (Button)loginView1.FindControl("btnWatchlist");
Button pm = (Button)loginView1.FindControl("btnPM");
Button report = (Button)loginView1.FindControl("btnReport");
Button edit = (Button)loginView1.FindControl("btnEdit");

监视列表,下午,报告,编辑正在获取空值。我的代码有错误吗?

提前感谢..

无法从登录视图中的登录模板访问控件

也许是

愚蠢的问题,但您是否登录到您的网站?在测试时,我注意到控件仅在登录时可见,因此在Page_Load中使用 FindControl 将找不到控件,除非我已登录。