我想扩展DropDownList控件来简单地添加一个额外的属性。添加一个额外的属性到asp.net DropDownList
代码如下
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
虽然包括很多来自其他来源的剪切/粘贴,似乎相当简单,我添加了一个局部变量,然后属性的get /组语句。
这有什么不对吗?我在与获得返回0的问题,即使我已经执行在此之前设定。
干杯! :d
编辑:
为了清楚起见,我用我的customDropDownList中继器,并在其ItemDataBound事件,我做了以下
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
不过,如果我访问PrimaryKey属性在稍后的时间,我得到0 ..了selectedValue和所有其余做工精细。
BBV您好,感谢您的帮助。虽然我不完全确定你会得到什么,但我没有改变我的ddl以任何方式处理它的项目集合的方式。我刚刚添加了额外的属性。所以上面应该和任何普通的dropDownList相同。 为了清晰起见,我将添加更多信息。 – 2009-07-08 15:20:42
Ahhh您需要将该属性添加到该对象的视图状态......查看Zachary的答案 – BigBlondeViking 2009-07-08 15:29:55