Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre lang="xml">Im very new and have found a solution which filter with on e grid but how do I manaipulate the Vb code to accomodate anothe industry filter.

here&#39;s my ASP current code
&lt;asp:GridView ID=&quot;GridView2&quot; runat=&quot;server&quot;
AutoGenerateColumns=&quot;False&quot; CellPadding=&quot;4&quot; DataSourceID=&quot;SqlDataSource2&quot;
ForeColor=&quot;#333333&quot; GridLines=&quot;None&quot; DataKeyNames=&quot;Permit_No&quot;
Width=&quot;798px&quot;&gt;
&lt;RowStyle BackColor=&quot;lightgrey&quot; ForeColor=&quot;#333333&quot; /&gt;
&lt;Columns&gt;

&lt;asp:BoundField DataField=&quot;Permit_No&quot; HeaderText=&quot;Permit_No&quot; SortExpression=&quot;Permit_No&quot; /&gt;
&lt;asp:HyperLinkField runat=&quot;server&quot; DataTextField=&quot;Name&quot; DataNavigateUrlFields=&quot;Permit_No,Name,Postal,Street,Tel,Fax,Cell,Website_Address,&quot;
DataNavigateUrlFormatString=&quot;Default2.aspx?ID={0}&amp;Name={1}&amp;Pos={2}&amp;Physical={3}&amp;Phone={4}&amp;PhoneFax={5}&amp;CellPhone={6}&amp;Web={7}&quot; HeaderText=&quot;Name&quot; /&gt;

&lt;asp:BoundField DataField=&quot;Spec_No&quot; HeaderText=&quot;Spec_No&quot;
SortExpression=&quot;Spec_No&quot; /&gt;
&lt;asp:BoundField DataField=&quot;Tel&quot; HeaderText=&quot;Tel&quot; SortExpression=&quot;Tel&quot; /&gt;

&lt;asp:TemplateField HeaderText=&quot;Region&quot; SortExpression=&quot;Region&quot;&gt;
&lt;HeaderTemplate&gt;
Region
&lt;asp:DropDownList ID=&quot;ddRegion&quot;
DataTextField=&quot;Region&quot;
AutoPostBack=&quot;true&quot;
OnSelectedIndexChanged=&quot;FilterDropDown_IndexChanged&quot;
OnPreRender=&quot;SetValue&quot;
DataSourceID=&quot;RegionDataSource&quot; runat=&quot;server&quot;/&gt;
&lt;/HeaderTemplate&gt;
&lt;EditItemTemplate&gt;
&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text=&#39;&lt;%# Bind(&quot;Region&quot;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt;
&lt;/EditItemTemplate&gt;
&lt;ItemTemplate&gt;
&lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&#39;&lt;%# Bind(&quot;Region&quot;) %&gt;&#39;&gt;&lt;/asp:Label&gt;
&lt;/ItemTemplate&gt;
&lt;/asp:TemplateField&gt;



&lt;asp:TemplateField HeaderText=&quot;Industry&quot; SortExpression=&quot;Industry&quot;&gt;
&lt;HeaderTemplate&gt;
Industry
&lt;asp:DropDownList ID=&quot;ddIndustry&quot;
DataTextField=&quot;Industry&quot;
AutoPostBack=&quot;true&quot;
OnSelectedIndexChanged=&quot;FilterDropDown_IndexChanged&quot;
OnPreRender=&quot;SetValue&quot;
DataSourceID=&quot;IndustryDataSource&quot; runat=&quot;server&quot;/&gt;
&lt;/HeaderTemplate&gt;
&lt;EditItemTemplate&gt;
&lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text=&#39;&lt;%# Bind(&quot;Industry&quot;) %&gt;&#39;&gt;&lt;/asp:TextBox&gt;
&lt;/EditItemTemplate&gt;
&lt;ItemTemplate&gt;
&lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text=&#39;&lt;%# Bind(&quot;Industry&quot;) %&gt;&#39;&gt;&lt;/asp:Label&gt;
&lt;/ItemTemplate&gt;
&lt;/asp:TemplateField&gt;







