Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2013, 05:33 PM
Authorized User
Points: 207, Level: 4
Points: 207, Level: 4 Points: 207, Level: 4 Points: 207, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 40
Thanks: 8
Thanked 5 Times in 4 Posts
Default Dropdownlist in createuser wizard.

Hi Imar this has really driven me nutts, please help!!
I am trying to implement dropdownlist inside createuserwizard to get user choice and then programatically insert it in the database.

Code:
  <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server">
                <ContentTemplate>
                    <table>
                        <tr><td>
                                <asp:TextBox ID="UserName" CssClass="textboxStyle" runat="server" 
                                    ToolTip="User Name"></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>
                                <asp:TextBox ID="Password" CssClass="textboxStyle" runat="server" 
                                    TextMode="Password" ToolTip="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>
                                <asp:TextBox ID="ConfirmPassword" CssClass="textboxStyle" runat="server" 
                                    TextMode="Password" ToolTip="Confirm 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>
                                <asp:TextBox ID="Email" CssClass="textboxStyle" runat="server" 
                                    ToolTip="someone@example.com"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="EmailRequired" runat="server" 
                                    ControlToValidate="Email" ErrorMessage="E-mail is required." 
                                    ToolTip="E-mail is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                       </td></tr>
                       <tr><td>
                       <p>Please identify yourself in one of the following:</p>
                           <asp:DropDownList ID="DropDownList1" runat="server">
                               <asp:ListItem>Student</asp:ListItem>
                               <asp:ListItem>Principal</asp:ListItem>
                               <asp:ListItem>Vice Principal</asp:ListItem>
                               <asp:ListItem>Teacher</asp:ListItem>
                               <asp:ListItem>Clerkial Staff</asp:ListItem>
                               <asp:ListItem>Other Staff</asp:ListItem>
                           </asp:DropDownList>
                                <asp:RequiredFieldValidator ID="identityRequired" runat="server" 
                                    ControlToValidate="DropDownList1" ErrorMessage="Identification in one of the categories is required." 
                                    ToolTip="Identification is required." ValidationGroup="CreateUserWizard1">*</asp:RequiredFieldValidator>
                       
                       </td></tr> 



                        <tr>
                            <td align="center">
                                <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" 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="left" colspan="0">
                                <asp:Button ID="StepNextButton" CssClass="buttonStyle" runat="server" CommandName="MoveNext" 
                                    Text="Create User" ValidationGroup="CreateUserWizard1" />
                            </td>
                        </tr>
                    </table>
                </CustomNavigationTemplate>
            </asp:CreateUserWizardStep>

C#

Code:
protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
        TextBox Category = DropDownList1.SelectedValue;

        // Get the UserId of the just-added user
        MembershipUser newUser = Membership.GetUser(CreateUserWizard1.UserName);
        Guid newUserId = (Guid)newUser.ProviderUserKey;

        // Insert a new record into UserProfiles
        string connectionString =
                ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        string insertSql = "INSERT INTO UserDetails(UserId, FirstName, MiddleName, LastName, HomeTown, Category) VALUES(@UserId, @FirstName, @MiddleNAme, @LastName, @HomeTown, @Category)";

        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {
            myConnection.Open();
            SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
            myCommand.Parameters.AddWithValue("@UserId", newUserId);
            myCommand.Parameters.AddWithValue("@FirstName", DBNull.Value);
            myCommand.Parameters.AddWithValue("@MiddleName", DBNull.Value);
            myCommand.Parameters.AddWithValue("@LastName", DBNull.Value);
            myCommand.Parameters.AddWithValue("@HomeTown", DBNull.Value);
            myCommand.Parameters.AddWithValue("@Category", Category.Text.Trim());
            myCommand.ExecuteNonQuery();
            myConnection.Close();
        }

}
Error: DropList1 doesn't exist in this context.

Thanks.

Last edited by 07Raghav; June 11th, 2013 at 05:42 PM..
 
Old June 11th, 2013, 08:31 PM
Authorized User
Points: 207, Level: 4
Points: 207, Level: 4 Points: 207, Level: 4 Points: 207, Level: 4
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2012
Posts: 40
Thanks: 8
Thanked 5 Times in 4 Posts
Default

ok I seem to have finally found a solution but don't know about its validity.

Code:
DropDownList category = (DropDownList)CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("DropDownList1");
Please sir look into it and can i use it for further usage.

Thanks

Last edited by 07Raghav; June 11th, 2013 at 08:34 PM..
 
Old June 14th, 2013, 09:17 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

yes, this is the right approach to find a control in an editable control.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!




Similar Threads
Thread Thread Starter Forum Replies Last Post
CreateUser Wizard sophia BOOK: Beginning ASP.NET 4 : in C# and VB 25 April 23rd, 2012 04:10 AM
Provider error by CreateUser dbardelt BOOK: Silverlight 4 Problem - Design - Solution 4 June 6th, 2011 08:21 AM
UserInformationService.CreateUser p115 Gabor66 BOOK: Silverlight 4 Problem - Design - Solution 1 May 14th, 2010 03:50 AM
CreateUser Error Gadall ASP.NET 2.0 Professional 1 May 9th, 2007 01:15 AM
Saving personalization properties w/CreateUser k8277 ASP.NET 2.0 Basics 0 April 5th, 2007 01:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.