在asp:CreateUserWizard中,DropDownList不会保存到db?

时间:2010-07-12 19:16:12

标签: asp.net vb.net drop-down-menu createuserwizard

在向导的第2步中,我有一个从db填充的下拉列表。当我进入第3步RegisterUser.ActiveStepChanged事件,如果被解雇。在那里我将数据保存到数据库。我的文本框正在保存,但我的vb代码没有从下拉列表中提取selectedIndex / Value。 我尝试填充Load,PreRender中的下拉列表,但两者都没有帮助。我也做过,如果不是Postback也没有帮助。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

以下是代码示例: 感谢。

Asp.net - 它只包含相关代码:

<%@ Page Title="Register" Language="vb" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeBehind="Register.aspx.vb" Inherits="Events.Register" EnableEventValidation="false" Trace="true" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>


<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript" src="../Scripts/CommonScripts.js">

</script>

<style type="text/css">
  .textInput {
     width: 150px;
  }
  .dropdownField 
  {
      width: 155px;

  }
</style>



</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
    </asp:ToolkitScriptManager> 
    <asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="False" 
        Height="68px" Width="70%" >
        <FinishNavigationTemplate>

            <table cellpadding="5px" width="100%"> 
                <tr>
             <td align="center" width="50%"> 
                 <asp:Button ID="FinishButton" runat="server" CommandName="MoveComplete" 
                Text="Finish" onclick="RegisterUser_ActiveStepChanged" /></td>
            </tr>
            </table>


        </FinishNavigationTemplate>
        <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>
                    <table style="font-size:100%;height:100%;width:100%;">
                        <tr>
                            <td align="center" colspan="2">
                                Sign Up for Your New Account</td>
                        </tr>
                        <tr>
                            <td align="right" style="width:40%">
                                <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="RegisterUser">*</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="RegisterUser">*</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="RegisterUser">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="EmailLabel" runat="server" AssociatedControlID="Email">E-mail:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Email" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                    ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                    ToolTip="E-mail is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Security Question:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Question" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="QuestionRequired" runat="server" 
                                    ControlToValidate="Question" ErrorMessage="Security question is required." 
                                    ToolTip="Security question is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                <asp:Label ID="AnswerLabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label>
                            </td>
                            <td>
                                <asp:TextBox ID="Answer" runat="server"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" 
                                    ControlToValidate="Answer" ErrorMessage="Security answer is required." 
                                    ToolTip="Security answer is required." ValidationGroup="RegisterUser">*</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="RegisterUser"></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>
                    </table>
                </ContentTemplate>
                <CustomNavigationTemplate>
                    <table border="0" cellspacing="5" style="width:100%;height:100%;">
                        <tr align="right">
                            <td align="center" colspan="0">
                                <asp:Button ID="StepNextButton" runat="server" CommandName="MoveNext" 
                                    Text="Create User" ValidationGroup="RegisterUser" />
                            </td>
                        </tr>
                    </table>
                </CustomNavigationTemplate>
            </asp:CreateUserWizardStep>
            <asp:WizardStep runat="server" Title="General Information" StepType="Finish" 
                ID="GeneralInfo">

                <table style="font-size:100%; width:100%; height:100%">
                        <tr>
                            <td align="center" colspan="2">
                                Please Add Additional Account Information</td>
                        </tr>

                            <td align="right">
                                Birthday:
                            </td>
                            <td>
                                <asp:TextBox class="textInput" ID="Birthday" runat="server"></asp:TextBox> 
                                <asp:ImageButton ID="BirthdayButton" runat="server"  ImageUrl="~/Images/Calendar.png"  Height="24" Width="24" ImageAlign="Baseline" />

                                <asp:CalendarExtender TargetControlID="Birthday" ID="BirthdayExtender" runat="server" PopupButtonID="BirthdayButton" />
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                                    ControlToValidate="Birthday" ErrorMessage="Birthday is required." 
                                    ToolTip="Birthday is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                                Religious Observance:
                            </td>
                            <td>
                                <asp:DropDownList class="dropdownField"  ID="ReligiousObservance" runat="server" 
                                    DataSourceID="HashkafaDataSource" DataTextField="chvDescription" 
                                    DataValueField="intReligiousMasterID" AppendDataBoundItems="true" >
                                        <asp:ListItem Value="0">Select One</asp:ListItem>
                                    </asp:DropDownList>
                                <asp:SqlDataSource ID="HashkafaDataSource" runat="server" 
                                    ConnectionString="<%$ ConnectionStrings:EventsDB.06-ConnectionString %>"
                                     SelectCommand="select s.chvSiteName, r.intReligiousMasterID, r.chvDescription from 
                                                tblSiteMaster as s inner join tblSiteReligious as sr on s.intSiteID=sr.intSiteID
                                                inner join tblReligiousMaster as r on r.intReligiousMasterID=sr.intReligiousMasterID
                                                where s.intSiteID=cast(@SiteID As int)">
                                    <SelectParameters>
                                        <asp:Parameter Name="SiteID"/>
                                    </SelectParameters>
                                </asp:SqlDataSource>

                                <asp:RequiredFieldValidator ID="ReligiousObservanceRequired" runat="server" 
                                    ControlToValidate="ReligiousObservance" ErrorMessage="Religious Observance is required." 
                                    ToolTip="Religious Observance is required." ValidationGroup="RegisterUser">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                       <tr>
                            <td align="center" colspan="2" style="color:Red;">
                                <asp:Literal ID="ErrorMessage" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                    </table>

             <%--  <CustomNavigationTemplate/> --%>
            </asp:WizardStep>
            <asp:CompleteWizardStep runat="server">
                <ContentTemplate>
                    <table style="font-size:100%;height:68px;width:70%;">

                        <tr>
                            <td>
                                Your account has been successfully created.</td>
                        </tr>
                        <tr>
                            <td align="center" colspan="2">
                                <asp:Button ID="ContinueButton" runat="server" CausesValidation="False" 
                                    CommandName="Continue" Text="Continue" ValidationGroup="RegisterUser" />
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:CompleteWizardStep>
        </WizardSteps>
        <StartNavigationTemplate>
            hello<br />
        </StartNavigationTemplate>
        <StepNavigationTemplate>
            <table cellpadding="5px" width="100%">
                <tr>
             <td align="right"><asp:Button ID="StepPreviousButton" runat="server" CausesValidation="False" 
                CommandName="MovePrevious" Text="Previous" /> </td>
            <td align="left"> <asp:Button ID="StepNextButton" runat="server" CommandName="MoveNext" 
                Text="Next" Height="20" Width="18" /> </td> </tr>
            </table>
        </StepNavigationTemplate>
    </asp:CreateUserWizard>
