Exemplo n.º 1
0
 public void Search(bool savePage = false, int selectID = -1)
 {
     pagerWidget.ClearCondition();
     pagerWidget.ClearOrderBy();
     if (comboBoxSearchCondition.SelectedIndex != 0)
     {
         string name = comboBoxSearchCondition.SelectedItem.ToString();
         string key  = (from kn in this.keyNames where kn.Name == name select kn.Key).FirstOrDefault();
         if (key == null)
         {
             throw new Exception("SearchWidget找不到字段 " + name + " 对应的Key,请检查传入的KeyNames");
         }
         //判断搜索字段类型
         PropertyInfo property = typeof(T).GetProperty(key);
         if (property == null)
         {
             throw new Exception(typeof(T).Name + " 中不存在字段 " + key + " 请检查程序!");
         }
         //如果是日期类型,按模糊搜索
         if (property.PropertyType == typeof(DateTime) || (property.PropertyType == typeof(DateTime?)))
         {
             pagerWidget.AddCondition(string.Format("DATEDIFF(day,@value,{0})=0", key), new SqlParameter("value", textBoxSearchCondition.Text));
         }
         else //否则按普通搜索
         {
             pagerWidget.AddCondition(key, textBoxSearchCondition.Text);
         }
     }
     if (comboBoxOrderByCondition.SelectedIndex != 0)
     {
         string name = comboBoxOrderByCondition.SelectedItem.ToString();
         string key  = (from kn in this.keyNames where kn.Name == name select kn.Key).FirstOrDefault();
         if (key == null)
         {
             throw new Exception("SearchWidget找不到字段 " + name + " 对应的Key,请检查传入的KeyNames");
         }
         //判断搜索字段类型
         PropertyInfo property = typeof(T).GetProperty(key);
         if (property == null)
         {
             throw new Exception(typeof(T).Name + " 中不存在字段 " + key + " 请检查程序!");
         }
         string orderByCond = key;
         if (this.comboBoxOrderByOrder.SelectedIndex == 1) //倒序
         {
             orderByCond += " DESC";
         }
         pagerWidget.AddOrderBy(orderByCond);
     }
     pagerWidget.Search(savePage, selectID);
 }