C#中的listbox的和lable的结合使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011046042/article/details/85224682
  • 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class  alerm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("in  ");
            ds.Tables["in "].Columns.Add("No",typeof(int));
            ds.Tables["in "].Columns.Add("name", typeof(string));
            ds.Tables["in  "].Columns.Add("data", typeof(int));
            ds.Tables["in "].Rows.Add(new object[]{1,"张三",200});
            ds.Tables["in "].Rows.Add(new object[]{2,"张2",200});
            ds.Tables["in "].Rows.Add(new object[]{3,"张a",200});
            ds.Tables["in "].Rows.Add(new object[]{4,"张b",200});
            ds.Tables["in "].Rows.Add(new object[]{5,"张i",200});

            this.ListBox1.DataSource = ds.Tables["in "];
            this.ListBox1.DataValueField = "No";
            this.ListBox1.DataTextField = "name";
            this.ListBox1.DataBind();
        }
    }
    protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
         this.Label1.Text = "你选择的是设备是:" + this.ListBox1.SelectedValue.ToString() + "名字是:" + this.ListBox1.SelectedItem.Text.ToString(); 
    }
    
}
  • 界面如下:
    在这里插入图片描述
  • 执行的效果如下:
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u011046042/article/details/85224682