WebFrom常用的控件与筛选条件查询、Repeater常用的四种绑定方式

WebFrom中的服务器控件有时需要自动提交到服务器处理数据,所以要把AutoPostBack属性设置为True 为了防止每次提交回来刷新页面导致数据重新绑定,所以页面加载事件里面要加 if (!IsPostBack)来判断

1.DropDownList:下拉列表控件  显示数据:     

//指定数据源      

   DropDownList1.DataSource = context.Nation;     

    DropDownList1.DataTextField = "Name";      

   DropDownList1.DataValueField = "Code";

        //绑定         DropDownList1.DataBind();

 

 

 取选中项的值:   DropDownList1.SelectedValue.ToString();

 设置哪一项被选中:    DropDownList1.SelectedIndex = 2;

2.ListBox:  属性:selectionmode:选中模式  显示数据:   

ListBox1.DataSource = context.Nation;   

ListBox1.DataTextField = "Name";   

ListBox1.DataValueField = "Code";  

  ListBox1.DataBind();

 

 

取选中项的值:   ListBox1.SelectedValue.ToString();

    

单选框:RadioButton  

属性:GroupName:组名,如果要实现单选效果,每个单选按钮的组名必须一样  看一下该按钮是否选中:RadioButton1.Checked;

 

单选按钮列表:

RadioButtonList  属性:RepeatDirection:横向或纵向  绑定数据:  

 

RadioButtonList1.DataSource = context.Nation;       

  RadioButtonList1.DataTextField = "Name";      

   RadioButtonList1.DataValueField = "Code";    

     RadioButtonList1.DataBind();

 

 取选中项的值:   RadioButtonList1.SelectedValue.ToString();  

设置哪一项被选中:   RadioButtonList1.SelectedIndex = 2;

3.CheckBox:复选框,Checked属性:是否选中

 看一下该按钮是否选中:checkbox1.Checked;

 

4.CheckBoxList:复选框列表

属性RepeatDirection:横向或纵向

 显示数据:   CheckBoxList1.DataSource = context.Nation;     

    

CheckBoxList1.DataTextField = "Name";     

    CheckBoxList1.DataValueField = "Code";      

   CheckBoxList1.DataBind();  //取选中项的值:  

  foreach (ListItem item in CheckBoxList1.Items)
        {
            if (item.Selected)
            {
                Label1.Text += item.Text;
            }
        }

 


 设置哪项选中:
  如果设置一项选中:SelectedIndex = 2;
  如果设置多项选中:foreach()

 

5.RadioButton:单选按钮,属性GroupName组名,同一个组名下的单选按钮产生互斥效果

6.RadioButtonList:单选按钮列表:  显示数据:

   

RadioButtonList1.DataSource = context.Nation;   

        RadioButtonList1.DataTextField = "Name";   

        RadioButtonList1.DataValueField = "Code";        

   RadioButtonList1.DataBind(); // 取选中项的值:

  RadioButtonList1.SelectedValue.ToString();  

//设置选中项:   RadioButtonList1.SelectedIndex = 2;

 

 树状图TreeView控件:

TreeNode a = new TreeNode("aa"); //根节点
        TreeView1.Nodes.Add(a);
        TreeNode b = new TreeNode("bb");//子节点
        TreeNode c = new TreeNode("cc");
        TreeNode d = new TreeNode("dd");
        a.ChildNodes.Add(b);
        a.ChildNodes.Add(c);
        a.ChildNodes.Add(d);
       

 

 

 

 

筛选条件查询:

        

    //方式一             //var query = context.House;

            //foreach (House data in query)         

    //{            

