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 25th, 2006, 12:11 PM
Authorized User
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Generating differing dropdowns in a repeater?

Hi there, first post on the board, I am hoping there is someone around that would be able to give me a hand...

Basically, here's the long and short of my problemo:
I am no stranger to web scripting and am quite fluent in php and a few other languages, but am learning asp.net (vb)...
So most of the problems I have been encountering in this project are not logic problems but more or less getting the idea across in vb!

Essentially, I have a list of data which needs to be displayed, probably via a repeater. No problem. This data is a list of several categories. For each one of these categories (for each row), there will be a dropdown box at the end of it, which is populated by a list of users, thereby matching users to categories.

This wouldn't be such a big deal to do if it were the *same* list of users for each dropdown box. In reality, the users displayed in each dropdown box is dependent on another db table. So, for category 1, users A, B, and C may be choices in the dropdown, but for category 2, users B, D, and F will be choices, and so on.

One suggestion was to embed an invisible label control into the itemtemplate of the repeater, which I did (called IDLabel). This label control would hold the ID of the category, so I could use it in the onItemDataBound event to dynamically generate a query from which to populate the dropdown menu.

So, in my codebehind, under the repeater's onItemDataBound subroutine, I included the following:
Dim thisLabel As Label = e.Item.FindControl("IDLabel")

Then, I assumed, I could use thisLabel.Text and put it into a query (which I am unsure how the query will work past that point)

Upon executing the next line, something to the effect of
somestring &= thisLabel.text
I got the following error:
Object reference not set to an instance of an object.

I saw an example online that used the exact same code, and for some reason, mine bombs out..

This whole object-oriented event-driven thing is pretty cool one minute and then a gigantic pain in the rear another minute... mostly because I'm still trying to understand how things work in this strange new asp.net world ;)

Any help is appreciated, maybe even a point in the right direction.

 
Old May 25th, 2006, 12:49 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 lancer,

Without looking at your code, this is pretty hard to answer.

Are you checking the ItemType of the e argument and only try to find the label inside an Item and AlternatingItem?

Also, you'll need to cast your label to a proper label, like this:

Dim thisLabel As Label = CType(e.Item.FindControl("IDLabel"), Label)

Finally, instead of reading a label, you could also try to access e.Item.DataItem which returns an instance of the object that is used to bind the repeater rows.

If this doesn't help, can you post the relevant parts of your code?

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 25th, 2006, 01:48 PM
Authorized User
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah ha, you are correct-- it was evidently looking for IDLabel within the header template, which would have not returned a result, giving me an error... I seem to remember seeing that caveat before, it just didn't make any sense to me at the time.

So now, I can change or use IDLabel, which brings me to my next point of question--

so if I have the necessary information to assemble a query, can I put a DropDownList control into the ItemTemplate, and then make up a data source in my ItemDataBound subroutine?
(i.e. construct a query and say UsersDropDown.DataSource = IteratingDataSource and then databind them?)

Perhaps I am on the right track with that, but I am still in the dark on how I will be able to process the informtion once it is posted... thanks for the help so far, let me know if I am getting somewhere on this one :)

 
Old May 25th, 2006, 02:06 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, you're on the right track.

The concepts to fill the DDL and retrieve the info later are pretty much the same.

Similar to how you found the Label, you can also find the DropDownList, bind it to a datasource and call DataBind().

Then, on post back, you can use FindControl again to find the DropDownList and get its Selected item.

However, it's probably a lot easier to add a DataSource control to the Item template, and bind that to the control. Then you can bind the SelectedValue of the DropDown to an item in its parent repeater. like this:

<asp:TemplateField HeaderText="LogType" SortExpression="LogType">
  <EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server"
        SelectedValue='<%# Bind("LogType") %>'
        DataSourceID="SomeDataSource" DataTextField="Description"
        DataValueField="Id">
    </asp:DropDownList>
  </EditItemTemplate>
  <ItemTemplate>
    <asp:DropDownList ID="DropDownList1" runat="server"
        SelectedValue='<%# Bind("LogType") %>'
        DataSourceID="SomeDataSource" DataTextField="Description"
        DataValueField="Id">
    </asp:DropDownList>
  </ItemTemplate>
