蛙蛙推荐:最近开发一个小型的OA,把常用代码和大家分享一下先

类别:Asp 点击:0 评论:0 推荐:

蛙蛙推荐:最近开发一个小型的OA,把常用代码和大家分享一下先

1.判断权限部分的代码

int i=Convert.ToInt32((string)Session["user_flag"]);

              switch(i)

              {//根据权限执行不同的sql语句

                   case 0://管理员

                        e.Item.Cells[9].Visible=false;

                       break;

                   case 1://厅领导

                        e.Item.Cells[6].Visible=false;

                        e.Item.Cells[7].Visible=false;

                        e.Item.Cells[8].Visible=false;

                        e.Item.Cells[9].Visible=false;

                       break;

                   case 2://省律师协会

                        e.Item.Cells[8].Visible=false;

                       break;

                   case 3://省律师处

                        e.Item.Cells[8].Visible=false;

                       break;

                   case 4://市律师协会

                        e.Item.Cells[8].Visible=false;

                       break;

                   case 5://市律师处

                        e.Item.Cells[8].Visible=false;

                       break;

                   case 6://省直律师事务所

                        e.Item.Cells[8].Visible=false;

                       break;

              }    

2.根据linkbutton来动态显示或者隐藏某些控件

private void LinkButton2_Click(object sender, System.EventArgs e)

         {

              LinkButton lb=(LinkButton)sender;

              Panel1.Visible=lb.CommandName=="yes";

              lb.CommandName=(lb.CommandName=="no")?"yes":"no";

         }

3.设置标题列的背景颜色

private void dg1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         {   

              if(e.Item.ItemType==ListItemType.Header)

              {//设置标题列的背景颜色

                   for(int j=0;j<e.Item.Cells.Count;j++)

                   {

                        //e.Item.Cells[j].BackColor=Color.FromName("#ffff66");

                        e.Item.Cells[j].CssClass="title";

                   }

              }

              }

4.本程序所用样式表

body

{

 font-size:12px;

}

table{

     border-collapse:collapse;

     border: 1px solid #28ACE2;

     word-wrap:break-word;

}td {

     border: 1px solid #28ACE2;

     background-color: #E7F8FF;

     font-size: 12px;

}

a {

     color: #003399;

     text-decoration: none;

}

a:hover {

     color: #003366;

}

.title

{

     background-color:#ffff66;

}

5.多条件高级查询

void bindgrid(string word1,string word2)

         {

              SqlConnection myconn=oa.cls.globalstate.GetConnection();

              string sql=@"select * from [suo] where ([id] is not null)";//因为id是主键,所以不可能为null,这是个小技巧

              if(Convert.ToInt32((string)Session["user_flag"])==0)

              {//如果是管理员就只能看到已经提交的信息

                   sql=@"select * from [suo] where [is]<>0";

              }

              if(word1!=""){sql+=" and ([name] like '%"+word1+"%')";}//加一个判断条件,注意语句开头有一个空格

              if(word2!=""){sql+=" and ([zhiye] like '%"+word2+"%')";}//加一个判断条件,注意语句开头有一个空格

              sql+=" order by id desc";

              Response.Write(sql);

              SqlDataAdapter da=new SqlDataAdapter(sql,myconn);

              DataSet ds=new DataSet();

              da.Fill(ds,"suo");

              dg1.DataSource=ds.Tables[0].DefaultView;

              dg1.DataBind();

         }

6.动态改变数据绑定列在编辑时自动生成的TextBox

private void dg1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         {

              if (e.Item.ItemType == ListItemType.EditItem)

              {//在编辑是让第3个数据帮定列自动生成的TextBox成为多行的

                   TextBox tb=e.Item.Cells[2].Controls[0] as TextBox;

                  if(tb!=null)             

                   {

                        tb.TextMode=TextBoxMode.MultiLine;

                   }

              }

         }

7.给删除添加确认

private void dg1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)

         {

              if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)

              {

                   //给删除加上确认

                   LinkButton lbtdelete = e.Item.Cells[4].Controls[0] as LinkButton;

                  

                   if (lbtdelete!=null)

                   {

                        lbtdelete.Attributes.Add("onclick","var d = '您确定要删除吗?';return window.confirm(d);");

                   }

              }

         }

8.DataGrid更新数据

