我相當肯定,這是行不通的,正確的翻譯是:
If(TypeOf Bind("sex") Is DBNull, Nothing, Bind("sex"))
爲什麼不在代碼隱藏中以可讀的方式執行它?
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim sex = getSexFromStoredProcedure()
If Not sex Is Nothing Then rd.SelectedValue = sex
End If
End Sub
編輯:你評論說,這是一個FormView
內。我會告訴你如何在DataBound
事件中做到這一點。
Private Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
Select Case FormView1.CurrentMode
Case FormViewMode.ReadOnly
' adjust the DataSource accordingly if its not a DataRow '
Dim row = DirectCast(FormView1.DataItem, DataRow)
Dim LblSex = DirectCast(FormView1.FindControl("LblSex"), Label)
Dim sex As String = row.Field(Of String)("Sex")
LblSex.Text = If(sex Is Nothing, "", sex)
Case FormViewMode.Edit
' adjust the DataSource accordingly if its not a DataRow '
Dim row As DataRow = DirectCast(FormView1.DataItem, DataRow)
' assuming your RadioButtonList is inside the EditItemTemplate '
Dim RblSex = DirectCast(FormView1.FindControl("RblSex"), RadioButtonList)
Dim sex As String = row.Field(Of String)("Sex")
If Not sex Is Nothing Then RblSex.SelectedValue = sex
Case FormViewMode.Insert
End Select
End Sub
什麼是你想才達到 – 2012-04-12 19:48:51
'如果(typeof運算綁定(「性」)是DBNull的,沒什麼,綁定(「性」))'但是我不認爲這工作。 – 2012-04-12 19:50:36
如果我的存儲過程返回null爲「性別」比沒有被選中,否則將選擇「男性」/「女性」基於從綁定返回的值(「性別」) – SZT 2012-04-12 19:51:51