2016-10-13 12 views
1

HiddenField1私はC#コードを実行すると存在しないようです。Runat = "server"は現在のコンテキストに存在しません

エラーが読み込み: 'HiddenField1'

名を現在のコンテキストに存在しません。

ライン54

でHiddenField1 C#コードは、サイト上でそのように見える出てくる理由は申し訳ありませんが、私は知りません。

public partial class Login : System.Web.UI.Page 
{ 
    protected void LoginButton_Click1(object sender, EventArgs e) 
    { 
     string userName1 = Login1.UserName; 
     string userPassword1 = Login1.Password; 
     bool logSuccess = false; 

     foreach (GridViewRow gvRow in GridView1.Rows) 
     { 

      if (userName1 == gvRow.Cells[1].Text && userPassword1 == gvRow.Cells[2].Text) 
      { 
       Response.Write("Logged in"); 
       logSuccess = true; 
       break; 
      } 
     } 
     if (logSuccess == true) 
     { Response.Redirect("~/MemberPages/Members.aspx"); } 
     else 
     { HiddenField1.Value = ""; } 
    } 
} 

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %> 
 

 
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server"> 
 
    <style type="text/css"> 
 
     .auto-style4 { 
 
      width: 158px; 
 
     } 
 
     .auto-style5 { 
 
      height: 28px; 
 
     } 
 
     .auto-style6 { 
 
      width: 158px; 
 
      height: 28px; 
 
     } 
 
    </style> 
 
</asp:Content> 
 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
 
    <h2>Login</h2> 
 
    <p> 
 
     <asp:Login ID="Login1" runat="server" Height="135px" Width="252px" OnAuthenticate="Login1_Authenticate"> 
 
      <LayoutTemplate> 
 
       <table cellpadding="1" cellspacing="0" style="border-collapse:collapse;"> 
 
        <tr> 
 
         <td> 
 
          <table cellpadding="0" style="height:135px;width:252px;"> 
 
           <tr> 
 
            <td align="center" colspan="2">Log In</td> 
 
           </tr> 
 
           <tr> 
 
            <td align="right" class="auto-style5"> 
 
             <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Email:</asp:Label> 
 
            </td> 
 
            <td class="auto-style6"> 
 
             <asp:TextBox ID="UserName" runat="server"></asp:TextBox> 
 
             <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator> 
 
            </td> 
 
           </tr> 
 
           <tr> 
 
            <td align="right"> 
 
             <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label> 
 
            </td> 
 
            <td class="auto-style4"> 
 
             <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox> 
 
             <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator> 
 
            </td> 
 
           </tr> 
 
           <tr> 
 
            <td colspan="2"> 
 
             <asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." /> 
 
            </td> 
 
           </tr> 
 
           <tr> 
 
            <td align="center" colspan="2" style="color:Red" > 
 
             <asp:HiddenField ID="HiddenField1" Value="Invalid user email or password." runat="server" /> 
 
             </td> 
 
           </tr> 
 
           <tr> 
 
            <td align="right" colspan="2"> 
 
             <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" OnClick="LoginButton_Click1" /> 
 
            </td> 
 
           </tr> 
 
          </table> 
 
         </td> 
 
        </tr> 
 
       </table> 
 
      </LayoutTemplate> 
 
     </asp:Login> 
 
</p> 
 
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" 
 
     Group="Login1" /> 
 

 
<p> 
 
    &nbsp;</p> 
 
</asp:Content>

答えて

5

あなたHiddenFieldコントロールは、あなたがそれを見つけるためにFindControlメソッドを使用する必要がLayoutTemplate内部にあるため、次の問題を修正し

((HiddenField)Login1.FindControl("HiddenField1")).Value = ""; 
+0

Yeap。ちょうど私のコードは私がそれがすると思ったものではありません。 –

+0

HiddenFieldにテキストを表示させることはできますか?それは価値の中にあるでしょうか? –

+0

私はそれを単なるテキストに変更し、コードと連携しました。どうもありがとう! –

関連する問題