本文介绍了文本框不在当前上下文中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code问题

2个文件:
Register.aspx
Register.aspx.cs

Register.aspx:

 <%@页标题=注册LANGUAGE =C#的MasterPageFile =〜/ Site.master母AutoEventWireup =真
    codeBehind =Register.aspx.cs继承=HSE_Monitoring_Application.Account.Register%GT;< ASP:内容ID =HeaderContent=服务器ContentPlaceHolderID =HeadContent>
< / ASP:内容>
< ASP:内容ID =的BodyContent=服务器ContentPlaceHolderID =日程地址搜索Maincontent>
    < ASP:CreateUserWizard控件ID =RegisterUser=服务器的EnableViewState =假OnCreatedUser =RegisterUser_CreatedUser>
        <&LayoutTemplate模板GT;
            < ASP:占位符ID =wizardStepPlaceholder=服务器>< / ASP:占位符>
            < ASP:占位符ID =navigationPlaceholder=服务器>< / ASP:占位符>
        < / LayoutTemplate模板>
        < WizardSteps>
            < ASP:CreateUserWizardStep ID =RegisterUserWizardStep=服务器>
                <&的ContentTemplate GT;
                    < H2>
                        创建一个新账户
                    < / H>
                    &所述p为H.;
                        使用下面的表单创建一个新帐户。
                    &所述; / P>
                    &所述p为H.;
                        密码被要求是一个最小&所述的;%= Membership.MinRequiredPasswordLength%GT;字符长。
                    &所述; / P>
                    <跨度类=failureNotification>
                        < ASP:文字ID =的ErrorMessage=服务器>< / ASP:文字>
                    < / SPAN>
                    < ASP:的ValidationSummary ID =RegisterUserValidationSummary=服务器的CssClass =failureNotification
                         的ValidationGroup =RegisterUserValidationGroup/>
                    < D​​IV CLASS =accountInfo>
                        <字段集类=注册>
                            <传奇>帐户信息与LT; /传说>
                            &所述p为H.;
                                < ASP:标签ID =UserNameLabel=服务器AssociatedControlID =用户名>用户名:​​LT; / ASP:标签>
                                < ASP:文本框ID =用户名=服务器的CssClass =TEXTENTRY>< / ASP:文本框>
                                < ASP:的RequiredFieldValidator ID =UserNameRequired=服务器的ControlToValidate =用户名
                                     的CssClass =failureNotification的ErrorMessage =用户名是必需的。工具提示=用户名是必需的。
                                     的ValidationGroup =RegisterUserValidationGroup> * LT; / ASP:&的RequiredFieldValidator GT;
                            &所述; / P>
                            &所述p为H.;
                                < ASP:标签ID =PasswordLabel=服务器AssociatedControlID =密码>密码:LT; / ASP:标签>
                                < ASP:文本框ID =密码=服务器的CssClass =passwordEntry的TextMode =密码>< / ASP:文本框>
                                < ASP:的RequiredFieldValidator ID =PasswordRequired=服务器的ControlToValidate =密码
                                     的CssClass =failureNotification的ErrorMessage =密码是必需的。工具提示=密码是必需的。
                                     的ValidationGroup =RegisterUserValidationGroup> * LT; / ASP:&的RequiredFieldValidator GT;
                            &所述; / P>
                            &所述p为H.;
                                < ASP:标签ID =ConfirmPasswordLabel=服务器AssociatedControlID =ConfirmPassword>确认密码:LT; / ASP:标签>
                                < ASP:文本框ID =ConfirmPassword=服务器的CssClass =passwordEntry的TextMode =密码>< / ASP:文本框>
                                < ASP:的RequiredFieldValidator的ControlToValidate =ConfirmPassword的CssClass =failureNotification显示=动态
                                     的ErrorMessage =确认密码是必需的。 ID =ConfirmPasswordRequired=服务器
                                     工具提示=确认密码是必需的。的ValidationGroup =RegisterUserValidationGroup> * LT; / ASP:&的RequiredFieldValidator GT;
                                < ASP:CompareValidator ID =PasswordCompare=服务器ControlToCompare =密码的ControlToValidate =ConfirmPassword
                                     的CssClass =failureNotification显示=动态的ErrorMessage =密码与密码确认密码必须匹配。
                                     的ValidationGroup =RegisterUserValidationGroup> * LT; / ASP:CompareValidator>
                            &所述; / P>
                        < /字段集>
                        < p =类提交按钮>
                            < ASP:按钮的ID =CreateUserButton=服务器的CommandName =MoveNext的文本=创建用户
                                 的ValidationGroup =RegisterUserValidationGroup的OnClick =onSubmitNewUser/>
                        &所述; / P>
                    < / DIV>
                < /&的ContentTemplate GT;
                < CustomNavigationTemplate>
                < / CustomNavigationTemplate>
            < / ASP:CreateUserWizardStep>
        < / WizardSteps>
    < / ASP:&的CreateUserWizard GT;
