I have a dropdownlist to select a security question within my CreateUserWizard. Here is the markup;
<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false"
CancelDestinationPageUrl="RegistrationFailed.aspx" DisableCreatedUser="true"
ContinueDestinationPageUrl="Login.aspx" LoginCreatedUser="false">
<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>
<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>
"all other fields removed for brevity
<p>
<asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Select Security Question:</asp:Label>
<asp:DropDownList ID="Question" runat="server"
OnSelectedIndexChanged="ddl_SelectedIndexChanged" >
</asp:DropDownList><br />
<i style="font-size: .7em;">If you forget your password you will be asked the security question you choose here and prompted to enter the answer you specify below.</i>
<asp:RequiredFieldValidator ID="rfvSecurityQuestion" runat="server" ControlToValidate="Question"
CssClass="failureNotification" ErrorMessage="« [Required]" ToolTip="A security question is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="Answerlabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label>
<asp:TextBox ID="Answer" runat="server" CssClass="textEntry"></asp:TextBox>
<asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer"
CssClass="failureNotification" ErrorMessage="« [Required]" ToolTip="Security answer is required."
ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator>
</p>
</fieldset>
<p class="submitButton">
<asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User"
ValidationGroup="RegisterUserValidationGroup" CssClass="buttons" />
</p>
</div>
</ContentTemplate>
<CustomNavigationTemplate>
</CustomNavigationTemplate>
</asp:CreateUserWizardStep>
</WizardSteps>
</asp:CreateUserWizard>
I am able to find the control and populate the questions from a database in Pre_load as follows;
Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad
Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList)
Dim itemlist As New List(Of String)
itemlist = GetSecurityQuestionsforDDL()
If ddl IsNot Nothing Then
For Each item As String In itemlist
ddl.Items.Add(New ListItem(item))
Next
ddl.DataBind()
End If
End Sub
I add an event handler in Page_Load so I know the control is already there even though it's already in the markup;
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
RegisterUser.ContinueDestinationPageUrl = Page.ResolveUrl("~/Default.aspx")
Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList)
AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged
End Sub
And I create the user in the CreatedUser event as follows;
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
dim question as string = ddl.SelectedValue
Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList)
Dim member As MembershipUser = Membership.CreateUser(RegisterUser.UserName, RegisterUser.Password, RegisterUser.Email, question, RegisterUser.Answer.ToLower(), False, MembershipCreateStatus.Success)
Dim newUser As MembershipUser = Membership.GetUser(RegisterUser.UserName)
'Other code
End Sub
The problem is that the question saved to the membership data store is ALWAYS the same question. Somehow the dropdown selection is not getting the new value. This is evidenced by the fact that nothing fires in my dropdown selectedindexchanged event.
Why are ddl.SelectedItem,Value and Index always giving me the same value regardless of what's selected in the dropdown control?
I set autopostback to true in the aspx markup but that did a) not trigger the event or b) provide the correct selection from the dropdown. In any event, i don't want a postback to occur from the dropdown selection changing
You don't want post back on dropdown selection then please remove OnSelectedIndexChanged="ddl_SelectedIndexChanged" from dropdown markup.
Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad
If(!Page.IsPostBack)
Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList)
Dim itemlist As New List(Of String)
itemlist = GetSecurityQuestionsforDDL()
If ddl IsNot Nothing Then
For Each item As String In itemlist
ddl.Items.Add(New ListItem(item))
Next
ddl.DataBind()
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
RegisterUser.ContinueDestinationPageUrl = Page.ResolveUrl("~/Default.aspx")
End Sub
sameer.khanjit@gmail.com
View Blog Click "Mark as Answer" on the post that helped you.
This will generate a postback if autopostback is set to tue, and will return an unpopulated dropdown. And the dropdown selectedindexchange event never fires anyway but I have removed the annotation in the markup. But there should never be a postback as the
createuser should generate the membership datastore information on click of createuser button and send you to the continueurl. The problem lies in that the selected value from the dropdown is ALWAYS the same value, index 0
Please replace your page pre load with below code will solve your problem :-
ProtectedSubPage_PreLoad(sender AsObject, e AsEventArgs)HandlesMe.PreLoad If(!Page.IsPostBack)
Dim ddl AsDropDownList=DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"),DropDownList) Dim itemlist AsNewList(OfString) itemlist =GetSecurityQuestionsforDDL() If ddl IsNotNothingThen ForEach item AsStringIn itemlist ddl.Items.Add(NewListItem(item)) Next ddl.DataBind() EndIf EndIf
EndSub
sameer.khanjit@gmail.com
View Blog Click "Mark as Answer" on the post that helped you.
I did change that code to the following, adding in the first listitem to select a question.
Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad
If Not Page.IsPostBack Then
Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList)
ddl.Items.Add(New ListItem("Please select a security question"))
Dim itemlist As New List(Of String)
itemlist = GetSecurityQuestionsforDDL()
If ddl IsNot Nothing Then
For Each item As String In itemlist
ddl.Items.Add(New ListItem(item))
Next
ddl.DataBind()
End If
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
RegisterUser.ContinueDestinationPageUrl = Page.ResolveUrl("~/Default.aspx")
End Sub
I now get a question required error when using the form. The problem still lies in that when a selection is made from the question dropdown, the code is NOT recognizing it. It absolutely has something to do with the fact that this dropdown lies within another
control as I placed another dropdown outside the control and get its value easily.
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
' this should be after Dim ddl As DropDownList statement;
Dim question as string = ddl.SelectedValue
Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList)
Dim member As MembershipUser = Membership.CreateUser(RegisterUser.UserName, RegisterUser.Password, RegisterUser.Email, question, RegisterUser.Answer.ToLower(), False, MembershipCreateStatus.Success)
Dim newUser As MembershipUser = Membership.GetUser(RegisterUser.UserName)
Dim question as string = ddl.SelectedValue
'Other code
End Sub
sameer.khanjit@gmail.com
View Blog Click "Mark as Answer" on the post that helped you.
Unfortunately I have already tried all these things. I am going to approach this from a different perspective as I ve wasted too much time implementing it this way which should've been strightforward but apparently isn't. Thank you for your input.
I add an event handler in Page_Load so I know the control is already there even though it's already in the markup;
I dont think this is acctually required. Instead just use a simple ddl and use ddl.SelectedItem.Text to get the questions selected. That should solve the problem.
dinotom
Member
24 Points
69 Posts
retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 09:45 AM|LINK
I have a dropdownlist to select a security question within my CreateUserWizard. Here is the markup;
<asp:CreateUserWizard ID="RegisterUser" runat="server" EnableViewState="false" CancelDestinationPageUrl="RegistrationFailed.aspx" DisableCreatedUser="true" ContinueDestinationPageUrl="Login.aspx" LoginCreatedUser="false"> <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> <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> "all other fields removed for brevity <p> <asp:Label ID="QuestionLabel" runat="server" AssociatedControlID="Question">Select Security Question:</asp:Label> <asp:DropDownList ID="Question" runat="server" OnSelectedIndexChanged="ddl_SelectedIndexChanged" > </asp:DropDownList><br /> <i style="font-size: .7em;">If you forget your password you will be asked the security question you choose here and prompted to enter the answer you specify below.</i> <asp:RequiredFieldValidator ID="rfvSecurityQuestion" runat="server" ControlToValidate="Question" CssClass="failureNotification" ErrorMessage="« [Required]" ToolTip="A security question is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> </p> <p> <asp:Label ID="Answerlabel" runat="server" AssociatedControlID="Answer">Security Answer:</asp:Label> <asp:TextBox ID="Answer" runat="server" CssClass="textEntry"></asp:TextBox> <asp:RequiredFieldValidator ID="AnswerRequired" runat="server" ControlToValidate="Answer" CssClass="failureNotification" ErrorMessage="« [Required]" ToolTip="Security answer is required." ValidationGroup="RegisterUserValidationGroup">*</asp:RequiredFieldValidator> </p> </fieldset> <p class="submitButton"> <asp:Button ID="CreateUserButton" runat="server" CommandName="MoveNext" Text="Create User" ValidationGroup="RegisterUserValidationGroup" CssClass="buttons" /> </p> </div> </ContentTemplate> <CustomNavigationTemplate> </CustomNavigationTemplate> </asp:CreateUserWizardStep> </WizardSteps> </asp:CreateUserWizard>I am able to find the control and populate the questions from a database in Pre_load as follows;
Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList) Dim itemlist As New List(Of String) itemlist = GetSecurityQuestionsforDDL() If ddl IsNot Nothing Then For Each item As String In itemlist ddl.Items.Add(New ListItem(item)) Next ddl.DataBind() End If End SubI add an event handler in Page_Load so I know the control is already there even though it's already in the markup;
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load RegisterUser.ContinueDestinationPageUrl = Page.ResolveUrl("~/Default.aspx") Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList) AddHandler ddl.SelectedIndexChanged, AddressOf ddl_SelectedIndexChanged End SubAnd I create the user in the CreatedUser event as follows;
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser dim question as string = ddl.SelectedValue Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList) Dim member As MembershipUser = Membership.CreateUser(RegisterUser.UserName, RegisterUser.Password, RegisterUser.Email, question, RegisterUser.Answer.ToLower(), False, MembershipCreateStatus.Success) Dim newUser As MembershipUser = Membership.GetUser(RegisterUser.UserName) 'Other code End SubThe problem is that the question saved to the membership data store is ALWAYS the same question. Somehow the dropdown selection is not getting the new value. This is evidenced by the fact that nothing fires in my dropdown selectedindexchanged event.
Why are ddl.SelectedItem,Value and Index always giving me the same value regardless of what's selected in the dropdown control?
gnosischief
Participant
1022 Points
222 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 09:51 AM|LINK
please try with
property.
dinotom
Member
24 Points
69 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 09:59 AM|LINK
I set autopostback to true in the aspx markup but that did a) not trigger the event or b) provide the correct selection from the dropdown. In any event, i don't want a postback to occur from the dropdown selection changing
sameer_khanj...
Star
7504 Points
1466 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 10:18 AM|LINK
You don't want post back on dropdown selection then please remove OnSelectedIndexChanged="ddl_SelectedIndexChanged" from dropdown markup.
Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad If(!Page.IsPostBack) Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList) Dim itemlist As New List(Of String) itemlist = GetSecurityQuestionsforDDL() If ddl IsNot Nothing Then For Each item As String In itemlist ddl.Items.Add(New ListItem(item)) Next ddl.DataBind() End If End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load RegisterUser.ContinueDestinationPageUrl = Page.ResolveUrl("~/Default.aspx") End Subsameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
dinotom
Member
24 Points
69 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 10:38 AM|LINK
Sameer,
This will generate a postback if autopostback is set to tue, and will return an unpopulated dropdown. And the dropdown selectedindexchange event never fires anyway but I have removed the annotation in the markup. But there should never be a postback as the createuser should generate the membership datastore information on click of createuser button and send you to the continueurl. The problem lies in that the selected value from the dropdown is ALWAYS the same value, index 0
sameer_khanj...
Star
7504 Points
1466 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 10:40 AM|LINK
sameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
dinotom
Member
24 Points
69 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 10:55 AM|LINK
I did change that code to the following, adding in the first listitem to select a question.
Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad If Not Page.IsPostBack Then Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList) ddl.Items.Add(New ListItem("Please select a security question")) Dim itemlist As New List(Of String) itemlist = GetSecurityQuestionsforDDL() If ddl IsNot Nothing Then For Each item As String In itemlist ddl.Items.Add(New ListItem(item)) Next ddl.DataBind() End If End If End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load RegisterUser.ContinueDestinationPageUrl = Page.ResolveUrl("~/Default.aspx") End SubI now get a question required error when using the form. The problem still lies in that when a selection is made from the question dropdown, the code is NOT recognizing it. It absolutely has something to do with the fact that this dropdown lies within another control as I placed another dropdown outside the control and get its value easily.
sameer_khanj...
Star
7504 Points
1466 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 11:04 AM|LINK
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser ' this should be after Dim ddl As DropDownList statement; Dim question as string = ddl.SelectedValue Dim ddl As DropDownList = DirectCast(RegisterUserWizardStep.ContentTemplateContainer.FindControl("Question"), DropDownList) Dim member As MembershipUser = Membership.CreateUser(RegisterUser.UserName, RegisterUser.Password, RegisterUser.Email, question, RegisterUser.Answer.ToLower(), False, MembershipCreateStatus.Success) Dim newUser As MembershipUser = Membership.GetUser(RegisterUser.UserName) Dim question as string = ddl.SelectedValue 'Other code End Subsameer.khanjit@gmail.com
View Blog
Click "Mark as Answer" on the post that helped you.
dinotom
Member
24 Points
69 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 12:48 PM|LINK
Unfortunately I have already tried all these things. I am going to approach this from a different perspective as I ve wasted too much time implementing it this way which should've been strightforward but apparently isn't. Thank you for your input.
N1ZAM
Member
85 Points
83 Posts
Re: retrieving dropdownlist selected value from a control within a control
Dec 26, 2012 02:53 PM|LINK
I dont think this is acctually required. Instead just use a simple ddl and use ddl.SelectedItem.Text to get the questions selected. That should solve the problem.