I have Description
and Division
DropDownList
When I generate data it will be shown in GridView. When I choose another Item in Description DropDownList the page must be clear but my problem is that the GridView of the previous data still shows even if I choose another Item in the DropDownList already.
Here's my code for Page_load
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles 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
Here's my code for Description
DropDownList
Protected Sub Description_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Description.SelectedIndexChanged InitializeSearchOptions() End Sub
code for 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
Code for 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
I tried to clear the Viewstate in gridview but it's still the same. I also tried the autopostback. I searched different solutions for this in the internet but I can't seem to solve this problem. I hope you can help me with this. Thanks.
UPDATE
Tried this
ViewState.Remove("Query")
and
VieState.Clear()
in Page_Load and every time I select another item in DropDownList but it's still the same. The data that previously loaded successfully still shows.