0

我正在尝试设置作为网格视图中模板列的单选按钮列表的选定值。我必须使用后面的代码来执行此操作,因为包含数据(状态)的数据库字段包含空值,因此我不能在 asp 端使用 SelectedValue='<%# Bind("Status") %>' 。

有人建议我使用 onRowDataBound 来执行此操作并使用 DataItem 从数据源中检索值并使用它来设置 radiobuttonlist 选定值,但我不确定如何在 vb 代码中执行此操作。

我尝试了以下方法:

Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim radioList = TryCast(e.Row.FindControl("rblActions"), RadioButtonList)

        ' this will be the object that you are binding to the grid
        Dim myObject = TryCast(e.Row.DataItem, DataRowView)
        Dim sStatus As String = Convert.ToString(myObject("Status"))

        If sStatus <> Nothing Then

            radioList.SelectedValue = sStatus
        End If

    End If
End Sub

但它没有工作。任何帮助,将不胜感激。谢谢

4

1 回答 1

0

找到我自己的解决方案如下:

Sub gv_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

        Dim rbl As RadioButtonList = CType(e.Row.FindControl("rblActions"), RadioButtonList)

        Dim selected As String = e.Row.DataItem("Status").ToString

        If Not IsNothing(selected) Then
            rbl.SelectedValue = selected
        End If

    End If
End Sub
于 2013-09-04T13:35:08.630 回答