帮助使用混合控制类型进行SQLDataSource过滤

本文关键字:SQLDataSource 过滤 类型 控制 混合 帮助 | 更新日期: 2023-09-27 18:14:49

我一直在研究数据源过滤,我的页面应用程序的复杂性使我的逻辑陷入困境。

我有一个Gridview显示3/17值从一个数据源。我想对数据源运行3下拉框和3复选框过滤器。这3个ddl的默认值为String。索引0为空,在页面加载时由另一个DS填充(!IsPostBack)。

我问题:

  • 当页面加载时,gridview中没有任何内容。应该跳过空的DDL值。
  • 最后一个复选框应该表示(Part_Catalog。PartCount> 0)
  • 目前过滤器是在数据源中定义的,但由于上述条件,我需要在cs文件中做一些处理。我不确定的是,我是否需要将整个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"));
    }            
    

帮助使用混合控制类型进行SQLDataSource过滤

现在明白了。我将下面的行添加到page_load (!isPostBack)中,以便过滤器在第一次加载时应用。

if (gvFilter.OEM == null || gvFilter.OEM == 0 ) { Filter_DataSet(null, null); }

用我的类别创建一个自定义过滤器对象:

 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;
}