&lt;asp:BoundField DataField=&quot;Industry&quot; HeaderText=&quot;Industry&quot;
SortExpression=&quot;Industry&quot; /&gt;
&lt;asp:HyperLinkField /&gt;
&lt;/Columns&gt;
&lt;FooterStyle BackColor=&quot;#990000&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt;
&lt;PagerStyle BackColor=&quot;red&quot; ForeColor=&quot;#333333&quot; HorizontalAlign=&quot;Center&quot; /&gt;
&lt;SelectedRowStyle BackColor=&quot;#FFCC66&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;Navy&quot; /&gt;
&lt;HeaderStyle BackColor=&quot;red&quot; Font-Bold=&quot;True&quot; ForeColor=&quot;White&quot; /&gt;
&lt;AlternatingRowStyle BackColor=&quot;White&quot; /&gt;
&lt;/asp:GridView&gt;


VB&lt;NET Code
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If SearchList.SelectedItem.Value = &quot;1&quot; Then

GridView1.Visible = True
GridView2.Visible = False
GridView3.Visible = False
GridView4.Visible = False
ElseIf SearchList.SelectedItem.Value = &quot;2&quot; Then
GridView1.Visible = False
GridView2.Visible = True
GridView3.Visible = False
GridView4.Visible = False
ElseIf SearchList.SelectedItem.Value = &quot;3&quot; Then
GridView1.Visible = False
GridView2.Visible = False
GridView3.Visible = True
GridView4.Visible = False
ElseIf SearchList.SelectedItem.Value = &quot;4&quot; Then
GridView1.Visible = False
GridView2.Visible = False
GridView3.Visible = False
GridView4.Visible = True

End If

End Sub
Private filter As New Hashtable

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
&#39;searchTextBox.Text = &quot;&quot;
filter = If(ViewState(&quot;FilterArgs&quot;) Is Nothing, New Hashtable(), DirectCast(ViewState(&quot;FilterArgs&quot;), Hashtable))
End Sub
Protected Sub FilterDropDown_IndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim dd As DropDownList = DirectCast(sender, DropDownList)
Dim ddIndustry = DirectCast(sender, DropDownList)

If dd.SelectedItem.Text &lt;&gt; &quot;ALL Regions&quot; Then

