我用以下的代码实现了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
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
解决方案 »
- 类内定义一个static函数,函数名与类名相同
- ps 钢笔和画笔各有什么用啊
- 存放图片路径到ACCESS数据库并读取显示
- Ajax UpdatePanel控件凝惑
- 设计了一个IDAL接口,还大家看看还需要什么规格?
- 为什么我用alert('dsfsd');弹出对话框的时候,页面的字体会变大啊?
- 应该怎样做才能在客户端IE中输入数据库的帐号、密码,然后登录呢?很急,大家帮帮忙!
- 为何我的XP的IIS不能运行ASPX.NET啊!!!!!!!!!!!!!
- 上传文件的路径该怎么写阿,绝对路径相对路径都不行,请大家指点小弟嘛
- 频繁获取网站页面内容后被禁止了,是哪里设置的问题?
- 小小的登陆验证问题(I AM 菜鸟)
- 这样取域名是否正确?
由于我需要列的排序不变,而我的表字段又很多,所以不能用模板列,需要动态添加dropdownlist。
有没有哪位指点一下,谢谢!!!
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已经又重新生成了,根本不可能得到用户所选的值。应该怎么办啊?
DataView控件在哪里?又该怎么用呢?