2012-07-11 123 views
2

我無法訪問我的代碼隱藏中的任何我的asp控件。在.aspx頁面如下無法在代碼隱藏中訪問asp.net changepassword控件

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="ChangePassword.aspx.cs" Inherits="ChangePassword" ValidateRequest="false" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
<asp:ChangePassword ID="ChangePassword1" runat="server" CssClass="MinutesList" 
    onchangedpassword="ChangePassword1_ChangedPassword"> 
    <ChangePasswordTemplate> 
     <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
      <tr> 
       <td> 
        <table cellpadding="0"> 
         <tr> 
          <td align="center" colspan="2"> 
           Change Your Password</td> 
         </tr> 
         <tr> 
          <td align="right"> 
           <asp:Label ID="CurrentPasswordLabel" runat="server" 
            AssociatedControlID="CurrentPassword">Password:</asp:Label> 
          </td> 
          <td> 
           <asp:TextBox ID="CurrentPassword" runat="server" TextMode="Password" Visible="false" >></asp:TextBox> 
           <asp:RequiredFieldValidator ID="CurrentPasswordRequired" runat="server" 
            ControlToValidate="CurrentPassword" ErrorMessage="Password is required." 
            ToolTip="Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator> 
          </td> 
         </tr> 
         <tr> 
          <td align="right"> 
           <asp:Label ID="NewPasswordLabel" runat="server" 
            AssociatedControlID="NewPassword">New Password:</asp:Label> 
          </td> 
          <td> 
           <asp:TextBox ID="NewPassword" runat="server" TextMode="Password"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="NewPasswordRequired" runat="server" 
            ControlToValidate="NewPassword" ErrorMessage="New Password is required." 
            ToolTip="New Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator> 
          </td> 
         </tr> 
         <tr> 
          <td align="right"> 
           <asp:Label ID="ConfirmNewPasswordLabel" runat="server" 
            AssociatedControlID="ConfirmNewPassword">Confirm New Password:</asp:Label> 
          </td> 
          <td> 
           <asp:TextBox ID="ConfirmNewPassword" runat="server" TextMode="Password"></asp:TextBox> 
           <asp:RequiredFieldValidator ID="ConfirmNewPasswordRequired" runat="server" 
            ControlToValidate="ConfirmNewPassword" 
            ErrorMessage="Confirm New Password is required." 
            ToolTip="Confirm New Password is required." ValidationGroup="ChangePassword1">*</asp:RequiredFieldValidator> 
          </td> 
         </tr> 
         <tr> 
          <td align="center" colspan="2"> 
           <asp:CompareValidator ID="NewPasswordCompare" runat="server" 
            ControlToCompare="NewPassword" ControlToValidate="ConfirmNewPassword" 
            Display="Dynamic" 
            ErrorMessage="The Confirm New Password must match the New Password entry." 
            ValidationGroup="ChangePassword1"></asp:CompareValidator> 
          </td> 
         </tr> 
         <tr> 
          <td align="center" colspan="2" style="color:Red;"> 
           <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal> 
          </td> 
         </tr> 
         <tr> 
          <td align="right"> 
           <asp:Button ID="ChangePasswordPushButton" runat="server" 
            CommandName="ChangePassword" Text="Change Password" 
            ValidationGroup="ChangePassword1" /> 
          </td> 
          <td> 
           <asp:Button ID="CancelPushButton" runat="server" CausesValidation="False" 
            CommandName="Cancel" Text="Cancel" /> 
          </td> 
         </tr> 
        </table> 
       </td> 
      </tr> 
     </table> 
    </ChangePasswordTemplate> 
    <SuccessTemplate> 
     <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
      <tr> 
       <td> 
        <table cellpadding="0"> 
         <tr> 
          <td align="center" colspan="2"> 
           Change Password Complete</td> 
         </tr> 
         <tr> 
          <td> 
           Your password has been changed!</td> 
         </tr> 
         <tr> 
          <td> 
           <asp:Button ID="HomePageButton" runat="server" Text="Continue" CausesValidation="true" 
            onclick="HomePageButton_Click" /> 
          </td> 
         </tr> 
        </table> 
       </td> 
      </tr> 
     </table> 
    </SuccessTemplate> 
</asp:ChangePassword> 
</asp:Content> 

代碼隱藏是

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class ChangePassword : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    //TextBox password = (TextBox)ChangePassword1.FindControl("CurrentPassword"); 
    //Label passwordLabel = (Label)ChangePassword1.FindControl("CurrentPasswordLabel"); 
    TextBox password = ChangePassword1.FindControl("CurrentPassword") as TextBox; 
    Label passwordLabel = ChangePassword1.FindControl("CurrentPasswordLabel") as Label; 
    //TextBox CurrentPassword = ChangePassword1.FindControl("CurrentPassword") as TextBox; 
    if (Request.QueryString["p"] != null) 
    { 
     passwordLabel.Visible = false; 
     password.TextMode = TextBoxMode.SingleLine; 
     password.Visible = false; 
     password.Text = Request.QueryString["p"]; 
    } 
} 
protected void ChangePassword1_ChangedPassword(object sender, EventArgs e) 
{ 

} 
protected void HomePageButton_Click(object sender, EventArgs e) 
{ 
    //Link.ToHomePage(); 
    Response.Redirect("HomePage.aspx"); 
} 

} 

我有是我得到的不是設置爲on passwordLabel和密碼的對象實例的對象引用的問題。有誰知道如何將它們納入範圍,以便我可以在代碼隱藏中編輯文本和可見性屬性?

回答

4

嘗試使用:

TextBox password = ChangePassword1.Controls[0].FindControl("CurrentPassword") as TextBox; 
    Label passwordLabel = ChangePassword1.Controls[0].FindControl("CurrentPasswordLabel") as Label; 
+1

感謝@Adil馬馬多夫,它的工作完美。你能解釋一下爲什麼這種方法有效,而其他方法卻不行? – tuckerjt07 2012-07-11 15:50:24

+1

我很高興它的工作。我認爲這是由於您必須在ChangePassword1中更改模板:ChangePasswordTemplate和SuccessTemplate。由於您的TextBox和Label位於第一個模板中,因此您必須編寫Controls [0]。如果他們在SuccessTempate中,它將是Controls [1]。可能是我錯了,請嘗試在SuccessTemplate中獲得「HomePageButton」並編寫控件[1]。如果它的作品,這意味着我沒有錯:) – 2012-07-12 05:06:27

1

使用代替控制ChangePasswordTemplateContainer [0]不會受到變化的對照模板的順序。例如...在ChangePasswordTemplate之前添加SuccessTemplate將導致控件[0]代碼中斷。

ChangePassword1.ChangePasswordTemplateContainer.FindControl( 「CurrentPassword」)

相關問題