0

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(Codebehind) 中访问按钮 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");

watchList,pm,report,edit 得到空值。我的代码有错误吗?

提前致谢..

4

1 回答 1

0

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

于 2013-01-31T07:26:39.867 回答