|
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
|
|
|
January 2nd, 2007, 09:29 AM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Iterating through DropDownBox
This one will be a bit hard to describe what it is I am needing to do, but basically, I have a createUserWizard in which I populate a dropdownbox with all available roles (only three) and allow the admin to select which role he would like to select for the user he is creating. This works perfectly and I have some default code to store this also in my own DB (I know, extra work, as it is stored also in the ASPNETDB, but I hate working with the ID's -- I still have a problem with type "uniqueidentifier" I don't know how to create a variable in VB to store those).
Anyways, I have an "edit" screen I have created and I have the same dropdownbox--but I am wanting the selected index to be that of the role stored in the DB for that user. So, here's what's going on:
a) I grad the username and pass it on to editUser.aspx in a querystring.
b) The dropDownBox (id='roleDrop') is populated with roles (along with a value "pick a role")
c) I can capture the role for the user and store in a string via role = roles.getallrolls(username)
How can I iterate through items in the dropdownbox? I have tried doing something (forgive syntax, I am at work) like...
for x as int = 1 to ??? (what syntax should I use for total count in dropbox)
if role(0) {my DB only uses one role per user} = dropDownBox.items(0).value {?? that is the problem I am having--what is syntax?}
y=x
next x
dropDownBox.selectedIndex(y) {something of that nature}
Basically, I am wanting to iterate through the roles and check for a match against the role so that I can grab the index. Once I have the index, then I want to have that index as selected so that when the page loads, the dropdownbox is showing the role of that user.
I am just having issues with out of ranges on the the indexes. I am assuming my syntax isn't right for getting:
a) count of items in a dropdown
b) checking for matches between role() and Dropdownbox.Items()
Regards,
Rob
|
January 2nd, 2007, 06:28 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
In .NET parlance, uniqueidentifiers are called GUIDs. You create a brand new random one like this:
Dim myGuid As Guid = Guid.NewGuid()
or you can create one from a string (the QueryString for example)
Dim myGuid As Guid = new Guid(stringThatContainsAValidGuid)
or from an (Sql)DataReader:
Dim myGuid As Guid = myReader.GetGuid(columnIndex)
But to answer your more urgent question: you need to use FindByValue on the Items collection of the list, like this:
If DropDownList1.Items.FindByValue(myValue) IsNot Nothing Then
DropDownList1.Items.FindByValue(myValue).Selected = True
End If
FindByValue returns a ListItem when found, or Nothing when it's not found. Therefore, you need to check for Nothing first before you try to set the Selected property.
In your case, you need to replace myValue with role(0).
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
January 2nd, 2007, 07:00 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Ok--first, you have no idea how logically sound that sounded--and I was so dumbfounded that I didn't think of something like that--you should have seen my loop. However, it is not working--and I'm sure because something I am doing. Here are my two functions..one that populates the drop down box upon rendering..and when I test--it is populated. The second one is to select the role that is picked. What is funny is that robtest is a label--and it displays "Admin" Also--"Admin" is in the drop down list...but it is not selected when I run the page.
Also--the "Yes it ran" doesn't populate
Protected Sub FormView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.PreRender
Dim dropBox As New DropDownList
dropBox = FormView1.FindControl("RoleDrop")
dropBox.DataSource = Roles.GetAllRoles()
dropBox.DataBind()
dropBox.Items.Insert(0, "Choose a role")
End Sub
Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadComplete
Dim dropBox As New DropDownList
dropBox = FormView1.FindControl("RoleDrop")
Dim userNameStr As String
Dim roleStr As String()
Dim y As Integer
Dim count As Integer = 0
userNameStr = Request.QueryString("id")
roleStr = Roles.GetRolesForUser(userNameStr)
If dropBox.Items.FindByValue(roleStr(0)) IsNot Nothing Then
dropBox.Items.FindByValue(roleStr(0)).Selected = True
robtest.Text = "YES it ran"
End If
robtest.Text = roleStr(0)
End Sub
|
January 2nd, 2007, 07:09 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Have you tried to debug this page? If you do, you'd find the error pretty soon. Judging by your robtest.Text code, you haven't though.
I am not going to give away the entire solution, but instead give a few hints. Set a break point on the first line of each function by pressing F9. Then hit F5 to debug the app.
The order of events is important here.....
Is there a reason why you're choosing events like PreRender and LoadComplete instead of something like DataBound on the FormView and do it all in one fell swoop?
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
January 2nd, 2007, 07:14 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
To be honest, I am very ignorant of timing. I thought that, originally, my problem was that it was trying to select from the dropdown before it had been rendered...so I thought I would allow the drop to be "filled" and then, at a later time, query the list. I was getting weird indexing errors, on my iteration, that caused me to believe that the list was not getting populated before I was searching for a value.
That was my main thought...I figured that load complete for the page would certainly come after the rendering of the simple formview that is part of the page.
I, embarrassingly enough, also have issues with debugging--I never know what I am doing....I'm sorry.
I will say, however, that I am not getting an error at all..
I'll play...I appreciate your help.
Kind Regards,
Rob
|
January 2nd, 2007, 07:42 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Imar,
Ok--the hint was blatant. I changed to function below, and it worked. Honestly, I am not clear on order of events. If there is a link that explains it--I'd appreciate it--none of the Wrox books I own clearly show this.
It would be a HUGE favor if you could explain to me why it didn't work with the two functions I had...only if you have a min or two.
Thanks....you really have an unending knowledge that hits all different aspects of ASP/ VB--I really wish I had a 1/10th of your knowledge!
Kind Regards,
Rob
|
January 3rd, 2007, 03:14 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
It didn't work and you didn't get an error because the LoadComplete event comes before PreRender. So, you searched for an item in LoadComplete that wasn't there yet, and then only added it in PreRender afterwards. I agree that it's natural to think that a *Completed event comes before a Pre* event, but unfortunately for you that's not how it is....
PreRender is pretty much the last stage in which you can do something useful to your controls. Right after that, the controls in your page start outputting their HTML to the browser.
You may want to take a look here: http://msdn2.microsoft.com/en-us/lib...81(VS.80).aspx. Then look under Page Lifecycle | New Events (although you might as well read the entire article).
That page describes the available events, their order and whether they occur at postback or not. These events fire not only for your page, but also for all your controls on that page (essentially, because a Page *is* a Control). So, for example, the FormView fires it own OnPreRender event, and so does the Page itself.
If I were you, I'd read a little about debugging in ASP.NET 2. It's really really useful, and it keeps you from adding debug labels all over your code. By adding breakpoints like I suggested you can halt execution of your code and gradually step through it with a number of keys (look in the Debug menu). Along the way, you can diagnose objects / variables, test out things etc.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
|
January 3rd, 2007, 09:24 AM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
That helps tremendously---I am going to print that article and REALLY appreciate the help/guidance.
The debugging--it's probably just something that I just have to spend time on. I remember trying it for simple C++ programs I was writing and never seemed to be able to understand what I was doing.
I will say that the multiple testing labels to show variable values are cumbersome! :)
Thank you very much!
Rob
|
January 3rd, 2007, 04:35 PM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You're welcome.....
Do yourself a favor and look into debugging. It makes your life as a developer much and much more easy....
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
P.S. Did I already say that debugging makes a developer's life much easier?
|
|
|