Recommended frog frog: recently developed a small OA, share the common code and share it first
1. Judgment the code of the right part
INT i = convert.toint32 (string) session ["user_flag"]);
Switch (i)
{/ / Different SQL statements based on permissions
Case 0: // Administrator
E.Item.cells [9] .visible = false;
Break;
Case 1: // Hall leadership
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: // Provincial Lawyers Association
E.Item.cells [8] .visible = false;
Break;
Case 3: // Provincial Law End
E.Item.cells [8] .visible = false;
Break;
Case 4: // City Lawyers Association
E.Item.cells [8] .visible = false;
Break;
Case 5: // City Law Firm
E.Item.cells [8] .visible = false;
Break;
Case 6: / / Provincial Law Firm
E.Item.cells [8] .visible = false;
Break;
}
2. Dynamically display or hide certain controls according to 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. Set the background color of the title column
Private Void DG1_Itemcreated (Object Sender, System.Web.ui.WebControls.DataGriditeMeventArgs E)
{
IF (E.Item.ItemType == ListItemType.Header)
{// Set the background color of the title column
For (int J = 0; j { //e.item.cells[j].backcolor=color.fromname ("#fffff66 "); E.Item.cells [j] .cssclass = "title"; } } } 4. The style sheet used in this program 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: # fff66; } 5. Many advanced queries Void BindGrid (String Word1, String Word2) { Sqlconnection myconn = OA.cls.globalState.getConnection (); string sql = @ "select * from [suo] where ([id] is not null); // Because the id is the primary key, it is impossible to be null, this is a Tips IF (Convert.Toint 32 (String) Session ["User_Flag"]) == 0) {// If the administrator can only see the information already submitted SQL = @ "Select * from [suo] where [IS] <> 0"; } IF (Word1! = "") {SQL = "and ([Name] Like '%" Word1 "%')";} // Add a judgment condition, pay attention to the beginning of the statement has a space IF (Word2! = ") {SQL =" and ([zhiye] Like '% " Word2 "%') ";} // add a judgment condition, pay attention to the beginning of the statement, there is a space 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. Dynamically change the data binding of the TEXTBOX that is automatically generated when editing Private Void DG1_Itemcreated (Object Sender, System.Web.ui.WebControls.DataGriditeMeventArgs E) { IF (E.Item.ItemType == ListItemType.editItem) {// Editing TextBox automatically generated by editing is multi-line TextBox Tb = E.Item.cells [2] .controls [0] as textbox; IF (TB! = NULL) { Tb.TextMode = TextBoxMode.Multiline; } } } 7. Add confirmation to delete Private void DG1_ItemDatabase (Object Sender, System.Web.ui.WebControls.DataGriditeMeventArgs E) { IF (E.Item.ItemType == ListItemType.Item || E.Item.ItemType == ListItemType.alternatingItem) { / / Confirm to delete LinkButton LbTdelete = E.Item.cells [4] .controls [0] as linkbutton; IF (lbtdelete! = null) { LBTDelete.attributes.add ("onclick", "VAR D = 'Are you sure you want to delete?'; return window.confirm (d);"); } } } 8.DataGrid update data Private void DG1_UPDATECOMMAND (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) {//update record 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]; // Get the primary key ID of the column being edited Parameters [1] .Value = ((DropDownList) E.Item.FindControl ("DropDownList1")). SELECTEDVALUE; / / This list is a template column, so use FindControl method Parameters [2] .value = ((TextBox) E.Cells [2] .controls [0]). Text; // This list is a data binding column and can be accessed directly with the Cell collection Oa.cls.globalstate.runsql (SQL, Parameters); DG1.EDITEMINDEX = -1; // Set the editing index to -1, do not display in edit mode Bindgrid (); // Binding form } 9.DataGrid delete data 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]; // Get the DataKeys collection of DataGrid, you need to specify on the page Oa.cls.globalstate.runsql (SQL, Parameters); Bindgrid (); // Finally, it is also bound to } 10.DataGrid editing and cancel editing data Private void DG1_cancelcommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) { DG1.EDITEMINDEX = -1; Bindgrid (); } Private void DG1_EDitCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E) { DG1.EDititemindex = (int) E.Item.itemindex; Bindgrid (); } 11. Execute a stored procedure and SQL statement Public Static Int Runsp (String SQL, SQLParameter [] Parameters) { // Perform a stored procedure SqlConnection conn = getConnection (); Sqlcommand cmd = new SQLCOMMAND (SQL, CONN); cmd.commandtype = commandType.StoredProcedure; for (int i = 0; i { Cmd.Parameters.Add (parameters [i]); } Try { Cn.open (); Return cmd.executenonquery (); } Catch (Exception Exp) {throw Exp;} Finally {conn.close (); } Public Static int Runsql (String SQL, SQLParameter [] Parameters) { / / Execute SQL statement SqlConnection conn = getConnection (); Sqlcommand cmd = new SQLCOMMAND (SQL, CONN); Foreach (Sqlparameter Pr in Parameters) { CMD.Parameters.Add (Pr); } Try { Cn.open (); Return cmd.executenonquery (); } Catch (Exception Exp) {throw Exp;} Finally {conn.close (); } 12. Get database connection instances Public Static SqlConnection getConnection () { / * * Get database connection instances * / SqlConnection myconn; MyConn = New SqlConnection (String) ConfigurationSettings.AppSettings ["connString"]); Return myconn; } 13. Generate random number Public Static String Makerand (String S, INT LEN) { / / Return a random number, select from S, then return to the random number of LEN length IF (s == null || s == ")" "String: s not null"; String tmpstr = "" IND IRANDNUM; Random rnd = new random (); For (int i = 0; i { IRANDNUM = rnd.next (s.ley); Tmpstr = s [IRANDNUM]; } Return Tmpstr; } Public Static String Makerand (int LEN) { // Upset the overload version of the last function String Str; Str = "Abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz"; Return Makerand (STR, LEN); } 14. I write my own upload function Public static string upfile (system.Web.ui.page p, system.web.ui.htmlcontrols.htmlinputfile file1) { //upload files 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; } } ""; " }