views:

477

answers:

4

Hi,

I'm trying to extend the DropDownList control to simply add one extra property.

Code as follows

Public Class CustomDropDownList
    Inherits DropDownList

    Private key As Int32

    Public Property PrimaryKey() As Int32
        Get
            Return key
        End Get
        Set(ByVal value As Int32)
            key = value
        End Set
    End Property

End Class

Although that includes a lot of cut/paste from other sources, it seems fairly simple, I've added a local variable, and then the property's get/set statements.

Is there anything wrong with this? I'm having issues with the get returning 0 even though I've performed the set prior to this.

cheers! :D

EDIT:

For clarity, I'm using a repeater with my customDropDownList, and in its ItemDatabound event, I do the following

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
        Dim row As DataRowView = e.Item.DataItem
        Dim cdlConst As CustomDropDownList = e.Item.FindControl("cdlConstituencies")
        cdlConst.SelectedValue = row.Item("constituencyrefno")
        cdlConst.PrimaryKey = row.Item("uniqueid")
End If

But then if I access the primaryKey property at a later time, I get 0.. selectedValue and all the rest work fine.

A: 

Can you use the value parameter of the ListItem when you add it to the DropDownList

ddlMyDropDown.Items.Add(new ListItem(obj.Name(), obj.PrimaryKey.ToString()));

You just look at the SelectedValue for you PK

object obj = PrimaryKeyParse(ddlMyDropDown.SelectedValue);

Sorry this is in c#, not sure what it looks like for VB... let me go check:

ASPX:

<asp:DropDownList ID="ddlMyDropDown" runat="server">
    <asp:ListItem Text="-- Select --" Value="0" Selected="True"></asp:ListItem>
    <asp:ListItem Text="ObjectName 1" Value="PK00001" ></asp:ListItem>
    <asp:ListItem Text="ObjectName 2" Value="PK00002" ></asp:ListItem>
</asp:DropDownList>

HTML:

<select name="ddlMyDropDown" id="ddlMyDropDown">
    <option selected="selected" value="0">-- Select --</option>
    <option value="PK00001">ObjectName 1</option>
    <option value="PK00002">ObjectName 2</option>
</select>
BigBlondeViking
Hi BBV, thanks for your help. I'm not entirely sure what you're getting at though, I haven't changed the way my ddl handles its collection of items in any way.. I've just added the extra property. So the above should be identical to any normal dropDownList.I'll add more info for clarity.
Tabloo Quijico
Ahhh you you need to add that property to the view-state of the object... look at Zachary's Answer
BigBlondeViking
A: 

Ideally what you should do is just bind an object as you would normally do when binding to a list, and then when you want the SelectedItem just cast it as the object it was bounded to.

For the following example, assume that Foo is a class that has 2 properties...a PK and a Name;

MyDropDownList.DataSource = new List { 
    new Foo {PK = 1, Name = "Hello"}, 
    new Foo {PK = 2, Name = "something else"}
};
MyDropDownList.DataTextField = "Name";
MyDropDownList.DataValueField = "PK"
MyDropDownList.DataBind();

Now in some event, you can do this:

var theItem = (Foo)MyDropDownList.SelectedItem;
var mm = theItem.PK;  //now you can access your object's properties

With the above approach, you don't need to extend a DropDownList but just use the class' existing properties to get your data.

Andreas Grech
Thanks for the help, I may be misleading some of you with the question and for that I apologise. My dropdownlist is essentially a lookup table with name/id pairs. I set that up as you have shown. My datasource then has a field which I want to update with values from the dropdownlist - it is this datasource's primary key that I wish to also store within the dropdownlist - not on an item by item basis but for the whole list, then I can use the primary key for any updates on selecteditemchanged.. thank you! I think I'm going to check the viewState problem..
Tabloo Quijico
A: 

The problem you seem to be having is related to viewstate. Your control doesn't save the value of the PrimaryKey property into the control's view state, and so the value isn't persisted between postbacks.

Here's a link to an article that demonstrates how to do that: ViewState and controls

Zachary Yates
Looks interesting, thank you!
Tabloo Quijico
works a treat! I love you!
Tabloo Quijico
A: 

Hi all, I want to do this same exact thing, but have the extra property be available in Design Mode. In Design Mode, the property will have a dropdown list of values to choose from. Can you please help me on achieving this scenario? Thanks in advance!!!!

How far have you got so far? The initial part is simple enough, with the property appearing in the design view.. for a list of values I would imagine it would involve creating an enum and using that as the object type for the property.. no idea if this automatically transfers to the design view though.
Tabloo Quijico