Protected Sub grdView_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles grdView.PageIndexChanging
grdView.SelectedIndex = -1
grdView.PageIndex = e.NewPageIndex
' To persist DDL values at paging using datatable
Dim Data As New DataTable
Data.Columns.Add("RowIndex", Type.GetType("System.Int32"))
Data.Columns.Add("SelectedValue", Type.GetType("System.String"))
For Each Row As GridViewRow In grdView.Rows
Dim ddl As DropDownList = DirectCast(Row.FindControl("ddlSample"), ComboBox)
If ddl.SelectedValue <> "" Then
Dim Rows As DataRow = Data.NewRow
Rows("RowIndex") = Row.RowIndex
Rows("SelectedValue") = ddl.SelectedValue
Data.Rows.Add(Rows)
End If
Next
' Passing the datatable to session
Session("MYDataTable") = Data
grdView.DataSource = grdDataSource
grdView.DataBind()
If Session("MYDataTable") IsNot Nothing AndAlso Session("MYDataTable") IsNot DBNull.Value Then
Data = CType(Session("MYDataTable"), DataTable)
For Each row In Data.Rows
If e.NewPageIndex = row("PageIndex") Then
Dim ddl As DropDownList = DirectCast(grdView.Rows(row("RowIndex")).FindControl("ddlSample"), DropDownList)
ddl.SelectedValue = row("SelectedValue")
End If
Next
End If
上面的代码是我为Persisting DropDownList值所拥有的代码,但它没有按预期工作。
在我的 GridView 中,我有两个列
第一栏详情:我有员工姓名列表
第二栏详情:每行都有一个dropDownlist,其中包含很多项目。
在我的网格中,我有近100个值,所以我设置了allowPaging="true"
并设置了pagesize="10".
我面临的问题是
但我想要的是
修改
我正在按照 here..!!
提及字典中的值 Dim ddlValues As New Dictionary(Of String, Integer)
For rowIndex As Integer = 0 To grdOnlineVoter.Rows.Count
Dim ddl As ComboBox = grdView.Rows(rowIndex).FindControl("ddlsample")
If ddl IsNot Nothing Then
If ddl.SelectedIndex > 0 Then
Dim ddlIndex As Integer = rowIndex
ddlValues.Add(ddl.SelectedValue, ddlIndex)
End If
End If
Next
但是我无法将其重新填充到Dropdownlist
答案 0 :(得分:0)
好的,根据你在这里展示的内容,我得出了这个结论。
您将字典中的值保存到特定的行号。那是0-9。但是,行号不考虑页码。因此,我建议您执行以下操作:(更改在第6行)
Dim ddlValues As New Dictionary(Of String, Integer)
For rowIndex As Integer = 0 To grdOnlineVoter.Rows.Count
Dim ddl As ComboBox = grdView.Rows(rowIndex).FindControl("ddlsample")
If ddl IsNot Nothing Then
If ddl.SelectedIndex > 0 Then
Dim ddlIndex As Integer = rowIndex + (GridView1.PageIndex * 10)
ddlValues.Add(ddl.SelectedValue, ddlIndex)
End If
End If
Next
现在第1页将包含值:0-9
第2页的值为10-19等。