</asp:Content>

VB.net代码

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'If (Not IsPostBack) Then
    '    PopulateReligiousDropdown()
    'End If

    'Scripts for state and country drop down
    ' Commented out because doing in ASP.Net
    '     Page.ClientScript.RegisterStartupScript(Me.GetType(), _
    '   "MyScript", _
    '       "window.onload=init();" & _
    ' "function init() { " & _
    '     "fillList(document.getElementById('MainContent_RegisterUser_dropCountry'), areas[5]);" & _
    '     "fillList(document.getElementById('MainContent_RegisterUser_dropState'), areas[0]);" & _
    '" }", True)


    PhotoUploadValidator.ValidationExpression = HelperMethods.regexPhotos.ToString()




    RegisterUser.ContinueDestinationPageUrl = Request.QueryString("ReturnUrl")

    ' Populate ReligiousDropdown
    HelperMethods.SiteID = HelperMethods.GetSiteID("Request.Url.Host")
    HashkafaDataSource.SelectParameters.Item(0).DefaultValue = HelperMethods.SiteID


End Sub


 Protected Sub RegisterUser_ActiveStepChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RegisterUser.ActiveStepChanged
    ' Have we JUST reached the Complete step?
    If RegisterUser.ActiveStep.Title = "General Information" Then
        'PopulateReligiousDropdown()
    End If

    If RegisterUser.ActiveStep.Title = "Complete" Then
        Dim GeneralInfo As WizardStep = CType(RegisterUser.FindControl("GeneralInfo"), WizardStep)

        ' Programmatically reference the TextBox controls
        Dim LastName As TextBox = CType(GeneralInfo.FindControl("LastName"), TextBox)
        Dim FirstName As TextBox = CType(GeneralInfo.FindControl("FirstName"), TextBox)
        Dim dropCountry As DropDownList = CType(GeneralInfo.FindControl("dropCountry"), DropDownList)
        Dim dropState As DropDownList = CType(GeneralInfo.FindControl("dropState"), DropDownList)
        Dim City As TextBox = CType(GeneralInfo.FindControl("City"), TextBox)
        Dim ZipCode As TextBox = CType(GeneralInfo.FindControl("ZipCode"), TextBox)
        Dim Address As TextBox = CType(GeneralInfo.FindControl("Address"), TextBox)
        Dim Phone As TextBox = CType(GeneralInfo.FindControl("Phone"), TextBox)
        Dim Birthday As TextBox = CType(GeneralInfo.FindControl("Birthday"), TextBox)
        Dim ReligiousObservance As DropDownList = CType(GeneralInfo.FindControl("ReligiousObservance"), DropDownList)
        ' Dim PhotoUpload As TextBox = CType(GeneralInfo.FindControl("PhotoUpload"), TextBox)


        ' Update the UserProfiles record for this user
        ' Get the UserId of the just-added user
        Dim newUser As MembershipUser = Membership.GetUser
        Dim newUserId As Guid = CType(newUser.ProviderUserKey, Guid)

        ' Insert a new record into UserProfiles
        Dim connectionString As String = ConfigurationManager.ConnectionStrings("EventsDB.06-ConnectionString").ConnectionString
        Dim updateSql As String = "Insert into tblUsers " & _
            "Values (@UserId, GETDATE(), @FirstName, @LastName, @dropCountry, @dropState, @City, @ZipCode, @Address, @Phone, @Birthday, @ReligiousObservance)"

        Using myConnection As New SqlConnection(connectionString)
            myConnection.Open()

            Dim myCommand As New SqlCommand(updateSql, myConnection)
            myCommand.Parameters.AddWithValue("@UserId", newUserId)
            myCommand.Parameters.AddWithValue("@LastName", LastName.Text.Trim())
            myCommand.Parameters.AddWithValue("@FirstName", FirstName.Text.Trim())
            myCommand.Parameters.AddWithValue("@dropCountry", dropCountry.SelectedValue)
            myCommand.Parameters.AddWithValue("@dropState", dropState.SelectedValue)
            myCommand.Parameters.AddWithValue("@City", City.Text.Trim())
            myCommand.Parameters.AddWithValue("@ZipCode", ZipCode.Text.Trim())
            myCommand.Parameters.AddWithValue("@Address", Address.Text.Trim())
            myCommand.Parameters.AddWithValue("@Phone", Phone.Text.Trim())
            myCommand.Parameters.AddWithValue("@Birthday", Birthday.Text.Trim())
            myCommand.Parameters.AddWithValue("@ReligiousObservance", ReligiousObservance.SelectedIndex)

            'The dropdown's are not populating the server. The Value isn't being passed 
            ' and I don't know why?!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


            myCommand.ExecuteNonQuery()

            myConnection.Close()
        End Using
    End If
End Sub