Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 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 December 17th, 2003, 09:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default Dropdown in repeater (Moved From C# Forum)

Hello all,

I am having major problems putting a dropdown list within a repeater table cell. I have tried several different ways to dynamically add the list items within the dropdown list all have provided an error of some kind.

Can anyone point me in the right direction.
Preferably c# but vb information would also help

It works fine with hard coded listitems but when I try to add them via binding it compains that the object has not been instantiated, which I though it had.

Any help would be much appreciated


======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
__________________
======================================
"They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad."
--Shakespeare
======================================
 
Old December 18th, 2003, 10:40 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Hey rod.

This subject can be a bit tricky. Can you explain a little more about what you are planning? Is this dropdown bound to data relative to the repeater? or do you just want a repeating dropdown of the same data?

For the latter, the problem is that the ddl is only instantiated within the context of an occurance of one repeater item. So what you need to do is bind the dropdown during each repeater item creation.

The repeater has an event "Repeater.ItemCreated" that fires for each item that gets created from your data bind. The handler for that event will receive an argument (usually "e") that is the RepeaterItemEventArgs for the current item. That arg object contains the property "Item" which is the current instance of the repeater item.

In the handler you should do the following:
- Find the dropdown (if you create it as "ddlMyDropDown" inside the repeater template, you should be able to find it as such within the controls collection of the current repeater item ("e.Item"): e.Item.FindControl("ddlMyDropDown"))
- Cast the found control to a DDL
- Set the datasource for the DDL and DataBind() it.

That will populate all DDLs in the repeater with the same set of items.

One note about this: I don't know about the datalist, but in the datagrid, you'll encounter and "Item" which is not actually a repeated item. The handler for the datagrid gets called for the header and footer as well. This might not apply to the datalist. If it does, just check the item type (e.Item.ItemType) for type "Item" or "AlternateItem" (are there AlternateItems in a datalist?).

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 18th, 2003, 09:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Peter,
Thanks heaps for that, you hit the nail on the head, I am repeating the same data, and all my problems have focused on the databind, I'll now try to databind on the item created event as suggested.
There are no alternative items, althought there should be, I've been changing the style attributes in code to keep it simple.
You have no idea of the round-about I've been on getting this to work.
Thanks again for your help.

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old December 18th, 2003, 10:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Peter,
Thanks for you help but it is still not recognizing the item as an instance of an object, I was unsure of the format of the Item Type attribute is it merely text "ITEM" and "HEADER" to test against, Either way I put a try catch arround the code in the item created function hoping that even if the header created an error it would continue and execute the next timew when the dropdown existed. It failed each timne and no data was entered into the boxes.
The error is below, thanks very much for your time to look at this.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 113: private void utilList_ItemCreated(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
Line 114: {
Line 115: ((DropDownList) e.Item.FindControl("DDLUtilType")).DataSource = dsUtils.Tables["utilCodeType"].DefaultView;*****ERROR THE FIRST TIME THE BOX IS USED EVEN IF NOT RELATED TO DATA, SAY BACKCOLOR.
Line 116: ((DropDownList) e.Item.FindControl("DDLUtilType")).DataTextField = "utilCode";
Line 117: ((DropDownList) e.Item.FindControl("DDLUtilType")).DataValueField = "utilCode";




======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old December 19th, 2003, 03:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Peter,
Just letting you know I have made some headway on this, I was able to get it to work by binding an array then worked out that to use a datatable i need to clear and fill <s>create </s>the datatable again on every item created event. Whilst it works now this does not seem to make much sence to me, why can't a disconnected object like a datatable be used globally to the page.
Anyways... Thanks heaps for your help in pointing me in the right direction.


======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old December 19th, 2003, 12:06 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

I wonder if the problem is that you are using "ItemCreated". I use the data binding event for a datagrid. On a datalist it looks like that would be ItemDataBound. Try that out instead and see what happens.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 23rd, 2003, 03:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Peter,

Thanks again, your assistance is very much appreciated. I have been able to get everything working, and will take some time to get a better understanding of the event sequence when the pressure is off a bit.
I am now using a datagrid for something else and find it much better to use, I wish I had never seen a repeater. I guess there will be pros and cons depending on what its used for but I will need some convincing to get me to use a repeater instead of a datagrid in the future.

Thanks for your help, your advise obviously carried through to my data grid work also.

Rod

======================================
They say, best men are moulded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old December 29th, 2003, 11:35 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Rod,

The datagrid is a great control if you need to interact with the data. if you want finer control over the layout of something that's just displayed, then the repeater would be useful. It gives you a streamlined data listing control with the templating capability but lacks the interaction of all the commands that the datagrid has. So it's really an issue of intended use. The datalist is somewhere in between: commands, but no autogeneration of columns for data members or built in "editing" columns. You'll figure it out.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old January 22nd, 2004, 11:27 AM
Registered User
 
Join Date: Jan 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,
I don't know if you still check this forum, but if you are - please help. I'm fighting the same problem with DDL in repeater. But in my case the data I need to display in DDL is relative to repeater's. I use a collection as a source for repeater. My question is: how can I refer back to that particular collection item that was a datasource for the "just created" repeater item?

Thanks,
Ilya.

 
Old January 22nd, 2004, 12:29 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

izarkhin -

I just did a test with a repeater to satisfy my own curiosity about your post. I couldn't find a direct way to do it in the markup (which i was hoping I could) but I was able to get something to work in the code-behind.

Private Sub rptRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptRepeater.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim objDDL As DropDownList = CType(e.Item.FindControl("ddlList"), DropDownList)
        objDDL.SelectedIndex = objDDL.Items.IndexOf(objDDL.Items.FindByValue(e.It em.DataItem.ToString))
    End If
End Sub

e.Item.DataItem is the key. That will give you the object of the bind, in your case it should give you the current item of the collection. I'm making the assumption that you want to pre-select something in a dropdownlist based on the value of a property of your collection item. My test only used an array list of integers so I could do a straight "tostring" on it without error. You'll need to convert it to the appropriate object type before you do can access the desired property for the DDL preselect value.

Peter
------------------------------------------------------
Work smarter, not harder.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Object Moved to Here r_ganesh76 General .NET 2 October 14th, 2009 01:12 AM
DropDown Within Repeater ninel ASP.NET 2.0 Basics 14 May 29th, 2008 12:10 PM
Dropdown in Repeater kylemccullough ASP.NET 1.0 and 1.1 Basics 7 June 17th, 2004 01:04 PM





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