Persisting DropDownList Value in PageIndexChanged of GridView

喜你入骨 提交于 2020-01-03 03:21:08

问题


 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

The above Code is the code I'm having for Persisting DropDownList value but it is not working as expected.

In my GridView i have two Columns

Details of First column : I have list of employee names

Details of Second Column : It has a dropDownlist in each row with many items in it.

In my grid i have nearly 100 values, So i have set allowPaging="true" and have set pagesize="10".

The problem what I'm facing is

  • Step 1 : I'm selecting a value in dropdownlist of rowindex 2 in page 0
  • Step 2 : changing the page to 5, the value I selected in page 0 for the rowindex 2 also appears in the page 5 for the rowindex 2 as per my code.

But what i want is

  • Step 1 : I'm selecting a value in dropdownlist of rowindex 2 in page 0
  • Step 2 : changing the page to 5, no value should be selected as i haven't selected any values in it.
  • Step 3 : I i go back again to Page 0 the value of ddl in Rowindex 2 should have the previously selected value.

EDIT

I'm peristing the Values in Dictionary as mentioned 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

But i'm unable to repopulate it to the Dropdownlist


回答1:


Alright, based on what you have displayed here I have reached this conclusion.

You are saving the values to the Dictionary to the specific Row number. That is 0-9. However, row numbers does not take pagenumber into consideration. So I recommend you to do the following: (Change is on row 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

Now page 1 will have values: 0-9

Page 2 will have values 10-19 etc.



来源:https://stackoverflow.com/questions/22830180/persisting-dropdownlist-value-in-pageindexchanged-of-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!