2012-12-13 59 views
0

我想在單個按鈕中給出多個選擇命令。 示例: 按日期搜索,名稱明智,在單個節目中按產品明智搜索。 但是,這種搜索可以是任意的,只要名稱明智搜索就可以完成 或名稱和產品明智的搜索可以完成。 但這應該只在一個單一的顯示按鈕.. PLZ幫我一個示例代碼..單擊按鈕單擊不同的選擇命令

回答

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(); 
        } 
} 
0

如果你想基於一個過濾器,你最好使用一個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;} 

我希望它能幫助。

+0

我sajanyamaha同意,這是一個邏輯疑問,這就是爲什麼我沒有在我的答案中使用SQL或asp.net代碼,你需要知道你想做什麼,然後就沒有區別無論您使用數據庫還是.txt文件,Windows應用程序或asp.net Web應用程序,都需要知道您想要什麼。 –

+0

謝謝..我會嘗試所有.. – sathya

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

如果你要在SQL中使用'string where'那麼你需要檢查sql注入。 – Greg

+0

我用參數改變了where子句 –