• ListBox,CheckBoxList,DropDownList,RadioButtonList的常见数据绑定


     

    ListBox,CheckBoxList,DropDownList,RadioButtonList的常见用法
    四个都是选择控件,用法大同小异,基本都是指定键值对:
    直接加选择项:
     
     void way1()
       {
        DropDownList1.Items.Add("c#");
        DropDownList1.Items.Add("vb");
        DropDownList1.Items.Add("c");
        DropDownList1.Items.Add("java");
        ListBox1.Items.Add("c#");
        ListBox1.Items.Add("VB");
        ListBox1.Items.Add("C++");
        ListBox1.Items.Add("JAVA");
        
        CheckBoxList1.Items.Add("c#");
        CheckBoxList1.Items.Add("VB");
        CheckBoxList1.Items.Add("javascript");
        CheckBoxList1.Items.Add("php");
        RadioButtonList1.Items.Add("asp");
        RadioButtonList1.Items.Add("php");
        RadioButtonList1.Items.Add("jsp");
        RadioButtonList1.Items.Add("ajax");
       }
    哈斯表法:
    void way2()
       {
        Hashtable ht=new Hashtable();
        ht.Add("1","c#");
        ht.Add("2","C++");
        ht.Add("3","php");
        ht.Add("4","CGI");
        this.DropDownList2.DataSource=ht;
        DropDownList2.DataTextField="Value";
        DropDownList2.DataValueField="Key";
        DropDownList2.DataBind();
        this.ListBox2.DataSource=ht;
        ListBox2.DataTextField="value";
        ListBox2.DataValueField="key";
        ListBox2.DataBind();
        this.CheckBoxList2.DataSource=ht;
        CheckBoxList2.DataTextField="value";
        CheckBoxList2.DataValueField="key";
        CheckBoxList2.DataBind();
        this.RadioButtonList2.DataSource=ht;
        RadioButtonList2.DataTextField="value";
        RadioButtonList2.DataValueField="key";
        RadioButtonList2.DataBind();
       }
    SortedList方法:
       void way3()
       {
        SortedList s1=new SortedList();
        s1.Add("1","haha");
        s1.Add("2","hehe");
        s1.Add("3","xixi");
        s1.Add("4","heihei");
        this.DropDownList3.DataSource=s1;
        DropDownList3.DataTextField="value";
        DropDownList3.DataValueField="key";
        DropDownList3.DataBind();
       }
    用数据对象dataread方法:
    void way4()
       {
        OleDbConnection cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("sailing.mdb")+";Persist Security Info=False");
        cn.Open();
        OleDbCommand cmd=new OleDbCommand();
        cmd.Connection=cn;
        cmd.CommandText="select RoleName,RoleID from Role";
        OleDbDataReader dr=cmd.ExecuteReader();
        while(dr.Read())
        {
         ListItem li=new ListItem(dr[0].ToString(),dr[1].ToString());
         this.ListBox4.Items.Add(li);
        }
        dr.Close();
        cn.Close();
       }
    用数据集法:
         OleDbConnection cn=new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("sailing.mdb")+";Persist Security Info=False");
         cn.Open();
         OleDbCommand cmd=new OleDbCommand();
         cmd.Connection=cn;
         cmd.CommandText="select RoleName,RoleID from Role";
         OleDbDataAdapter adp=new OleDbDataAdapter(cmd);
         DataSet ds=new DataSet();
         adp.Fill(ds);
    
         this.CheckBoxList4.DataSource=ds.Tables[0];
         this.CheckBoxList4.DataTextField=ds.Tables[0].Columns[0].ToString();
         this.CheckBoxList4.DataValueField=ds.Tables[0].Columns[1].ToString();
         this.CheckBoxList4.DataBind();
    
    还有一种数组法:
    
    <%@ Page Language="C#" AutoEventWireup="True" %>
    <Script language="C#" runat="server">
    void Page_Load(Object Sender,EventArgs e){
    ArrayList array=new ArrayList();
    array.Add("Windows");
    array.Add("Linux");
    array.Add("unix");
    CheckBox_1.DataSource=array;
    CheckBox_1.DataBind();
    RadioList_1.DataSource=array;
    RadioList_1.DataBind();
    }
    </Script>
    <form id="Form_1" runat="server">
    <ASP:checkboxList ID="CheckBox_1" runat="server"/>
    <input id="Button_1" type="Button" value="CheckButton" runat="server"/>
    <asp:radiobuttonlist ID="RadioList_1" runat="server"/>
    <input id="Button_2" type="Button" value="RadioButton" runat="server"/>
    </form>
  • 相关阅读:
    replace函数的使用(替换单个和全局)
    数据库记录删除方式
    UDP接收百万级数据的解决方案
    早睡早起+微信朋友圈控制在10人以内…
    java Date型时间比较大小
    教会你时间管理的奥秘,提升工作效率。
    大多数人都输给了这个字——等
    二分法查找和普通查找
    王者荣耀里拿个王者有啥了不起,有胆就来挑战一下ApsaraCache源码
    给予Java初学者的学习路线建议
  • 原文地址:https://www.cnblogs.com/joean/p/4415187.html
Copyright © 2020-2023  润新知