随笔-68  评论-89  文章-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;
 
namespace student
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class allselect : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Panel Panel1;
  protected System.Web.UI.WebControls.Label Label1;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.DropDownList DropDownList2;
  protected System.Web.UI.WebControls.Label Label4;
  protected System.Web.UI.WebControls.TextBox TextBox1;
  protected System.Web.UI.WebControls.Label Label5;
  protected System.Web.UI.WebControls.TextBox TextBox2;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.TextBox TextBox3;
  protected System.Web.UI.HtmlControls.HtmlForm Form1;
  protected System.Web.UI.WebControls.DropDownList DropDownList3;
  protected System.Web.UI.WebControls.Label Label7;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Button chaxun;
  protected System.Web.UI.WebControls.Label Label9;
  protected System.Web.UI.WebControls.DropDownList DropDownList4;
  protected System.Web.UI.WebControls.Button bAllDelete;
  protected System.Web.UI.WebControls.Button btnMIME;
  protected System.Web.UI.WebControls.Button bAllUpdate;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.DropDownList nian;
  protected System.Web.UI.WebControls.DropDownList yue;
  protected System.Web.UI.WebControls.DropDownList ri;
  protected System.Web.UI.WebControls.DropDownList DropDownList7;
  protected System.Web.UI.WebControls.Label Label8;
  protected System.Web.UI.WebControls.DropDownList DropDownList5;
  protected System.Web.UI.WebControls.DropDownList DropDownList6;
  
 
  string strconn = System.Configuration.ConfigurationSettings.AppSettings["testdb"];//从Web.config中读取
       

  private void Page_Load(object sender, System.EventArgs e)
  {
   
   bAllDelete.Attributes.Add("onclick", "return confirm('您真的要删除所选项吗?');");
   bAllUpdate.Attributes.Add("onclick","return confirm('您真的要更新所选项吗?')");
   if(!Page.IsPostBack)
   {
       BindData();
    getDep();
    getZy();
    bind();
   }
  }

  

  public void bind()
  {
   string date=System.DateTime.Today.ToShortDateString();
   

   if(date.Length.Equals("8") || date.Length==8)
   {
    nian.SelectedValue=date.Substring(0,4);
    yue.SelectedValue=date.Substring(5,1);
    ri.SelectedValue=date.Substring(7);
    DropDownList7.SelectedValue=date.Substring(0,4);
    DropDownList6.SelectedValue=date.Substring(5,1);
    DropDownList5.SelectedValue=date.Substring(7);
   }
   else if(date.Length.Equals("9") || date.Length==9)
   {

    if(date.IndexOf("-",5)==7)
    {
     nian.SelectedValue=date.Substring(0,4);
     yue.SelectedValue=date.Substring(5,2);
     ri.SelectedValue=date.Substring(8);
     DropDownList7.SelectedValue=date.Substring(0,4);
     DropDownList6.SelectedValue=date.Substring(5,2);
     DropDownList5.SelectedValue=date.Substring(8);
    }
    else
    {
     nian.SelectedValue=date.Substring(0,4);
     yue.SelectedValue=date.Substring(5,1);
     ri.SelectedValue=date.Substring(7);
     DropDownList7.SelectedValue=date.Substring(0,4);
     DropDownList6.SelectedValue=date.Substring(5,1);
     DropDownList5.SelectedValue=date.Substring(7);
    }
   }
   else if(date.Length.Equals("10") || date.Length==10)
   {
    nian.SelectedValue=date.Substring(0,4);
    yue.SelectedValue=date.Substring(5,2);
    ri.SelectedValue=date.Substring(8);
    DropDownList7.SelectedValue=date.Substring(0,4);
    DropDownList6.SelectedValue=date.Substring(5,2);
    DropDownList5.SelectedValue=date.Substring(8);
   }
  }


  private void BindData()
  {
   SqlConnection con = new SqlConnection(strconn);
   SqlDataAdapter da = new SqlDataAdapter("select Bjbh,Bjjc,Bjrs,Yxmc,Zymc,Rxnf from Class INNER JOIN Major on Class.Zybh=Major.Zybh inner join Department on Department.Yxbh=Major.Yxbh order by  Rxnf desc",con);
   DataSet ds = new DataSet();
   da.Fill(ds,"Class");
   this.DataGrid1.DataSource = ds.Tables["Class"].DefaultView;
   DataGrid1.DataBind();

  }
  
  public void getDep()
  {
   SqlConnection con = new SqlConnection(strconn);
   SqlDataAdapter da = new SqlDataAdapter("select * from Department",con);
   DataSet ds=new DataSet();
   da.Fill(ds,"Dep");

   DataTable table =new DataTable();
   table=ds.Tables[0];
   
   for (int i=0; i<table.Rows.Count;i++ )
   {
    this.DropDownList1.Items.Add((string)table.Rows[i]["Yxmc"].ToString().Trim());
    this.DropDownList4.Items.Add((string)table.Rows[i]["Yxmc"].ToString().Trim());
   }
   DropDownList1.DataBind();
   con.Close();
  }

  public void getMajor(string str)
  { 
   SqlConnection con = new SqlConnection(strconn);
  
   SqlDataAdapter da1=new SqlDataAdapter("select * from Department where Yxmc='"+str.ToString().Trim()+"'",con);
   DataSet ds1=new DataSet();
   da1.Fill(ds1,"depid");
   DataTable dt=new DataTable();
   dt=ds1.Tables["depid"];
   string Yxbh"].ToString();
   
   SqlDataAdapter da2 = new SqlDataAdapter("select * from Major where Yxbh='"+id.Trim()+"'",con);
   DataSet ds2=new DataSet();
   da2.Fill(ds2,"Major");

   DataTable table =new DataTable();
   table=ds2.Tables[0];
   
   for (int i=0; i<table.Rows.Count;i++ )
   {
    this.DropDownList2.Items.Add((string)table.Rows[i]["Zymc"].ToString().Trim());
   }
   DropDownList2.DataBind();
   con.Close();
  }

  public void getZy()
  { 
   SqlConnection con = new SqlConnection(strconn);
   
   SqlDataAdapter da = new SqlDataAdapter("select * from Major",con);
   DataSet ds=new DataSet();
   da.Fill(ds,"Major");

   DataTable table =new DataTable();
   table=ds.Tables[0];
   
   for (int i=0; i<table.Rows.Count;i++ )
   {
    this.DropDownList2.Items.Add((string)table.Rows[i]["Zymc"].ToString().Trim());
   }
   DropDownList2.DataBind();
   con.Close();
  }

  ////////////////////////////////////////////////////////////
  //说明:执行制定SQL语句/////////////////////////////////////
  ///////////////////////////////////////////////////////////
  private void ExecuteSql(string strSql)
  {
   try
   {
    SqlConnection conn =new SqlConnection(strconn);
    SqlCommand com = new SqlCommand(strSql,conn);
    conn.Open();
    com.ExecuteNonQuery();
    conn.Close();
   }
   catch(Exception e)
   {
    Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
       
   }
  }

  public void CheckAll(object sender, System.EventArgs e)
  {
   CheckBox cbAll = (CheckBox)sender;
   if(cbAll.Text=="全选")
   {
    foreach(DataGridItem dgi in DataGrid1.Items)
    {
     CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
     cb.Checked = cbAll.Checked;
    }
   }
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
   this.bAllDelete.Click += new System.EventHandler(this.bAllDelete_Click);
   this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
   this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
   this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
   this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
   this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
   this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
   this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
   this.bAllUpdate.Click += new System.EventHandler(this.bAllUpdate_Click);
   this.btnMIME.Click += new System.EventHandler(this.btnMIME_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   DataGrid1.CurrentPageIndex = e.NewPageIndex;
   BindData();
  }

  private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex = -1;
   BindData();
  }

  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)// 删除操作
  {
   if(DataGrid1.Items.Count==1)
   {
    if(DataGrid1.CurrentPageIndex!=0)
     DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1;
   }
   string strSql = "delete from Class where Bjbh='"+e.Item.Cells[1].Text+"'";
   ExecuteSql(strSql);
   BindData();
  }

  private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex = e.Item.ItemIndex;
   BindData();
  }

  private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)   //
  {
   if(e.CommandName=="UserDelete")
    DataGrid1_DeleteCommand(source,e);
  }

  private void bAllDelete_Click(object sender, System.EventArgs e)   //全选按钮删除操作
  {
   foreach(DataGridItem dgi in DataGrid1.Items)
   {
    CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
    if(cb.Checked)
    {
     //以下执行删除操作
     int nID = int.Parse(dgi.Cells[1].Text);
     string strSql = "delete from Class where Bjbh='"+nID+"'";
     ExecuteSql(strSql);
    }
   }
   DataGrid1.CurrentPageIndex = 0;
   BindData();
  }

 

  private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   switch(e.Item.ItemType)
   {
    case ListItemType.Item:
    case ListItemType.EditItem:
    case ListItemType.AlternatingItem:
     Button  myDeleteButton = (Button)e.Item.FindControl("UDelete");
     myDeleteButton.Text = "删除";
     myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除第"+e.Item.ItemIndex.ToString()+"行吗?');");
     break;
   }
  }

  private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if (e.Item.ItemType == ListItemType.EditItem)
   {
    for (int i=0;i<e.Item.Cells.Count;i++)
    {
     if(e.Item.Cells[i].Controls.Count>0)
     {
      try
      {
       TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
       t.Width=100;
      }
      catch(Exception ee)
      {
       ee.ToString();
      }
     }
    }
   }
  }

  private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)//更新操作
  {
   string bjbh = e.Item.Cells[1].Text;//处于非编辑状态
   string yx =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
   string rxnf =((TextBox)(e.Item.Cells[6].Controls[0])).Text;
   string strSql = "update Class set Rxnf='"+rxnf+"'where Bjbh='"+bjbh+"'";
   
   ExecuteSql(strSql);
   DataGrid1.EditItemIndex = -1;
   BindData();
  }

  //************************* excel
  private void btnMIME_Click(object sender, System.EventArgs e)
  {
   Response.ContentType = "application/vnd.ms-excel";
   Response.Charset = "utf-8";
   this.EnableViewState = false;
   System.IO.StringWriter sw = new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
   int nCur = DataGrid1.CurrentPageIndex;
   int nSize = DataGrid1.PageSize;
   
   DataGrid1.AllowPaging = false;
   BindData();
    
   DataGrid1.Columns[0].Visible =false;
   DataGrid1.Columns[7].Visible =false;
   DataGrid1.Columns[8].Visible =false;
   DataGrid1.RenderControl(hw);
   
   //以下恢复分页
   DataGrid1.AllowPaging = true;
   DataGrid1.CurrentPageIndex = nCur;
   DataGrid1.PageSize = nSize;
   BindData();
   Response.Write(sw.ToString());
   Response.End();
  }

  private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   this.DropDownList2.Items.Clear();
   this.getMajor(DropDownList1.SelectedValue.ToString());
  }

  private void bAllUpdate_Click(object sender, System.EventArgs e)
  {
   foreach(DataGridItem dgi in DataGrid1.Items)
   {
    CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
    if(cb.Checked)
    {
     //以下执行删除操作
     int nID = int.Parse(dgi.Cells[1].Text);
     //string str=r.date;
     //string strSql = "update Class set Rxnf='"+str+"'where Bjbh='"+nID+"'";
     // string strSql = "update Class set Rxnf='2006-8-2' where Bjbh='"+nID+"'";
     //ExecuteSql(strSql);
    }
   }
   DataGrid1.CurrentPageIndex = 0;
   BindData();
  }


 }
}

posted on 2007-05-31 13:36 噢耶游戏 阅读(...) 评论(...) 编辑 收藏