如何为特定的DataGridItem运行OnSelectedIndexChanged?

时间:2015-01-15 12:02:04

标签: .net vb.net datagrid

我使用包含radiobutton列表和下拉列表的模板创建了一个DataGrid。 在更改radiobutton列表的索引时,下拉列表将填充值。我的OnSelectedIndexChanged方法代码是:

Protected Sub rbtnLstOptions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
                Dim rbLst As RadioButtonList
        Dim DBAccess As New DBAccess
        Dim check As Integer
        Dim strQuery As String
        Dim ddl As String

        Dim newval As String

        For Each dgItem As DataGridItem In dgQuestions.Items

            rbLst = CType(dgItem.FindControl("rbtnLstOptions"), RadioButtonList)
            ddl = CType(dgItem.FindControl("ddlMembers"), DropDownList).SelectedValue
            newval = CType(dgItem.FindControl("rbtnLstOptions"), RadioButtonList).SelectedValue


            If (rbLst.SelectedValue.ToString <> "" And ddl = "") Or (rbLst.SelectedValue.ToString <> "" And rbLst.SelectedValue <> Me.PreviousSelectedValue) Then
                check = rbLst.SelectedItem.Value
                strQuery = "Select SNo,Participant from Participants where Role=" & check
                dsRoles = DBAccess.RunQueryReturnDataset(strQuery)
                CType(dgItem.FindControl("ddlMembers"), DropDownList).DataTextField = "Participant"
                CType(dgItem.FindControl("ddlMembers"), DropDownList).DataValueField = "SNo"
                CType(dgItem.FindControl("ddlMembers"), DropDownList).DataSource = dsRoles
                CType(dgItem.FindControl("ddlMembers"), DropDownList).DataBind()
                CType(dgItem.FindControl("ddlMembers"), DropDownList).Visible = True
                'Me.PreviousSelectedValue = rbLst.SelectedValue
            End If
        Next
    End Sub

运行此代码时,将遍历所有数据网格项目,并根据给定的条件填充值。我希望此方法仅在特定数据网格项目单选按钮索引更改时执行,而不是对所有数据网格项目执行。

1 个答案:

答案 0 :(得分:0)

使用SELECT CASE检查其索引?

Protected Sub rbtnLstOptions_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

    Select Case rbtnLstOptions.SelectedIndex    'ADD THIS
        Case 1,3,5 'This are the radio button indices where you only want an action to take effect
             Dim rbLst As RadioButtonList
             Dim DBAccess As New DBAccess
             Dim check As Integer
             Dim strQuery As String
             Dim ddl As String

             Dim newval As String

             For Each dgItem As DataGridItem In dgQuestions.Items

                 rbLst = CType(dgItem.FindControl("rbtnLstOptions"), RadioButtonList)
                 ddl = CType(dgItem.FindControl("ddlMembers"), DropDownList).SelectedValue
                 newval = CType(dgItem.FindControl("rbtnLstOptions"), RadioButtonList).SelectedValue


                 If (rbLst.SelectedValue.ToString <> "" And ddl = "") Or (rbLst.SelectedValue.ToString <> "" And rbLst.SelectedValue <> Me.PreviousSelectedValue) Then
                     check = rbLst.SelectedItem.Value
                     strQuery = "Select SNo,Participant from Participants where Role=" & check
                     dsRoles = DBAccess.RunQueryReturnDataset(strQuery)
                     CType(dgItem.FindControl("ddlMembers"), DropDownList).DataTextField = "Participant"
                     CType(dgItem.FindControl("ddlMembers"), DropDownList).DataValueField = "SNo"
                     CType(dgItem.FindControl("ddlMembers"), DropDownList).DataSource = dsRoles
                     CType(dgItem.FindControl("ddlMembers"), DropDownList).DataBind()
                     CType(dgItem.FindControl("ddlMembers"), DropDownList).Visible = True
                     'Me.PreviousSelectedValue = rbLst.SelectedValue
                 End If
             Next
        Case Else
            'Do something here or just remove this
    End Select
End Sub