如何在单击另一个下拉列表项时清除GridView?

时间:2017-02-06 06:02:11

标签: asp.net vb.net gridview viewstate

我有DescriptionDivision DropDownList

当我生成数据时,它将显示在GridView中。 当我在Description DropDownList中选择另一个项目时,页面必须清晰,但我的问题是,即使我已经在DropDownList中选择了另一个项目,前面数据的GridView仍会显示。

这是Page_load

的代码

受保护的子Page_Load(ByVal sender As Object,ByVal e As System.EventArgs)处理Me.Load             userid = IIf(SessionHandler.UserID =“”,User.Identity.Name.ToUpper,SessionHandler.UserID)

        If Not IsPostBack Then
            Description.Items.Clear()

            If com.isAdmin(com.RetUser_Id(userid)) Then
                Description.Items.Add(New ListItem("Number Of Part Numbers/Family Input By User", "1"))
            End If

            Description.Items.Add(New ListItem("Product Family with No CAS Number", "2"))
            Description.Items.Add(New ListItem("Product Family with Inactive Exemption", "3"))
            Description.Items.Add(New ListItem("Part Number Validation", "4"))

            InitializeSearchOptions()
        End If

        ' this conditional statement will not bind the gridview if the sender object is from dropdownlist
        ' this will avoid unnecessary binding and reduce execution time
        Dim ctrlname As String = Page.Request.Params.Get("__EVENTTARGET")
        If ctrlname <> Nothing And ctrlname <> String.Empty Then
            If Not ctrlname.ToLower.Contains("dropdownlist") And ViewState.Item("Query") <> Nothing Then
                BindGridview(ViewState.Item("Query"))
            End If
        Else
            For Each ctrl As String In Page.Request.Form
                Dim c As Control = Page.FindControl(ctrl)

                If Not TypeOf (c) Is System.Web.UI.WebControls.Button Then
                    If ViewState.Item("Query") <> Nothing Then
                        BindGridview(ViewState.Item("Query"))
                    End If

                    Exit For
                End If
            Next
            Exit Sub

        End If
    End Sub

这是我的Description DropDownList

的代码

受保护的Sub Description_SelectedIndexChanged(ByVal sender As Object,ByVal e As System.EventArgs)Handles Description.SelectedIndexChanged             InitializeSearchOptions()         结束子

InitializeSearchOptions

的代码
Private Sub InitializeSearchOptions()
    Dim dt As New DataTable

    Division.Items.Clear()

    Panel2.Visible = False

    If Description.Text = "1" Then

        Division.Items.Add(New ListItem("Select Division", "0"))
        Division.AutoPostBack = True
        Username.Items.Clear()

        Username.Items.Add(New ListItem("Select UserName", "0"))
        CalendarStyle()
        MultiView1.ActiveViewIndex = 0

    ElseIf Description.Text = "2" Then

        Division.Items.Add(New ListItem("Select Division", "0"))
        Division.AutoPostBack = False
        MultiView1.ActiveViewIndex = 1

    ElseIf Description.Text = "3" Then

        Division.Items.Add(New ListItem("Select Division", "0"))
        Division.AutoPostBack = False
        MultiView1.ActiveViewIndex = 2

    ElseIf Description.Text = "4" Then
        Division.AutoPostBack = False
        Division.Items.Add(New ListItem("Select Division", "0"))
        MultiView1.ActiveViewIndex = 3
        Panel2.Visible = True
    End If

    Division.DataSource = GetDivision()
    Division.DataBind()
End Sub

BindGridView的代码

Private Sub BindGridview(ByVal query As String)
    ViewState.Add("Query", query)
    Dim strBB As String = ""

    Dim count As Integer = 0

    If hiddenDescription.Value = "4" Then
        DataSource = New DataTable
        DataSource.Columns.Add(New DataColumn("PART NUMBER", GetType(String)))
        DataSource.Columns.Add(New DataColumn("IS EXIST?", GetType(String)))
        DataSource.Columns.Add(New DataColumn("DIVISION NAME", GetType(String)))

        Dim str() As String = PartNo.Text.Split(vbNewLine)

        For Each value As String In str

            If value.Trim = String.Empty Then
                Continue For
            End If

            strBB = "SELECT DISTINCT a.Part_No AS 'PART NUMBER', a.Part_No AS 'IS EXIST?', c.Div_LE_Name AS 'DIVISION NAME' FROM MPartNumber a INNER JOIN MFamily b ON a.Family_Id = b.Family_Id INNER JOIN MDivision c ON b.Div_Id = c.Div_Id WHERE ('" + Division.Text + "' = '0' OR c.Div_Id = '" + Division.Text + "') AND " + IIf(value.Contains("*"), "a.Part_No LIKE '" + value.Trim().Replace("*", "%") + "'", "a.Part_No = '" + value.Trim() + "'")
            Dim dt As DataTable = dbHelper.GetRecordDT(strBB)

            Dim i As Integer
            For i = 0 To dt.Rows.Count - 1

                Dim row As DataRow = DataSource.NewRow
                If dt.Rows.Count > 0 Then
                    row("PART NUMBER") = dt.Rows(i)(0).ToString()
                    row("IS EXIST?") = "YES"
                    row("DIVISION NAME") = dt.Rows(i)(2).ToString()
                Else
                    row("PART NUMBER") = value.Trim
                    row("IS EXIST?") = "NO"
                    row("DIVISION NAME") = ""
                End If

                DataSource.Rows.Add(row)
            Next
        Next
    Else
        DataSource = dbHelper.GetRecordDT(query)
    End If

    count = DataSource.Rows.Count

    GridView1.DataSource = Nothing
    GridView1.DataBind()
    GridView1.PageIndex = 0

    GridView1.DataSource = DataSource
    GridView1.Visible = True
    GridView1.DataBind()

    ExportToExcel.Visible = IIf(GridView1.Rows.Count < 1, False, True)
    Panel1.Visible = IIf(GridView1.Rows.Count < 1, False, True)
    SetTotalResult(count)
End Sub

我试图在gridview中清除Viewstate,但它仍然是相同的。 我也试过了autopostback。 我在互联网上搜索了不同的解决方案,但我似乎无法解决这个问题。 我希望你能帮助我。 感谢。

更新

试过这个

ViewState.Remove("Query")

VieState.Clear()
在Page_Load中

,每次我在DropDownList中选择另一个项目,但它仍然是相同的。先前成功加载的数据仍会显示。

1 个答案:

答案 0 :(得分:1)

在dropwonlist selectedindexchange事件中,编写以下代码。

GridView1.DataSource = Nothing
GridView1.DataBind()