使用后面的代码在gridview中设置radiobuttonlist值

时间:2013-09-04 11:49:30

标签: asp.net vb.net gridview

我正在尝试设置一个radiobuttonlist的选定值,该值是gridview中的模板列。我不得不使用后面的代码执行此操作,因为包含数据的数据库字段(状态)包含空值,因此我无法使用SelectedValue ='<%#Bind(“Status”)%>'在asp方面。

有人建议我使用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

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

1 个答案:

答案 0 :(得分: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