</asp:TemplateField>

You can define the DataSource "SomeDataSource" anywhere on your page.

Notice how the dropdown is bound to the field LogType. This is a field from the parent data source that is bound to the repeater.

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 25th, 2006, 04:39 PM
Authorized User
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

well, here's what I have so far:

Sub TrackChairRepeater_DataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim thisLabel As Label = CType(e.Item.FindControl("IDLabel"), Label)
        Dim UsersDD As DropDownList = CType(e.Item.FindControl("UsersDD"), DropDownList)
        Dim resultLabel As Label = CType(e.Item.FindControl("resultLabel"), Label)

        DropDownSource.SelectCommand = "select * from vwUserPrefTrackInConference where TrackInConferenceID = '" & thisLabel.Text & "'"
        resultLabel.Text = DropDownSource.SelectCommand
        UsersDD.DataBind()
    End Sub


and


<ItemTemplate>
            Track : <%#Container.DataItem("TrackName")%><br />
            Track ID : <%#Container.DataItem("TrackID")%><br />
            TrackInConference ID : <%#Container.DataItem("TrackInConferenceID")%><b r />
            <asp:Label id="IDLabel" Runat="Server" Text='<%# Eval("TrackInConferenceID") %>' Visible="false" /><br />
            <asp:DropDownList ID="UsersDD" runat="server" DataSourceID="DropDownSource" DataTextField="UserID" DataValueField="UserID"></asp:DropDownList>
            <asp:label id="resultLabel" runat="server" /><br />
            <br />
        </ItemTemplate>


It spits out two different queries in the repeater, as one would expect (which is good):
select * from vwUserPrefTrackInConference where TrackInConferenceID = '5'
and
select * from vwUserPrefTrackInConference where TrackInConferenceID = '7'

but, the dropdown boxes both use the results from the last query... perhaps they are getting overwritten and I am not quite doing something right...

if I could get the dropdown boxes to display the results from the respective queries, then everything would be great...

it's getting closer I think..

 
Old May 26th, 2006, 03:34 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Have you tried moving the data source to inside the ItemTemplate??

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 30th, 2006, 08:58 AM
Authorized User
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Imar
 Have you tried moving the data source to inside the ItemTemplate??

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Yeah, shortly after I posted that, I tried moving the data source, which ended up working. It is definitely looking good, but of course I still need to process the data once it is posted..
On that point I am still a little bit fuzzy...
Do I need a condition in my OnDataBound subroutine to see if the form has been posted, or do I go about it some other way? I believe e.Item.FindControl will work only in a subroutine that has been passed the RepeaterItemEventArgs, correct? I assume that means that I have to take care of the processing in my OnDataBound subroutine as well...?
Thanks for the help so far, I would definitely be lost without it!

 
Old May 30th, 2006, 02:01 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 lancer,,

You can probably do this inside a button's Click event (or another event that triggers a PostBack).

Instead of using ItemDataBound that fires for each item when the repeater is being data bound, you can loop through the items manually. Then, like you said, you don't have access to e.Item (which is indeed a property of the e argument of some events), you can access each Item individually:
Code:
  Protected Sub Button1_Click(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles Button1.Click
    Dim myItem As RepeaterItem
    For Each myItem In Repeater1.Items
      'myItem.FindControl(.....)
    Next
  End Sub
  This code loops through the Items collection. Inside the loop, myItem is a RepeaterItem that gives you access to the exact same stuff as e.Item in the ItemDataBound event does.

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
button list with dropdowns? lisabb ASP.NET 2.0 Basics 2 June 1st, 2007 08:56 AM
Dynamic Dropdowns jasmine_vj Classic ASP Basics 2 April 17th, 2006 12:47 PM
Dropdowns in datagrid amithkiran ASP.NET 1.x and 2.0 Application Design 1 March 24th, 2005 05:33 AM
Three dropdowns, one depended on another. Possible gilgalbiblewheel Classic ASP Databases 2 December 26th, 2004 05:59 PM





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