//    ListItem item = new ListItem();         

    //    item.Text = data.Area;         

    //    if (!CheckBoxList1.Items.Contains(item))     

        //    {         

    //        CheckBoxList1.Items.Add(item);        

     //    }        

     //}

            //方式二:

            List<string> list = context.House.Select(p=>p.Area).Distinct().ToList();

            foreach (string text in list)          

   {            

     ListItem item = new ListItem();      

           item.Text = text;

                CheckBoxList1.Items.Add(item);     

        }

            List<string> listR = context.House.Select(p => p.RentType).Distinct().ToList();

            foreach (string text in listR)     

        {     

            ListItem item = new ListItem();    

             item.Text = text;

                CheckBoxList2.Items.Add(item);       

      }

            List<string> listH = context.House.Select(p => p.HouseType).Distinct().ToList();

            foreach (string text in listH)           

  {         

        ListItem item = new ListItem();       

          item.Text = text;

                CheckBoxList3.Items.Add(item);   

          }

        }   

  }   

  protected void Button1_Click(object sender, EventArgs e)   

  {       

  Response.Redirect("Insert.aspx");    

}    

protected void ckQuAll_CheckedChanged(object sender, EventArgs e)   

  {   

      foreach (ListItem item in CheckBoxList1.Items)   

      {           

  item.Selected = ckQuAll.Checked;     

    }    

}   

  protected void ckZuAll0_CheckedChanged(object sender, EventArgs e)  

   {     

    foreach (ListItem item in CheckBoxList2.Items)    

     {       

      item.Selected = ckZuAll0.Checked;    

     }

    }  

   protected void ckFangAll1_CheckedChanged(object sender, EventArgs e)   

  {     

    foreach (ListItem item in CheckBoxList3.Items)       

  {          

   item.Selected = ckFangAll1.Checked;     

    }    

}  

   protected void Button2_Click(object sender, EventArgs e)   

  {    

     TestDataContext context = new TestDataContext();     

    List<House> list = context.House.ToList();

        ArrayList listArea = new ArrayList();      

   ArrayList listZu = new ArrayList();   

      ArrayList listHouse = new ArrayList();

        //区域筛选     

    if (CheckBoxList1.SelectedIndex >= 0 && !ckQuAll.Checked)       

  {          

   foreach (ListItem item in CheckBoxList1.Items)      

       {              

   if (item.Selected)               

  {            

         listArea.Add(item.Text);       

          }           

  }         

    list = list.Where(p=>listArea.Contains(p.Area)).ToList();    

       }        

//租赁类型筛选        

if (CheckBoxList2.SelectedIndex >= 0 && !ckZuAll0.Checked)     

    {        

     foreach (ListItem item in CheckBoxList2.Items)          

   {         

        if (item.Selected)          

       {             

        listZu.Add(item.Text);          

       }         

    }          

   list = list.Where(p => listZu.Contains(p.RentType)).ToList();         

  }     

    //房屋类型筛选    

     if (CheckBoxList3.SelectedIndex >= 0 && !ckFangAll1.Checked)    

     {      

       foreach (ListItem item in CheckBoxList3.Items)     

        {        

         if (item.Selected)          

       {              

       listHouse.Add(item.Text);           

      }        

     }          

   list = list.Where(p => listHouse.Contains(p.HouseType)).ToList();   

      }

 

 

 

 

 

 

 

 

 

1.Repeater:
 网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?>
 绑定数据的四种方式:
  1.直接绑定 <%#Eval("Code") %>
  2.调用函数 <%#ShowSex()%>
  3.显示外键关系列 <%#Eval("Nation1.Name") %>
  4.格式化显示 <%#Eval("Birthday","{0:yyyy年MM月dd日}") %>

 

需要绑定数据后才能显示数据:

DataClassesDataContext context = new DataClassesDataContext();

        Repeater1.DataSource = context.Car;         Repeater1.DataBind();

  <HeaderTemplate> 头模板

 <ItemTemplate>像模板

<FooterTemplate>脚模板

<%#ShowSex()%>转换Sex为男女:

<%#ShowNation()%>,转换民族为字符:

转换日期:

 

posted @ 2015-10-08 10:37  疯子霖  阅读(1104)  评论(0编辑  收藏  举报