我一直在研究DataSource过滤,我的页面应用程序的复杂性正在让我的逻辑陷入困境。
我有一个显示来自数据源的3/17值的Gridview。 我想针对数据源在3个DropDowns和3个Checkbox上运行一个过滤器。 3个DDL在索引0处具有默认值String.Empty,并由页面加载(!IsPostBack)上的另一个DS填充。
我的问题:
此外,我不确定要接线的事件。
<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
EnableCaching="true"
DataSourceMode="DataSet"
ConnectionString="<%$ ConnectionStrings:inventory_v2ConnectionString %>"
SelectCommand="SELECT ID, OEMPartCode, PartCode2, UsedByOEM, ItemType, GroupType, PartCount, PartDesc, PartComment, PartMin, PartActive, MFRPartNumber, PartCapacity, PreTurnRequired, AssemblyPart, PartImage, PartImage2, NonInventoryPart FROM dbo.Part_Catalog"
FilterExpression="UsedByOEM = '{0}' AND ItemType = '{1}' AND GroupType = '{2}' OR (UsedByOEM = '{0}' OR ItemType = '{1}' OR GroupType = '{2}') OR (UsedByOEM = '{0}' OR ItemType = '{1}' AND GroupType = '{2}') OR (UsedByOEM = '{0}' AND ItemType = '{1}' OR GroupType = '{2}')">
<FilterParameters>
<asp:ControlParameter Name="UsedByOEM" ControlID="DDL_OEM" PropertyName="SelectedValue" />
<asp:ControlParameter Name="ItemType" ControlID="DDL_ItemTypes" PropertyName="SelectedValue" />
<asp:ControlParameter Name="GroupType" ControlID="DDL_GroupTypes" PropertyName="SelectedValue" />
</FilterParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="DDL_OEM" runat="server" AutoPostBack="True" AppendDataBoundItems="True"></asp:DropDownList>
<asp:DropDownList ID="DDL_ItemTypes" runat="server" AutoPostBack="True" AppendDataBoundItems="True"></asp:DropDownList>
<asp:DropDownList ID="DDL_GroupTypes" runat="server" AutoPostBack="True" AppendDataBoundItems="True"></asp:DropDownList>
<asp:CheckBox ID="CheckBox1" runat="server" Checked="True" />
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:CheckBox ID="CheckBox3" runat="server" Checked="True" />
protected void BindOEMs()
{
SqlConnection connectionString = new SqlConnection(ConfigurationManager.ConnectionStrings["inventory_v2ConnectionString"].ConnectionString);
connectionString.Open();
SqlCommand cmd = new SqlCommand("SELECT [Manufacturer], [ID] FROM [Models_OEMs] ORDER BY [Manufacturer]", connectionString);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
connectionString.Close();
DDL_OEM.DataSource = ds;
DDL_OEM.DataTextField = "Manufacturer";
DDL_OEM.DataValueField = "ID";
DDL_OEM.DataBind();
DDL_OEM.Items.Insert(0, new ListItem(String.Empty, "0"));
}
答案 0 :(得分:0)
现在想出来。我将下面的行添加到page_load(!isPostBack),因此首次加载时应用过滤器。
if (gvFilter.OEM == null || gvFilter.OEM == 0 ) { Filter_DataSet(null, null); }
使用我的类别创建自定义Filter对象:
public class Filters
{
private int oem;
private int item;
private int group;
private bool active;
private bool ninventoried;
private bool stocked;
public int OEM
{
set { oem = value; }
get { return oem; }
}
public int Item
{
set { item = value; }
get { return item; }
}
public int Group
{
set { group = value; }
get { return group; }
}
public bool Active
{
set { active = value; }
get { return active; }
}
public bool Ninventoried
{
set { ninventoried = value; }
get { return ninventoried; }
}
public bool Stocked
{
set { stocked = value; }
get { return stocked; }
}
}
然后我创建了一个方法将过滤器应用于数据源并将所有过滤器控制事件设置为此方法:
protected void Filter_DataSet(object sender, EventArgs e)
{
DetailsView1.Visible = false;
string FEString= String.Empty;
gvFilter.OEM = Convert.ToInt32(DDL_OEM.SelectedValue);
gvFilter.Item = Convert.ToInt32(DDL_ItemTypes.SelectedValue);
gvFilter.Group = Convert.ToInt32(DDL_GroupTypes.SelectedValue);
if (CheckBox1.Checked == true) { gvFilter.Active = true; } else { gvFilter.Active = false; }
if (CheckBox2.Checked == true) { gvFilter.Ninventoried = true; } else { gvFilter.Ninventoried = false; }
if (CheckBox3.Checked == true) { gvFilter.Stocked = true; } else { gvFilter.Stocked = false; }
if (gvFilter.OEM >0)
{
FEString += "UsedByOEM = " + gvFilter.OEM.ToString();
}
if (gvFilter.Item > 0)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "ItemType = " + gvFilter.Item.ToString();
}
if (gvFilter.Group > 0)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "GroupType = " + gvFilter.Group.ToString();
}
if (gvFilter.Active == true)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "PartActive = " + gvFilter.Active.ToString();
}
if (gvFilter.Active == false)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "PartActive = " + gvFilter.Active.ToString();
}
if (gvFilter.Ninventoried == true)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "NonInventoryPart = " + gvFilter.Ninventoried.ToString();
}
if (gvFilter.Ninventoried == false)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "NonInventoryPart = " + gvFilter.Ninventoried.ToString();
}
if (gvFilter.Stocked == true)
{
if (FEString.Length > 0) { FEString += " AND "; }
FEString += "PartCount > 0 ";
}
//if (gvFilter.Stocked == false)
//{
// if (FEString.Length > 0) { FEString += " AND "; }
// FEString += "PartCount <= 0 ";
//}
SqlDataSource1.FilterExpression = FEString;
}