DataListItem à DropDownList ou TextBox VB.Net
-
20-08-2019 - |
Question
J'ai un DataListItem qui peut potentiellement être un dropdownlist ou une zone de texte. Pour obtenir la valeur que je dois faire:
CType(item.FindControl("myControl"), TextBox).Text
ou
CType(item.FindControl("myControl"), DropDownList).SelectedValue.ToString()
Le problème est, si c'est un dropdownlist que je reçois ..
Impossible de coulée objet de type 'System.Web.UI.WebControls.DropDownList' taper 'System.Web.UI.WebControls.TextBox'.
Y at-il un moyen de vérifier si le CType prendra avant Ctyping il?
La solution
Utilisation TryCast:
Dim txt as TextBox = TryCast(item.FindControl("myControl"), TextBox)
If txt Is Nothing Then
TryCast(item.FindControl("myControl"), DropDownList)
End If
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow