最近開發一個小型的OA
發表于:2007-06-30來源:作者:點擊數:
標簽:
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=fa
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://www.kjueaiud.com