< / ASP:内容>

问题:
看不到文本框用户名从Register.aspx.cs类:

 无效onSubmitNewUser(对象发件人,EventArgs的发送)
        {
            字符串STRCON =connect_information;
            SqlConnection的CON =新的SqlConnection(STRCON);            COM的SqlCommand =新的SqlCommand(storeloginCON);
            com.CommandType = CommandType.StoredProcedure;
            的SqlParameter P1 =新的SqlParameter(用户名,UserName.Text);
            的SqlParameter P3 =新的SqlParameter(密码,ConfirmPassword.Text);
            com.Parameters.Add(P1);
            com.Parameters.Add(P3);
            con.Open();
            com.ExecuteNonQuery();
            Labelinfo.Text =注册成功;
        }


解决方案

它也有可能,你会得到一个很难得到的ConfirmPassword文本框中contents..Because你的控件被掩埋另一个容器里面,你已经得到回报后,一些小excavation..Lets开始挖......

使用向导这已经是接近找到您的文本框

 文本框的UserName =(文本框)CreateUserWizard.FindControl(用户名);

//然后你want.Hope这有助于使用它。

problem in code

2 files:Register.aspxRegister.aspx.cs

Register.aspx:

    <%@ Page Title="Register" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Register.aspx.cs" Inherits="HSE_Monitoring_Application.Account.Register" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" OnCreatedUser="RegisterUser_CreatedUser">
        <LayoutTemplate>
            <asp:PlaceHolder ID="wizardStepPlaceholder" runat="server"></asp:PlaceHolder>
            <asp:PlaceHolder ID="navigationPlaceholder" runat="server"></asp:PlaceHolder>
        </LayoutTemplate>
        <WizardSteps>
            <asp:CreateUserWizardStep ID="RegisterUserWizardStep" runat="server">
                <ContentTemplate>
                    <h2>
                        Create a New Account
                    </h2>
                    <p>
                        Use the form below to create a new account.
                    </p>
                    <p>
                        Passwords are required to be a minimum of <%= Membership.MinRequiredPasswordLength %> characters in length.
                    </p>
                    <span class="failureNotification">
                        <asp:Literal ID="ErrorMessage" runat="server"></asp:Literal>
                    </span>
                    <asp:ValidationSummary ID="RegisterUserValidationSummary" runat="server" CssClass="failureNotification" 
                         ValidationGroup="RegisterUserValidationGroup"/>
                    <div class="accountInfo">
                        <fieldset class="register">
                            <legend>Account Information</legend>
                            <p>
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
                                <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                                     CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                                <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                                     CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                            </p>
                            <p>
                                <asp:Label ID="ConfirmPasswordLabel" runat="server" AssociatedControlID="ConfirmPassword">Confirm Password:</asp:Label>
                                <asp:TextBox ID="ConfirmPassword" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ControlToValidate="ConfirmPassword" CssClass="failureNotification" Display="Dynamic" 
                                     ErrorMessage="Confirm Password is required." ID="ConfirmPasswordRequired" runat="server" 
                                     ToolTip="Confirm Password is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
                                <asp:CompareValidator ID="PasswordCompare" runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword" 
                                     CssClass="failureNotification" Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                                     ValidationGroup="RegisterUserValidationGroup">*</asp:CompareValidator>
                            </p>
                        </fieldset>
                        <p class="submitButton">
                            <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" 
                                 ValidationGroup="RegisterUserValidationGroup" OnClick="onSubmitNewUser"/>
                        </p>
                    </div>
                </ContentTemplate>
                <CustomNavigationTemplate>
                </CustomNavigationTemplate>
            </asp:CreateUserWizardStep>
        </WizardSteps>
    </asp:CreateUserWizard>
</asp:Content>

Problem:cannot see Textbox 'UserName' from Register.aspx.cs class:

void onSubmitNewUser(Object sender, EventArgs e)
        {
            string strcon = "connect_information";
            SqlConnection con = new SqlConnection(strcon);

            SqlCommand com = new SqlCommand("storelogin", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlParameter p1 = new SqlParameter("username",  UserName.Text);
            SqlParameter p3 = new SqlParameter("password", ConfirmPassword.Text);
            com.Parameters.Add(p1);
            com.Parameters.Add(p3);
            con.Open();
            com.ExecuteNonQuery();
            Labelinfo.Text = "registered successful";
        }
解决方案

It's also likely that you will get a hard time getting the ConfirmPassword text box contents..Because your controls are buried inside another container, you have to be rewarded after some little excavation..Lets start digging......

Using the wizard which is already accessible locate your text box

    TextBox  UserName= (TextBox)CreateUserWizard.FindControl("UserName");

//Then use it as you want.Hope this helps.

这篇关于文本框不在当前上下文中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:55