Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

gridview with listbox filter problem

Status
Not open for further replies.

PureDevelopers

IS-IT--Management
Aug 25, 2006
18
US
I have a gridview with some control filters. A couple of text, a drop down list and a listbox with multiple.
The sql is placed in the code behind in the isPostBack section. For some reason, the grid does not redraw itself
when I change the filter values unless I change other filter values in addition to the listbox, and I can't figure out why. All filters are controlParameters.
How do I get the grid to recognize that the listbox has changed?

here is the code:

<tr valign="top">
<td class="Content" align="right">Level:&nbsp;</td>
<td class="Content" align="left">
<asp:ListBox ID="lbLevel" EnableViewState="false" runat="server" DataSourceID="dsLevel" DataTextField="Name"
DataValueField="Name" SelectionMode="Multiple"></asp:ListBox></td>
</tr>
<tr valign="top">
<td class="Content" align="right"><asp:ImageButton ID="btnSubmit" ImageUrl="~/Images/Btn_Submit.jpg" runat="server" /></td>
<td class="Content" align="left"><asp:ImageButton ID="btnReset" ImageUrl="~/Images/Btn_Reset.jpg" runat="server" /></td>
</tr>
<tr valign="top">
<td colspan="2" align="center" class="Content"><br />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" CssClass="Content"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1" BackColor="White"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" CellPadding="2" HorizontalAlign="Left" PageSize="3">
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<%# DisplayLink(Eval("PID"), Eval("FirstName"), Eval("EmailFlag"), Eval("Email")) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName" />
<asp:BoundField DataField="Gender" HeaderText="Gender" SortExpression="Gender" />
<asp:BoundField DataField="DOB" HeaderText="Age" SortExpression="DOB" />
<asp:BoundField DataField="Level" HeaderText="Level" SortExpression="Level" />
<asp:BoundField DataField="Side" HeaderText="Side" SortExpression="Side" />
<asp:BoundField DataField="Height" HeaderText="Height" SortExpression="Height" />
<asp:BoundField DataField="SettingStyle" HeaderText="Setting Style" SortExpression="SettingStyle" />
<asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
</Columns>
<HeaderStyle BackColor="DimGray" Font-Underline="True" ForeColor="White" Wrap="False"
BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" />
<RowStyle VerticalAlign="Top" />
<SelectedRowStyle BackColor="White" HorizontalAlign="Left" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>">
<SelectParameters>
<asp:ControlParameter ControlID="txtFirstName" PropertyName="Text" Name="FirstName" Type="String" DefaultValue="%" />
<asp:ControlParameter ControlID="txtLastName" PropertyName="Text" Name="LastName" Type="String" DefaultValue="%" />
<asp:ControlParameter ControlID="ddlGender" PropertyName="SelectedValue" Name="Gender" Type="String" DefaultValue="--"/>
<asp:ControlParameter ControlID="ddlAgeFrom" PropertyName="SelectedValue" Name="AgeFrom" Type="String" DefaultValue="0" />
<asp:ControlParameter ControlID="ddlAgeTo" PropertyName="SelectedValue" Name="AgeTo" Type="String" DefaultValue="0" />
<asp:ControlParameter ControlID="lbLevel" PropertyName="SelectedValue" Name="Level" Type="string" />
</SelectParameters>
</asp:SqlDataSource>
</td>
</tr>
</table>
&nbsp;
<asp:SqlDataSource ID="dsLevel" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [ID] = 0, [Name] = '--' UNION SELECT [ID], [Name] FROM [Level]"></asp:SqlDataSource>




Code Behind:

if(IsPostBack)
{
string level = "";

foreach (ListItem selectedItem in this.lbLevel.Items)
{
// Checks if the item in the ListBox is selected or not
if (selectedItem.Selected)
{
if (level != "")
{
level = level + ",'" + selectedItem.Value + "'";
}
else
{
level = "'" + selectedItem.Value + "'";
}
}
}

this.SqlDataSource1.SelectCommand =
"SELECT P.EmailFlag " +
" , P.Email " +
" , PID = P.ID " +
" , DOB = Convert(int, DATEDIFF(dd, P.DOB, GETDATE()) / 365.2425) " +
" , P.Comments " +
" , Height = CASE WHEN P.Height_Ft IS NOT NULL THEN CAST(P.Height_FT as varchar(3)) + ''' ' + CASE WHEN ISNULL(P.Height_In, 0) = 0 THEN '' ELSE CAST(P.Height_In as varchar(3)) + '''''' END ELSE '' END " +
" , LEVEL = L.[Name] " +
" , Side = S.[Name] " +
" , P.Gender " +
" , P.FirstName " +
" , P.LastName " +
" , SettingStyle = CASE WHEN Setting = 1 THEN 'Hand Setter' ELSE 'Bump Setter' END " +
" FROM Player P " +
" INNER JOIN LEVEL L ON L.ID = P.LEVEL " +
" INNER JOIN Side S ON S.ID = P.Side " +
" WHERE P.ID <> " + Session["PID"] +
" AND 1 = CASE WHEN @FirstName <> '%' THEN CASE WHEN P.FirstName LIKE @FirstName + '%' THEN 1 ELSE 0 END ELSE 1 END " +
" AND 1 = CASE WHEN @LastName <> '%' THEN CASE WHEN P.LastName LIKE @LastName + '%' THEN 1 ELSE 0 END ELSE 1 END " +
" AND 1 = CASE WHEN @Gender <> '--' THEN CASE WHEN P.Gender= @Gender THEN 1 ELSE 0 END ELSE 1 END " +
" AND 1 = CASE WHEN @AgeFrom <> 0 THEN CASE WHEN ISNULL(Convert(int, DATEDIFF(dd, P.DOB, GETDATE()) / 365.2425), 0) >= @AgeFrom THEN 1 ELSE 0 END ELSE 1 END " +
" AND 1 = CASE WHEN @AgeTo <> 0 THEN CASE WHEN ISNULL(Convert(int, DATEDIFF(dd, P.DOB, GETDATE()) / 365.2425), 0) <= @AgeTo THEN 1 ELSE 0 END ELSE 1 END" +
" AND 1 = CASE WHEN @Level <> '--' THEN CASE WHEN L.[Name] IN (" + level + ") THEN 1 ELSE 0 END ELSE 1 END ";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top