private void dg1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

         {//更新记录

              string sql=@"update [suo_jl]set jibie=@jibie,neirong=@neirong where id=@id";

              SqlParameter[]parameters={                                              

                                                new SqlParameter("@id",SqlDbType.Int,4),

                                                new SqlParameter("@jibie",SqlDbType.VarChar,200),

                                                new SqlParameter("@neirong",SqlDbType.Text,16),                     

              };

              parameters[0].Value=(int)dg1.DataKeys[e.Item.ItemIndex];//获取正在编辑的列的主键ID

              parameters[1].Value=((DropDownList)e.Item.FindControl("DropDownList1")).SelectedValue;//此列为模板列,所以要用FindControl方法

              parameters[2].Value=((TextBox)e.Item.Cells[2].Controls[0]).Text;//此列为数据绑定列,直接用cell集合就能访问

              oa.cls.globalstate.Runsql(sql,parameters);

              dg1.EditItemIndex=-1;//设置编辑索引为-1,要不老显示在编辑模式

              bindgrid();//绑定表格

         }

9.DataGrid删除数据

          private void dg1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

         {

              string sql=@"delete from [suo_jl] where id=@id";

              SqlParameter[]parameters={new SqlParameter("@id",SqlDbType.Int,4)};

              parameters[0].Value=(int)dg1.DataKeys[e.Item.ItemIndex];//获取DataGrid的DataKeys集合,需要在页面上指定

              oa.cls.globalstate.Runsql(sql,parameters);

              bindgrid();//最后还要绑定数据      

         }

10.DataGrid编辑和取消编辑数据

private void dg1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

         {

              dg1.EditItemIndex=-1;

              bindgrid();

         }

 

          private void dg1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)

         {

              dg1.EditItemIndex=(int)e.Item.ItemIndex;

              bindgrid();

         }

11.执行存储过程和Sql语句

public static int Runsp(string SQL,SqlParameter[] parameters)

         {

              //执行存储过程

              SqlConnection conn=GetConnection();

              SqlCommand cmd=new SqlCommand(SQL,conn);

              cmd.CommandType = CommandType.StoredProcedure;

 

              for(int i=0;i<parameters.Length;i++)

              {

                   cmd.Parameters.Add(parameters[i]);

             

              }

              try

              {

                   conn.Open();

                   return cmd.ExecuteNonQuery();

              }

              catch(Exception exp){throw exp;}

              finally{conn.Close();}

         }

         public static int Runsql(string SQL,SqlParameter[] parameters)

         {

              //执行sql语句

              SqlConnection conn=GetConnection();

              SqlCommand cmd=new SqlCommand(SQL,conn);

              foreach(SqlParameter pr in parameters)

              {

                            cmd.Parameters.Add(pr);

 

              }

              try

              {

                   conn.Open();

                   return cmd.ExecuteNonQuery();

              }

              catch(Exception exp){throw exp;}

              finally{conn.Close();}

         }

12.获取数据库连接实例

public static SqlConnection GetConnection()

         {

              /*

               * 获取数据库连接实例

               */

              SqlConnection myconn;

              myconn=new SqlConnection((string)ConfigurationSettings.AppSettings["connstring"]);

              return myconn;

         }

13.生成随机数

public static string MakeRand(string s, int len)

         {

              //返回一个随机数,从s里选,然后返回len长度的随机数

              if (s==null || s=="") throw new MyException("String:s not null");

 

              string tmpstr = "";

              int iRandNum;

              Random rnd = new Random();

              for(int i=0;i<len;i++)

              {

                   iRandNum = rnd.Next(s.Length);

                   tmpstr += s[iRandNum];

              }

              return tmpstr;

         }

 

         public static string MakeRand(int len)

         {

              //上一个函数的重载版本

              string str;                      

              str="abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";            

              return MakeRand(str,len);

         }

14.我自己写的上传函数

public static string upfile(System.Web.UI.Page p,System.Web.UI.HtmlControls.HtmlInputFile file1){

        

              //上传文件

              if (file1.PostedFile != null)

              {

                   try

                   {

                       string spath=p.Server.MapPath(p.Request.ApplicationPath)+ConfigurationSettings.AppSettings["uppath"];

                       string fpath=oa.cls.globalstate.MakeRand(10)+System.IO.Path.GetFileName(file1.PostedFile.FileName);

                        spath+=fpath;

                        file1.PostedFile.SaveAs(spath);

                       return fpath;

 

                      

                   }

                   catch (Exception exc)

                   {

                       throw exc;

                   }

 

              }

              return "";

         }

本文地址:http://com.8s8s.com/it/it8790.htm