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