0

我正在尝试在单个按钮中提供多个选择命令。示例:在单个显示按钮中按日期搜索、按名称搜索、按产品搜索。但是这种搜索可以是随机的,例如可以单独进行名称搜索,也可以进行名称和产品搜索。但这应该只在一个显示按钮中完成..请帮助我提供示例代码..

4

3 回答 3

0

这不是编码问题,这更可能是逻辑问题,为此您有很多选择,您可以执行一些 SQL 查询连接或一些If..else...elseIf语句,如下所示:

protected void btnSave_Click(object sender, EventArgs e)
{

                    if (ddpcustomer.SelectedIndex == 0 && ddpproperty.SelectedIndex == 0)
                    {
                        DataSet ds1 = Tbl_MasterBooking.getDetailsforBookingSummaryWithoutStatusCheckinDate(inputField.Text, inputField1.Text, userId, Cid);
                        GVBookingSummary.DataSource = ds1;
                        GVBookingSummary.DataBind();
                        GridView1.DataSource = ds1;
                        GridView1.DataBind();
                    }
                    else if (ddpcustomer.SelectedIndex != 0 && ddpproperty.SelectedIndex == 0)
                    {
                        DataSet ds1 = Tbl_MasterBooking.getDetailsforBookingSummaryWithoutStatusWithCustNameCheckinDate(inputField.Text, inputField1.Text, userId, Cid,ddpcustomer.SelectedItem.Text);
                        GVBookingSummary.DataSource = ds1;
                        GVBookingSummary.DataBind();
                        GridView1.DataSource = ds1;
                        GridView1.DataBind();
                    }
                    else if (ddpcustomer.SelectedIndex != 0 )
                    {
                        DataSet ds1 = Tbl_MasterBooking.getDetailsforBookingSummaryWithoutStatusWithPnameCheckinDate(inputField.Text, inputField1.Text, userId, Cid, ddpproperty.SelectedItem.Text);
                        GVBookingSummary.DataSource = ds1;
                        GVBookingSummary.DataBind();
                        GridView1.DataSource = ds1;
                        GridView1.DataBind();
                    }
                    else if (ddpcustomer.SelectedIndex != 0 && ddpproperty.SelectedIndex != 0)
                    {
                        DataSet ds1 = Tbl_MasterBooking.getDetailsforBookingSummaryWithoutStatusWithPnameandCustNameCheckinDate(inputField.Text, inputField1.Text, userId, Cid, ddpproperty.SelectedItem.Text, ddpcustomer.SelectedItem.Text);
                        GVBookingSummary.DataSource = ds1;
                        GVBookingSummary.DataBind();
                        GridView1.DataSource = ds1;
                        GridView1.DataBind();
                    }
 }
于 2012-12-13T04:58:26.917 回答
0

如果您想基于 One 过滤器进行搜索,您最好使用 DropDownList 并根据所选值进行搜索或...但如果您怀疑在多个数据集合中搜索,我将在下面为您提供代码片段:

    private class Product 
    {
        public string Name;
        public double Price;
        public string ToString()
        {
            return Name + " : " + Price.ToString() + "$";
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {
        List<string> names = new List<string>();
        names.Add("Jack");
        names.Add("John");
        names.Add("Nick");
        names.Add("Rock");

        List<Product> products = new List<Product>();
        products.Add(new Product { Name = "Laptop", Price = 1000 });
        products.Add(new Product { Name = "Tablet", Price = 750 });
        products.Add(new Product { Name = "Rock", Price = 1 });

        List<DateTime> dates = new List<DateTime>();
        dates.Add(DateTime.Now.AddDays(-1));
        dates.Add(DateTime.Now);
        dates.Add(DateTime.Now.AddDays(1));

        lblOutput.Text = "";

        foreach (string name in names)
        {
            if (name == txtSearch.Text)
                lblOutput.Text += name + "[Name] ";
        }
        foreach (Product product in products)
        {
            if (product.Name == txtSearch.Text)
                lblOutput.Text += product.ToString();
        }
        foreach (DateTime date in dates)
        {
            DateTime dt;
            if (DateTime.TryParse(txtSearch.Text, out dt))
                if (date == dt)
                    lblOutput.Text = date.Date.ToShortDateString();
        }
    }

此外,如果您想在找到匹配项后立即停止搜索,您可以return在每个找到匹配项的代码块之后放置一个,例如:

if (name == txtSearch.Text)
    { lblOutput.Text += name + "[Name] "; return;}

我希望它有所帮助。

于 2012-12-13T05:13:33.860 回答
0

我希望这样做。您可以设置您喜欢的选项。

    public class Parameters
    {

        //Properties.
        public string CompanyName
        {
            get;set;
        }

        public string ProductName
        {
            get;set;
        }

        public string Design
        {
            get;set;
        }

        public string Size
        {
            get;set;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Parameters para = new Parameters();
        if (cbxCompanyName.Text.Trim().Length != 0)
        {
            para.CompanyName = "'" + this.cbxCompanyName.Text + "'";

        }
        if (cbxProductName.Text.Trim().Length != 0)
        {
            para.ProductName = "'" + this.cbxProductName.Text + "'";

        }
        if (cbxDesign.Text.Trim().Length != 0)
        {
            para.Design = "'" + this.cbxDesign.Text + "'";

        }
    }

    public void test(Parameters paras)
    {
        try
        {
            con = new SqlConnection(source);
            con.Open();

            string select;


            select = "SPGetSaleRegCulture ";

            DataSet ds = new DataSet();

            cmd = new SqlCommand(select, con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Company", paras.CompanyName);
            cmd.Parameters.AddWithValue("@Product", paras.ProductName);
            cmd.Parameters.AddWithValue("@Design", paras.Design);
            cmd.Parameters.AddWithValue("@Size", paras.Size);

            da.SelectCommand = cmd;

            da.Fill(ds, "SaleRegister");


        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
于 2012-12-13T05:42:41.340 回答