的DataListItem为DropDownList或文本框VB.Net
-
20-08-2019 - |
题
我有一个的DataListItem其可以潜在地是一个下拉列表或文本框。要获得价值,我需要做的:
CType(item.FindControl("myControl"), TextBox).Text
或者
CType(item.FindControl("myControl"), DropDownList).SelectedValue.ToString()
但问题是,如果它是一个下拉列表,我得到..
无法转换类型的对象 'System.Web.UI.WebControls.DropDownList' 输入 'System.Web.UI.WebControls.TextBox'。
有没有一种方法来检查,如果CTYPE将采取Ctyping之前?
解决方案
使用TryCast:
Dim txt as TextBox = TryCast(item.FindControl("myControl"), TextBox)
If txt Is Nothing Then
TryCast(item.FindControl("myControl"), DropDownList)
End If
不隶属于 StackOverflow