If filter.ContainsKey(&quot;Region&quot;) Then
filter(&quot;Region&quot;) = &quot;=&#39;&quot; &amp; dd.SelectedItem.Text &amp; &quot;&#39;&quot;
Else
filter.Add(&quot;Region&quot;, &quot;=&#39;&quot; &amp; dd.SelectedItem.Text &amp; &quot;&#39;&quot;)
End If
Else
filter.Remove(&quot;Region&quot;)
End If

If dd.SelectedItem.Text &lt;&gt; &quot;ALL Industry&quot; Then

If filter.ContainsKey(&quot;Industry&quot;) Then
filter(&quot;Industry&quot;) = &quot;=&#39;&quot; &amp; ddIndustry.SelectedItem.Text &amp; &quot;&#39;&quot;
Else
filter.Add(&quot;Industry&quot;, &quot;=&#39;&quot; &amp; ddIndustry.SelectedItem.Text &amp; &quot;&#39;&quot;)
End If
Else
filter.Remove(&quot;Industry&quot;)
End If
ApplyGridFilter()
GridView2.PageIndex = 0

End Sub

Protected Sub ApplyGridFilter()
Dim args As String = &quot; &quot;
Dim i As Integer = 0
For Each key As Object In filter.Keys
If i = 0 Then
args = key.ToString() + filter(key).ToString()
Else
args += (&quot; AND &quot; &amp; key.ToString()) + filter(key).ToString()
End If
i += 1
Next
SqlDataSource2.FilterExpression = args
&#39;Filter needs to be saved between postbacks
ViewState.Add(&quot;FilterArgs&quot;, filter)
End Sub

Protected Sub SetValue(ByVal sender As Object, ByVal e As EventArgs)
Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim ddlIndustry As DropDownList = DirectCast(sender, DropDownList)

If filter IsNot Nothing Then
If filter.ContainsKey(&quot;Region&quot;) Then
For Each li As ListItem In ddl.Items
If li.Text = filter(&quot;Region&quot;).ToString().Substring(2, filter(&quot;Region&quot;).ToString().Length - 3) Then
li.Selected = True
Exit Sub
End If

Next
End If
Else
If filter.ContainsKey(&quot;Industry&quot;) Then
For Each li As ListItem In ddl.Items
If li.Text = filter(&quot;Industry&quot;).ToString().Substring(2, filter(&quot;Industry&quot;).ToString().Length - 3) Then
li.Selected = True
Exit Sub
End If

Next
End If


End If
End Sub
Protected Sub GridView2_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
ApplyGridFilter()
End Sub
&#39;Protected Sub btnRemoveFilter_Click(ByVal sender As Object, ByVal e As EventArgs)
&#39; &#39;ViewState.Remove(&quot;FilterArgs&quot;);
&#39; filter.Clear()

&#39; ApplyGridFilter()
&#39;End Sub
Please assist i have been trying without sucess</pre>
Posted
Comments
Johnny J. 14-Jul-10 4:21am    
You know, I can even be bothered to try to make something of this... Please try to format it in a readable way. Apart from that, it just looks like a code dump. You might want to describe what you're trying to do and what your problem is, as well as what errors you get

This is an utter disaster. You are using ASP.NET, not ASP. You should tag your question accordingly. You should also edit your post and make use of the preview functionality to see how your post looks. Then you should remove a ton of this code and give us just the bit that matters to what you're asking.
 
Share this answer
 
the code below only filters for region ( one dropdown list),i want to add another dropdown,if the user is not using the region dropdown he can filter by industry.therefore he can use the region or the industry dropdown.


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

filter = If(ViewState("FilterArgs") Is Nothing, New Hashtable(), DirectCast(ViewState("FilterArgs"), Hashtable))

End Sub

Protected Sub FilterDropDown_IndexChanged(ByVal sender As Object, ByVal e As EventArgs)

Dim dd As DropDownList = DirectCast(sender, DropDownList)
Dim ddIndustry = DirectCast(sender, DropDownList)

If dd.SelectedItem.Text <>"ALL Regions" Then

If filter.ContainsKey("Region") Then
filter("Region") = "=" & dd.SelectedItem.Text "'"

Else

filter.Add("Region", "=" & dd.SelectedItem.Text &"'"
End If

Else

filter.Remove("Region")
End If

ApplyGridFilter()
GridView2.PageIndex = 0

End Sub

Protected Sub ApplyGridFilter()

Dim args As String = ""
Dim i As Integer = 0

For Each key As Object In filter.Keys
If i = 0 Then
args = key.ToString() + filter(key).ToString()
Else
args += (" AND " & key.ToString()) + filter(key).ToString()
End If
i += 1
Next
SqlDataSource2.FilterExpression = args

ViewState.Add("FilterArgs", filter)
End Sub

Protected Sub SetValue(ByVal sender As Object, ByVal e As EventArgs)

Dim ddl As DropDownList = DirectCast(sender, DropDownList)
Dim ddlIndustry As DropDownList = DirectCast(sender, DropDownList)

If filter IsNot Nothing Then
If filter.ContainsKey(&quot;Region&quot;) Then
For Each li As ListItem In ddl.Items
If li.Text = filter(&quot;Region&quot;).ToString().Substring(2, filter("Region").ToString().Length - 3) Then
li.Selected = True
Exit Sub
End If

Next
End If

End Sub

Protected Sub GridView2_PageIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

ApplyGridFilter()

End Sub
 
Share this answer
 
Comments
Scubapro 14-Jul-10 5:50am    
Don't post this as an answer. Review/edit your original question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900