RSS订阅优然探索
你的位置:首页 » 学习收藏 » 正文

C#技术学习之-DataReader到ListBox

选择字号: 超大 标准 发布时间:2008-9-28 9:19:5 | 作者:admin | 0个评论 | 人浏览

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
namespace Mirror.AutoWeb.HelpCSharp.DataBase
{
 /// <summary>
 /// StudyDataReaderListBox 的摘要说明。
 /// </summary>
 public class StudyDataReaderListBox : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Literal Literal1;
  protected System.Web.UI.WebControls.Literal ScalarLabel;
  protected System.Web.UI.WebControls.ListBox ReaderList;
  protected System.Web.UI.WebControls.Literal Literal3;
  protected System.Web.UI.WebControls.Button AddOneBtn;
  protected System.Web.UI.WebControls.Button AddAllBtn;
  protected System.Web.UI.WebControls.Button DelOneBtn;
  protected System.Web.UI.WebControls.Button DelAllBtn;
  protected System.Web.UI.WebControls.ListBox ReaderListOther;
  protected System.Web.UI.WebControls.Literal SuccessMessage;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  private readonly string connstr = ConfigurationSettings.AppSettings["MySQLConnString1"].ToString();
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(!Page.IsPostBack)
   {
    ScalarLabel.Text = ExecuteScalarMySqlCommand().ToString();
    ExecuteXmlReaderMySqlCommand();
   }
  }

  private object ExecuteScalarMySqlCommand()
  {
   string cmdText = "Select Count(*) From Role";
   SqlConnection MyConn = new SqlConnection(connstr);
   SqlCommand MyCmd = new SqlCommand(cmdText,MyConn);
   MyConn.Open();
   object objectInfo = MyCmd.ExecuteScalar();
   MyConn.Close();
   return objectInfo;

  }
  private void ExecuteXmlReaderMySqlCommand()
  {
   string cmdText = "Select * From Role";
   SqlConnection MyConn = new SqlConnection(connstr);
   SqlCommand MyCmd = new SqlCommand(cmdText,MyConn);
   MyConn.Open();
   System.Data.SqlClient.SqlDataReader MyDataReader =  MyCmd.ExecuteReader();
   this.ReaderList.Items.Clear();
   while(MyDataReader.Read())
   {
    ListItem List = new ListItem(MyDataReader["Role_Name"].ToString());
    //List.Attributes.Add("style","background-color:#EEEEEE");
    this.ReaderList.Items.Add(List);
    this.DropDownList1.Items.Add(List);
   }
   //this.ReaderList.Items[1].Attributes.Add("style","background-color:#EEEEEE");
   //this.DropDownList1.Items[1].Attributes.Add("style","background-color:#EEEEEE");
   int i;
   for(i=0;i<this.ReaderList.Items.Count;i++)
   {
    if((i%2)==0)
    {
     ReaderList.Items[i].Attributes.Add("style","background-color:#EEEEEE");
    }
   }
   MyConn.Close();

  }
  
  private void AddAndDeleteBtn_Command(object sender,System.Web.UI.WebControls.CommandEventArgs e)
  {
   string commandName = ((Button)sender).CommandName;
   //htm = htm&("命令:"+commandName);
   switch(commandName.ToLower())
   {
    case "addone":
    {
     if(this.ReaderList.SelectedIndex>-1)
     {
      this.ReaderListOther.Items.Add(this.ReaderList.SelectedItem);
      SuccessMessage.Visible = false;
     }
     else
     {
      SuccessMessage.Visible = true;
     }
     break;
    }
    case "addall":
    {
     this.ReaderListOther.Items.Clear();
     foreach(ListItem item in this.ReaderList.Items)
     {
      this.ReaderListOther.Items.Add(item);
     }
     break;
    }
    case "delone":
    {
     if(this.ReaderListOther.SelectedIndex>-1)
     {
      this.ReaderListOther.Items.Remove(this.ReaderListOther.SelectedItem);
      SuccessMessage.Visible = false;
     }
     else
     {
      SuccessMessage.Visible = true;
     }
     break;
    }
    case "delall":
    {
     this.ReaderListOther.Items.Clear();
     break;
    }
    default:break;

   }
   this.ReaderList.SelectedIndex = -1;
   this.ReaderListOther.SelectedIndex = -1;
    
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.AddAllBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(this.AddAndDeleteBtn_Command);
   this.DelAllBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(this.AddAndDeleteBtn_Command);
   this.DelOneBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(this.AddAndDeleteBtn_Command);
   this.AddOneBtn.Command += new System.Web.UI.WebControls.CommandEventHandler(this.AddAndDeleteBtn_Command);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

 }
}
 

标签:DataList  ListBox  C#  

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。