我用以下的代码实现了datagrid嵌入dropdownlist,并在点击编辑后,dropdownlist中的选定项为该行记录所对应的值,但是更新的时候得不到用户所选的值,应该怎么办?
    Sub SetTextboxWidth(ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        If DataGrid1.EditItemIndex <> -1 Then
            e.Item.Cells(6).Controls.Clear()
            Dim MyDropDownList As New DropDownList()
            e.Item.Cells(6).Controls.Add(MyDropDownList)
            MyDropDownList = CType(e.Item.Cells(6).Controls(0), DropDownList)
  
            Dim str As String = " SELECT DISTINCT 姓名 FROM  UserTable "
            Dim da As OleDbDataAdapter = New OleDbDataAdapter(str, cn_main)
            Dim dstmp As New DataSet()
            da.Fill(dstmp)
            Dim k As Int16
            MyDropDownList.Items.Add("")
            For k = 0 To dstmp.Tables(0).Rows.Count - 1
                MyDropDownList.Items.Add(dstmp.Tables(0).Rows(k).Item(0))
                If Not IsDBNull(dstmp.Tables(0).Rows(k).Item(0)) Then
                    If dstmp.Tables(0).Rows(k).Item(0) = Label17.Text Then
                        MyDropDownList.SelectedIndex = k + 1
                    End If
                End If
            Next
         
        End If
    End Sub
   '确认删除,控制列宽,ID列不可见
    Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
                Dim myTableCell As TableCell
                myTableCell = e.Item.Cells(1)
                Dim myDeleteButton As LinkButton
                myDeleteButton = myTableCell.Controls(0)
                myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?');")
        End Select
      
        If (e.Item.ItemType <> ListItemType.Pager) And (e.Item.ItemType <> ListItemType.Footer) Then
            e.Item.Cells(2).Visible = False  'ID列不可见
        End If
        If (e.Item.ItemType = ListItemType.EditItem) Then
            SetTextboxWidth(e)
        End If
    End Sub
  Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.EditCommand
        If e.Item.Cells(3).Text.IndexOf(Label13.Text) = -1 Then
            Label17.Text = e.Item.Cells(6).Text
            DataGrid1.EditItemIndex = Int(e.Item.ItemIndex)
        End If
        mydatabind()
    End Sub

解决方案 »

  1.   

    没有人帮忙吗,这个问题很急,我弄了很久一直不成功
    由于我需要列的排序不变,而我的表字段又很多,所以不能用模板列,需要动态添加dropdownlist。
    有没有哪位指点一下,谢谢!!!
      

  2.   

    试试用Reqeust.From["下拉控件的ClientID"]来提取用户所输入的值,当然,你在动态添加dropdownlist的时候需要把此id记录到ViewState
      

  3.   

    请问怎样把dropdownlist的id记录到ViewState?还有dropdownlist是要在局部定义还是全局定义?
      

  4.   

    把它动态添加到myTableCell.Controls中去就可以了吧
      

  5.   

    e.Item.Cells(6).Controls.Clear()
       Dim MyDropDownList As New DropDownList()
       e.Item.Cells(6).Controls.Add(MyDropDownList)
       MyDropDownList = CType(e.Item.Cells(6).Controls(0), DropDownList)
    这已经实现了DropDownList的添加,问题是怎么把MyDropDownList添加到ViewState,因为点击更新刷新的时候MyDropDownList已经又重新生成了,根本不可能得到用户所选的值。应该怎么办啊?
      

  6.   

    可能,不过要配和JS来做.用DataView控件可以哈.
      

  7.   

    JS我比较不熟,有没有比较简单的办法来解决这个问题啊,只用ASP.net代码能实现吗?
    DataView控件在哪里?又该怎么用呢?
      

  8.   

    怎么都没有人回答啊?help!!!!!!!!!!1