使用DropDown过滤DataGrid

时间:2011-02-25 23:27:47

标签: c# asp.net

我正在尝试使用updatepanel通过CompanyID过滤我的数据绑定数据网格视图。由于某种原因,它不起作用。这是我的代码。谢谢你的帮助!

C#

string company_id;

protected void Page_Load(object sender, EventArgs e)
{

        SqlCommand cmd = new SqlCommand("SELECT CompanyName, CompanyID FROM Company ORDER BY CompanyName", conn);
        SqlCommand cmd2 = new SqlCommand("SELECT p.ProjectName AS ProjectName, p.ProjectID, p.CompanyID, p.Status AS Status FROM Project p, Company c WHERE p.CompanyID = c.CompanyID AND c.CompanyID = '" + company_id + "' ORDER BY ProjectName", conn);

        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        if (!Page.IsPostBack)
        {
            company_list.DataSource = ds;
            company_list.DataTextField = "CompanyName";
            company_list.DataValueField = "CompanyID";
            company_list.DataBind();

            company_list.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-- Please Select Company --"));


        //cmd2.Connection.Open();

            cmd2.CommandType = CommandType.Text;

            SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd2);
            DataSet ds2 = new DataSet();
            sqlAdapter.Fill(ds2);

            Gridview1.DataSource = ds2;
            Gridview1.DataBind();

            conn.Close();
        }

        //cmd2.Connection.Close();
        //cmd2.Connection.Dispose();
    }

}
protected void company_list_SelectedIndexChanged(object sender, EventArgs e)
{

    company_id = company_list.SelectedValue;

}

ASP.NET:

<asp:DropDownList ID="company_list" runat="server" 
                             onselectedindexchanged="company_list_SelectedIndexChanged" width="175" AutoPostBack="true" />

...

<asp:UpdatePanel ID="UpdateGrid" runat="server">
                <ContentTemplate>
                           <asp:gridview ID="Gridview1" runat="server" ShowFooter="True"
                       AutoGenerateColumns="False" GridLines="None">
....

                </ContentTemplate>
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="company_list" />
                </Triggers>
 </asp:UpdatePanel>

2 个答案:

答案 0 :(得分:0)

只有在!IsPostback中并且在用户从SelectedIndexChanged-Eventhandler中的DropDownlist而不是从Page_Load中选择公司后重新绑定网格时,才应该在page_load中绑定网格。所有事件处理程序都在page_load之后调用,因此您的数据库查询太早了。

Page Lifecycle

答案 1 :(得分:0)

您的代码需要遵循此模式(使用本地数据源而不是db调用):

public partial class _default : System.Web.UI.Page
{
    ICollection<Company> companies = new List<Company>()
        {
            new Company(1, "company1"),
            new Company(2, "company2"),
            new Company(3, "company3")
        };

    ICollection<Project> projects = new List<Project>()
        {
            new Project(1, "project1a", 1),
            new Project(2, "project2a", 2),
            new Project(3, "project3a", 3),
            new Project(4, "project1b", 1),
            new Project(5, "project2b", 2),
            new Project(6, "project3b", 3),
        };

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            company_list.DataSource = companies;
            company_list.DataTextField = "Name";
            company_list.DataValueField = "Id";
            company_list.DataBind();
            company_list.Items.Insert(0, new System.Web.UI.WebControls.ListItem("-- Please Select Company --"));
        }
    }

    protected void company_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        Gridview1.DataSource = projects.Where(x => x.CompanyId == Int32.Parse(ddl.SelectedValue));
        Gridview1.DataBind();
    }
}

需要注意的事项:

  • 您当前的代码容易受到SQL注入攻击 - 您应该参数化SQL查询而不是连接字符串。