单击按钮中的不同选择命令

时间:2012-12-13 04:42:04

标签: c# asp.net sql-server

我试图在单个按钮中提供多个选择命令。  例:   日期搜索,名称方式,产品智能搜索在单个显示按钮。   但是这种搜索可以是随机的,就像单独使用名称搜索一样   或名称和产品明智的搜索可以完成。   但这应该只在一个显示按钮中完成..   Plz帮我一个示例代码..

3 个答案:

答案 0 :(得分: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();
                    }
 }

答案 1 :(得分: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;}

我希望它有所帮助。

答案 2 :(得分: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;
        }
    }