0

我正在使用带有 C# 代码的 ASP.NET Web 应用程序在 Microsoft Visual Studio 中开发一个项目。我添加了一个 CreateUserWizard 并在步骤 1 中自定义了表单。从事件处理程序中,我将 CreatedUser 处理程序放入代码隐藏中以更新数据库表。我想从表单中提取处理程序的数据值,但代码隐藏文件由于某种原因看不到它们。帮助!

主页

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="PageControls" runat="server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="RightBox" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="SiteControl1" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="SiteControl2" runat="server">
</asp:Content>
<asp:Content ID="Content6" ContentPlaceHolderID="SiteControl3" runat="server">
</asp:Content>
<asp:Content ID="Content7" ContentPlaceHolderID="CPMain" runat="server">
    <center>
        <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
CreateUserButtonText="Submit"
            RequireEmail="False" OnCreatedUser="CreateUserWizard1_CreatedUser">
            <WizardSteps>
                <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                    <ContentTemplate>
                        <table border="0" style="font-size: 100%">
                            <tr>
                                <td align="center" colspan="2" style="font-weight: 
bold; color: white; background-color: #5d7b9d">
                                    Register with Acme!
                                </td>
                            </tr>
                            <tr>
                                <td align="right">
                                    <asp:Label ID="Label1" runat="server" 
AssociatedControlID="UserName">
                        First Name:</asp:Label>
                                </td>
                                <td>
                                    <asp:TextBox ID="FName" runat="server">
</asp:TextBox>
                                    <asp:RequiredFieldValidator 
ID="RequiredFieldValidator1" runat="server" ControlToValidate="FirstName"
                                        ErrorMessage="First Name is required." 
ToolTip="First Name is required." ValidationGroup="CreateUserWizard1">*
</asp:RequiredFieldValidator>
                                </td>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="Label2" runat="server">
                        Last Name:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="LName" runat="server">
</asp:TextBox>
                                        <asp:RequiredFieldValidator 
ID="RequiredFieldValidator2" runat="server" ControlToValidate="LastName"
                                            ErrorMessage="Last Name is required." 
ToolTip="Last Name is required." ValidationGroup="CreateUserWizard1">*
</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="UserNameLabel" runat="server" 
AssociatedControlID="UserName">
                        User Name:</asp:Label>
                                    </td>
                                    <td>
                                        <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="CreateUserWizard1">*
</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="PasswordLabel" runat="server" 
AssociatedControlID="Password">
                        Password:</asp:Label>
                                    </td>
                                    <td>
                                        <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="CreateUserWizard1">*
</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="ConfirmPasswordLabel" 
runat="server" AssociatedControlID="ConfirmPassword">
                        Confirm Password:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:TextBox ID="ConfirmPassword" 
runat="server" TextMode="Password"></asp:TextBox>
                                        <asp:RequiredFieldValidator 
ID="ConfirmPasswordRequired" runat="server" ControlToValidate="ConfirmPassword"
                                            ErrorMessage="Confirm Password is 
required." ToolTip="Confirm Password is required."
                                            ValidationGroup="CreateUserWizard1">*
</asp:RequiredFieldValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2">
                                        <asp:CompareValidator ID="PasswordCompare" 
runat="server" ControlToCompare="Password"
                                            ControlToValidate="ConfirmPassword" 
Display="Dynamic" ErrorMessage="The Password and Confirmation Password must match."
                                            ValidationGroup="CreateUserWizard1">
</asp:CompareValidator>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center" colspan="2" style="color: red">
                                        <asp:Literal ID="ErrorMessage" runat="server" 
EnableViewState="False"></asp:Literal>
                                    </td>
                                </tr>
                                <tr>
                                    <td align="right">
                                        <asp:Label ID="Label3" runat="server" 
AssociatedControlID="UserName">
                        Role:</asp:Label>
                                    </td>
                                    <td>
                                        <asp:DropDownList ID="RoleSelector" 
runat="server">
                                            <asp:ListItem 
Value="Visitor">Visitor</asp:ListItem>
                                            <asp:ListItem 
Value="Employee">Employee</asp:ListItem>
                                            <asp:ListItem Value="PM">Project 
Manager</asp:ListItem>
                                            <asp:ListItem Value="DM">Department 
Manager</asp:ListItem>
                                            <asp:ListItem Value="Director">IT 
Director</asp:ListItem>
                                        </asp:DropDownList>
                                    </td>
                                </tr>
                        </table>
                    </ContentTemplate>
                </asp:CreateUserWizardStep>
                <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server">
                    <ContentTemplate>
                    </ContentTemplate>
                </asp:CompleteWizardStep>
            </WizardSteps>
        </asp:CreateUserWizard>
    </center>
</asp:Content>

代码隐藏:

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

namespace Acme
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
        {
            RolesManager.InsertEmployee(FName.Text, LName.Text, RoleSelector.Value, 
User.Identity);
        }
    }
}

我在编译时收到这些错误:

Error   1   The name 'FName' does not exist in the current context  C:\MV_Training\Jan2013\C#Projects\AcmePresentation\AcmePresentation\Register.aspx.cs    20  41  AcmePresentation
Error   2   The name 'LName' does not exist in the current context  C:\MV_Training\Jan2013\C#Projects\AcmePresentation\AcmePresentation\Register.aspx.cs    20  53  AcmePresentation
Error   3   The name 'RoleSelector' does not exist in the current context   C:\MV_Training\Jan2013\C#Projects\AcmePresentation\AcmePresentation\Register.aspx.cs    20  65  AcmePresentation

这来自浏览器:

编译错误描述:在编译服务此请求所需的资源期间发生错误。请查看以下特定错误详细信息并适当修改您的源代码。

编译器错误消息:CS1061:“ASP.register_aspx”不包含“CreateUserWizard1_CreatedUser”的定义,并且找不到接受“ASP.register_aspx”类型的第一个参数的扩展方法“CreateUserWizard1_CreatedUser”(您是否缺少 using 指令或装配参考?)

源错误:

Line 16: <asp:Content ID="Content7" ContentPlaceHolderID="CPMain" runat="server">
Line 17:     <center>
Line 18:         <asp:CreateUserWizard ID="CreateUserWizard1" runat="server" 
Line 19:             CreateUserButtonText="Submit" RequireEmail="False" 
Line 20:             oncreateduser="CreateUserWizard1_CreatedUser">

源文件:c:\MV_Training\Jan2013\C#Projects\AcmePresentation\AcmePresentation\Register.aspx 行:18

谢谢,比尔

4

1 回答 1

0

你想明确地投射控制是这样的 -

public void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
   var fNameTextBox = (TextBox)CreateUserWizardStep1
          .ContentTemplateContainer.FindControl("FName");
   var lNameTextBox = (TextBox)CreateUserWizardStep1
          .ContentTemplateContainer.FindControl("LName");
   var usernameTextBox = (TextBox)CreateUserWizardStep1
          .ContentTemplateContainer.FindControl("UserName");

   MembershipUser user = Membership.GetUser(usernameTextBox.Text);
   Guid userId = (Guid)user.ProviderUserKey;
}
于 2013-02-20T17:04:50.710 回答