• 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>
  • 相关阅读:
    ArcGis面要素空间连接,取相交面积最大者 C#
    迅雷下载器无限制版_无敏感_无限速
    redhat 6.8 配置 centos6 163 的 yum 源
    apache cgi 程序: End of script output before headers
    centos php 安装 decrypt
    url传输中+转为%2B取到变空格的问题
    快速搭建自己的搜索引擎
    ffmpeg 文件推流 rtsp和rtmp
    svn 服务器操作
    edusoho迁移
  • 原文地址:https://www.cnblogs.com/joean/p/4415187.html
Copyright © 2020-2023  润新知