简体   繁体   English

使用下拉列表中的全选并在网格视图中显示

[英]Using select all in drop down list and display in grid view

I have two controls a textbox and drop down list to search the database.我有两个控件一个文本框和下拉列表来搜索数据库。 I have added all to the drop down list that will allow the user to see all the data in the database;我已将所有内容添加到允许用户查看数据库中所有数据的下拉列表中; if the textbox is empty or not empty to show the employee and their positions (relating to what was entered in the textbox).如果文本框为空或不为空以显示员工及其职位(与在文本框中输入的内容有关)。 What show be added to query?查询添加什么显示?

  protected void btnSearch_Click(object sender, EventArgs e)
  {
        string mainconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection sqlConn = new SqlConnection(mainconn);
        sqlConn.Open();
        SqlCommand com = new SqlCommand();
        string sqlquery = "  SELECT CONCAT(c.FIRSTNAME, ' ', c.LASTNAME) AS 'EmployeeName' , Position FROM[TWCL_OPERATIONS].[dbo].[PP_Employee] c where CONCAT(c.FIRSTNAME, ' ', c.LASTNAME)  LIKE '%' + @EmployeeName + '%' and  position like '%' + @position + '%' ";

        com.CommandText = sqlquery;
        com.Connection = sqlConn;

        com.Parameters.AddWithValue("@EmployeeName", txtEmployeeName.Text.Trim());

        com.Parameters.AddWithValue("@position", ddlPostion.SelectedItem.Text);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(com);
        sda.Fill(ds);

        if (ds.Tables[0].Rows.Count == 0)
        {                 
            Response.Write("<script language='javascript'>window.alert('No Records Found!');window.location='AddEmployee.aspx';</script>");
        }
        else
        {
            grdEmployee.DataSource = ds;
            grdEmployee.DataBind();
        }
    }

在网格中

You are passing the All text in to your query so in result its is looking for a position that is equal to All您将All文本传递给您的查询,因此它正在寻找一个等于All的位置

position like '%All%' 

You just need to add a condition in your btnSearch click event if the selected on ddlposition is equal to All then set the value on your parameter to a empty string.如果 ddlposition 上的选定项等于All,则只需在 btnSearch 单击事件中添加一个条件,然后将参数上的值设置为空字符串。

 protected void btnSearch_Click(object sender, EventArgs e)
      {
            string positionvalue =string.Empty;
            string mainconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(mainconn);
            sqlConn.Open();
            SqlCommand com = new SqlCommand();
            string sqlquery = "  SELECT CONCAT(c.FIRSTNAME, ' ', c.LASTNAME) AS 'EmployeeName' , Position FROM[TWCL_OPERATIONS].[dbo].[PP_Employee] c where CONCAT(c.FIRSTNAME, ' ', c.LASTNAME)  LIKE '%' + @EmployeeName + '%' and  position like '%' + @position + '%' ";

            com.CommandText = sqlquery;
            com.Connection = sqlConn;

            com.Parameters.AddWithValue("@EmployeeName", txtEmployeeName.Text.Trim());

            if(ddlPostion.SelectedItem.Text == "All"){
                 positionvalue  ="";
             }
            com.Parameters.AddWithValue("@position", positionvalue  );
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(com);
            sda.Fill(ds);

            if (ds.Tables[0].Rows.Count == 0)
            {                 
                Response.Write("<script language='javascript'>window.alert('No Records Found!');window.location='AddEmployee.aspx';</script>");
            }
            else
            {
                grdEmployee.DataSource = ds;
                grdEmployee.DataBind();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM