Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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 May 9th, 2006, 09:14 AM
Authorized User
 
Join Date: May 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default CreateUserWizard with DropDownList

Hi,

I am trying to customize a CreateUserWizard to contain a DropDownList which allows me to select the role of a new user.
The DDL gets populated correctly, however, I can't seem to get the value of the dropdownlist selected by the user, just the initial value it was set to. The Dropdownlist is populated during the PageLoad event. Snippets of the code are shown below.
Please would someone show me where I'm going wrong....

Thank you,
Simon



<asp:createuserwizard id="cuwMain" runat="server" oncreateduser="cuwMain_CreatedUser" requireemail="false" completesuccesstext="The account has been successfully created." unknownerrormessage="The account was not created. Please try again.">
<wizardsteps>
<asp:createuserwizardstep id="CreateUserWizardStep1" ondeactivate="cuwStep1_Deactivate" runat="server">
<contenttemplate>
<table border="0">
.......
<tr>
<td align="right">
<asp:label id="UserRoleLabel" runat="server" associatedcontrolid="ddlUserRole">User Role:</asp:label></td>
<td>
<asp:dropdownlist id="ddlUserRole" runat="server"> </asp:dropdownlist>
</td>
</tr>
.......
</table>
</contenttemplate>
</asp:createuserwizardstep>




protected void Page_Load(object sender, EventArgs e)
{
((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).DataSourc e = Roles.GetAllRoles();
((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).DataBind( );
((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).Items[4].Selected = true;
}




protected void cuwMain_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(((TextBox)cuwMain.CreateUserSt ep.ContentTemplateContainer.FindControl("UserName" )).Text,
/ ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).SelectedV alue);
}
 
Old May 9th, 2006, 01:37 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You need to check for IsPostBack in your Page_Load event handler. Right no, you bind the drop downs to the data source every time the page loads. That includes the PostBack. So, when you made a new selection, the page post backs, the drop down is bound again which causes it to loose the selection you made. Try this:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
  ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).DataSourc e = Roles.GetAllRoles();
  ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).DataBind( );
  ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).Items[4].Selected = true;
}
}

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 9th, 2006, 08:12 PM
Authorized User
 
Join Date: May 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thank you, this fixed the problem. I guess I need to watch my postbacks! Is this the way fields are usually added to the CreateUserWizard or is there a better way?
Btw, your book is excellent and has helped me a lot learning asp.net 2.0.

Cheers,
Simon
 
Old May 10th, 2006, 12:58 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Simon,

Yeah, that's a common way to add the roles to a DropDown control. It's similar to how chapter 12 - The Bug Base does it in the Management section (Members.aspx.vb), although it uses the RowCommand of the GridView.

Another way to do it is with an ObjectDataSource control like this:
Code:
<asp:DropDownList 
      ID="DropDownList1" 
      runat="server" 
      DataSourceID="ObjectDataSource1"
 />
<asp:ObjectDataSource 
      ID="ObjectDataSource1" 
      runat="server" 
      SelectMethod="GetAllRoles" 
      TypeName="System.Web.Security.Roles"
 />
This binds the ODS to the GetAllRoles method of the System.Web.Security.Roles class.

Glad you are enjoying the book. Are you using one of the projects as the basis for your own? Or are you just learning from the code and building something new?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
 
Old May 11th, 2006, 03:08 AM
Authorized User
 
Join Date: May 2006
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Thank you for your help. I will also take a look at ObjectDataSources and Chapter 12 (not there yet).
As for the project, it is a Helpdesk type of application so it will probably be similar to your Bug Base app. I am using your book to get up to speed on asp.net 2.0 although I am using C# instead of VB.

Thanks once again,
Simon
 
Old March 23rd, 2007, 08:04 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

This post was really helpful... But I am having a few troubles converting the c# script to VBScript:

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
  ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).DataSourc e = Roles.GetAllRoles();
  ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).DataBind( );
  ((DropDownList)cuwMain.CreateUserStep.ContentTempl ateContainer.FindControl("ddlUserRole")).Items[4].Selected = true;
}
}

Can a help me out? I am pretty new to VBScript and finding it a struggle :(



--
Thanks
Andy
 
Old March 23rd, 2007, 09:44 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If (Page.IsPostBack = False) Then
            CreateUserWizard1.CreateUserStep.ContentTemplateCo ntainer.FindControl("ddlUserRole").DataSource() = Roles.GetAllRoles()
            CreateUserWizard1.CreateUserStep.ContentTemplateCo ntainer.FindControl("ddlUserRole").DataBind()
           CreateUserWizard1.CreateUserStep.ContentTemplateCo ntainer.FindControl("ddlUserRole").Items[4].Selected = true;
End If

I managed to get this far but I am stuck... The DataSource() and Items[] parameters seem to be invalid. :(

--
Thanks
Andy
 
Old March 23rd, 2007, 09:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

change the [] to ()...

also take out the () after the datasource....
(should be like this)
Code:
CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlUserRole").DataSource = Roles.GetAllRoles()
CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlUserRole").DataBind()
CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlUserRole").Items(4).Selected = true
HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
 
Old March 23rd, 2007, 10:08 AM
Registered User
 
Join Date: Mar 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Heya,

Thanks for your help, but its still not like it... It appears that DataSource & Items is not a member of the Web.UI.Control. I am stumped :(

--
Thanks
Andy
 
Old March 23rd, 2007, 10:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

i know see your problem better...

you have to cast the object to the correct type...

you have to do something like
Code:
dim dd as dropdownlist = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("ddlUserRole")
'then work with dd
dd.DataSource = Roles.GetAllRoles()
dd.DataBind()
d.Items(4).Selected = true
the problem with your code is that you are not obtaining the object that you want in the find control first, you are directly trying to work with the result of find control without cast it to the rigth type...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
CreateUserWizard AghaKhan ASP.NET 2.0 Professional 1 December 5th, 2007 08:14 PM
CreateUserWizard michaelcode ASP.NET 2.0 Basics 2 February 5th, 2007 06:17 PM
CreateUserWizard elavio ASP.NET 2.0 Basics 1 November 18th, 2006 07:50 PM
CreateUserWizard michaelcode ASP.NET 2.0 Basics 1 August 19th, 2006 09:04 AM
CreateUserWizard zoltac007 ASP.NET 2.0 Basics 4 June 1st, 2006 04:08 PM





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