【问题标题】:How to run OnSelectedIndexChanged for a particular DataGridItem?如何为特定的 DataGridItem 运行 OnSelectedIndexChanged?
【发布时间】:2015-03-13 19:39:10
【问题描述】:

我已经使用包含单选按钮列表和下拉列表的模板制作了一个 DataGrid。 更改单选按钮列表的索引时,下拉列表将填充值。我的 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

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

【问题讨论】:

    标签: .net vb.net datagrid


    【解决方案1】:

    使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-10
      • 2013-03-29
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多