A few articles about all code about DataGrid

xiaoxiao2021-03-06  39

Some netizens have been coded to me, and now all this DataGrid contains demonstrations for the following features: Add, delete, modify, pagin, download data to client, total, filter record, radio, multiple choices.

Database table name: TB1, 3 of which are the main key of the ID from the ID, VName Varchar (50), IAGE INT client code: <% ... @ page language = "c #" codebehind = "Webform5.aspx .cs "autoeventwireup =" false "inherits =" 9cbs.webform5 "%> Webform5 </ title> <meta content = "Microsoft Visual Studio .NET 7.1" name = "GENERATOR"> <meta content = "C #" name = "CODE_LANGUAGE"> <meta content = "JavaScript" name = "vs_defaultClientScript"> <meta content = "http://schemas.microsoft.com/intellisense/ie5" Name = "vs_targetschema"> </ head> <body> <form id = "form1" method = "post" runat = "server"> <input type = "hidden" id = "rd" runat = "server" name = "rd"> <asp: textbox id = "textbox1" runat = "server" width = "88px"> </ asp: textbox> <ask: textbox id = "TextBox2" runat = "server" width = "40px"> </ asp: textbox> <ask: button id = "Button1" runat = "server" text = "Add"> </ asp: button> <asp: TextBox ID = "TextBox3" runat = "Width =" 128 PX "> </ asp: textbox> <ask: button id =" button2 "runat =" server "text =" filter "> </ asp: button> <ask: button id =" Button3 "runat ="</p> <p>Server "Text =" Download "> </ asp: button> <ask: DataGrid ID =" DataGrid1 "runat =" server "autogeneratecolumns =" ​​false "datakeyfield =" id "allowpaging =" true "Pagesize =" 20 "OneDitCommand = "edit" OnCancelCommand = "cancel" OnUpdateCommand = "update"> <Columns> <asp: TemplateColumn HeaderText = "multiple choice"> <ItemTemplate> <input type = "hidden" id = "SelectedID" runat = "server" value = '<% # Container.itemindex%>' name = "selectedid" /> <asp: checkbox id = "chkexport" runat = "server" /> </ itemtemplate> <asp: templateColumn> <ask: templatecolumn headertext = " Single selection "> <itemtemplate> <% ... # container.itemindex 1%> <input type = radio name =" rad "value = '<% # Container.itemindex 1%>> </ itemtemplate> < / ASP: TEMPLATECOLUMN> <Asp: templateColumn headertext = "Name"> <itemtemplate> <% ... # myfunc (databinder.eval ("vname"))%> </ itemtemplate> <edititemtemplate> <edititemtemplate> <asp: textbox id = "</p> <p>Name "runat =" server "text = '<% # DataBinder.eval (Container.DataItem," VNAME ")%>' width =" 88px "> </ asp: textbox> </ edititemtemplate> </ ask: templateColumn> <ask: templateColumn headertext = "age"> <itemtemplate> <% ... # dataBinder.eval (container.DataItem, "IAGE")%> </ itemtemplate> <edititemtemplate> <asp: textbox id = "age" runat = "Server" text = '<% # dataBinder.eval ("iAGE")%>' width = "40px"> </ asp: textbox> </ adj: TemplateColumn> <asp: TemplateColumn Headertext = "Open"> <itemtemplate> <asp: hyperlink text = "Open" NavigateURL = '<% # "newpage.aspx? Name =" DataBinder.eval ("vname") "& agn = " DataBinder.eval (container.dat) AITEM, "IAGE")%> 'Runat = "Server" target = "_ blank"> </ ask: HYPERLINK> </ itemtemplate> </ ask: templateColumn> <ask: templateColumn headertext = "Open> <itemtemplate> < ASP: HyperLink text = "Open" NavigateURL = '</p> <p><% # myfunc2 (databinder.eval ("vname"), DataBinder.eval (Container.DataItem, "IAGE")))%> 'Runat = "server" target = "_ blank" id = "HyperLink1"> </ asp: HyperLink> </ ItemTemplate> </ asp: TemplateColumn> <asp: EditCommandColumn ButtonType = "LinkButton" UpdateText = "update" CancelText = "cancel" EditText = "edit"> </ asp: EditCommandColumn> <asp: ButtonColumn Text = "Delete" CommandName = "DEL"> </ asp: buttoncolumn> </ columns> <PagersTyle mode = "numericpages"> </ pagerstyle> </ ask: DataGrid> <ask: label id = "label1" Runat = "Server"> </ asp: label> <ask: button id = "button4" runat = "server" text = "radio results"> </ asp: button> <ask: button id = "Button5" runat = "Server" text = "Multiple Results"> </ asp: button> <asp: button id = "Button6" runat = "text ="> </ asp: button> <asp: button id = "Button7" runat = "server" text = "Cancel all"> </ asp: button> </ form> </ body> < / HTML> server code: using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.ui; use system.web.ui.webcontrols; using system.web.ui.htmlcontrols;</p> <p>Using System.io; Namespace 9cbs ... {/ ** //// <summary> /// WebForm5 summary description.</p> <p>/// </ summary> public class WebForm5: System.Web.UI.Page ... {protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web .UI.WebControls.Button Button1; protected System.Web.UI.WebControls.TextBox TextBox3; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.Button Button3; protected System.Web.UI .WebControls.Label Label1; protected System.Web.UI.HtmlControls.HtmlInputHidden rd; protected System.Web.UI.WebControls.Button button4; protected System.Web.UI.WebControls.Button Button5; protected System.Web.UI.WebControls .Button Button6; protected System.Web.UI.WebControls.DataGrid DataGrid1; protected System.Web.UI.WebControls.Button Button7; private void Page_Load (object sender, System.EventArgs e) ... {string js = ""; JS = "<script> / r / n"; JS = "Function LD () {/ r / n"; js = "for (i = 0; i <document.getlementsByname ('RAD'). Length; i ) / r / n"; js = "if (Document .getelementSbyName ('RAD') [i] .value == "; JS =" Document.getElementByid ('RD'). Value) "; JS =" Document.GtelementsByName ('Rad') [i] .checked = TRUE / r / n "; JS ="} / r / n "; js =" window.οnlοad = ld / r / n "; JS =" </ script> / r / n "; this.registerClientScriptblock (" JS " , JS);</p> <p>if (IsPostBack!) ... {SetBind ();}} protected void SetBind () ... {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "Select * from TB1", Conn; DataSet DS = New Dataset (); DA.FILL (DS, "Table1"); this.DataGrid1.datasource = DS.TABLES ["Table1"]; this.DataGrid1.databind ); This.lable1.text = ds.tables ["Table1"]. Compute ("iAGE)", "IAGE> 20"). Tostring ();} protected (); String S) ... {SQLConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from tb1", conn); DataSet ds = new DataSet (); da.Fill (ds, " Table1 "); DataView DV = DS.TABLES [" Table1 "]. defaultview; dv.rowfilter = s; this.d Atagrid1.datasource = DV; this.DataGrid1.database ();} Web Form Designer Generated Code #Region Web Form Designer Generated Code Override Protected Void OnInit (Eventargs E) ... {/// Codegen : This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.oninit (e);} / ** //// <summary> /// Designer supports the required method - do not use the code editor to modify the // / this method.</p> <p>/// </ summary> private void InitializeComponent () ... {this.Button1.Click = new System.EventHandler (this.Button1_Click); this.Button2.Click = new System.EventHandler (this.Button2_Click); this.Button3.Click = new System.EventHandler (this.Button3_Click); this.button4.Click = new System.EventHandler (this.Button4_Click); this.Button5.Click = new System.EventHandler (this.Button5_Click) ; this.Button6.Click = new System.EventHandler (this.Button6_Click); this.Button7.Click = new System.EventHandler (this.Button7_Click); this.Load = new System.EventHandler (this.Page_Load); 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.DataGridPageChang edEventHandler (this.DataGrid1_PageIndexChanged); this.DataGrid1.ItemDataBound = new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound);} #endregion private void Button1_Click (object sender, System.EventArgs e) ... {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlCommand comm = new SqlCommand ( "insert into tb1 (vName, iAge) values ​​(@ vName, @ iAge)", conn); SqlParameter parm1 = New SqlParameter ("@ vname", sqldbtype.nvarchar, 50);</p> <p>PARM1.VALUE = this.textBox1.text; sqlparameter parm2 = new sqlparameter ("@ iAGe", sqldbtype.int); parm2.value = this.textBox2.text; comm.parameters.add (PARM1); comm.pameters.add (PARM2); conn.open (); comm.executenonquery (); conn.close (); setbind ();} private void data; }. west, system.web.ui.webcontrols.DataGridCommandeventEventArgs e) ... {ix (e.CommandName == "del" && e.Item.ItemType = ListItemType.EditItem!) ... {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlCommand comm = new SqlCommand ( "delete from tb1 where id = @ id", conn); sqlparameter parm1 = new sqlparameter ("@ ID", sqldbtype.int); parm1.value = this.dataGrid1.DataKeys [E.Item.itemindex]; comm.parameters .Add (parm1); conn.open (); Comm.executenonq uery (); conn.Close (); SetBind ();}} private void DataGrid1_PageIndexChanged (object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) ... {this.DataGrid1.CurrentPageIndex = e.NewPageIndex; SetBind ( );} protected void edit (object sender, DataGridCommandEventArgs e) ... {this.DataGrid1.EditItemIndex = e.Item.ItemIndex; SetBind ();} protected void cancel (object sender, DataGridCommandEventArgs e) ... {this. DataGrid1.edititemindex = -1;</p> <p>SetBind ();} protected void update (object sender, DataGridCommandEventArgs e) ... {if (e.Item.ItemType == ListItemType.EditItem) ... {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); sqlcommand comm = new sqlcommand ("Update TB1 set vname = @ vname, IAGE = @ IAGE WHERE ID = = @ id", conn); SQLParameter Parm1 = New Sqlparameter ("@ vname", sqldbtype.nvarchar, 50); parm1.Value = ((TextBox) e.Item.FindControl ( "name")) Text;. SqlParameter parm2 = new SqlParameter ( "@ iAge", SqlDbType.Int); parm2.Value = ((TextBox) e .Item.FindControl ("age")). Text; sqlparameter parm3 = new SQLParameter ("@ ID", sqldbtype.int); PARM3.Value = this.dataGrid1.DataKeys [E.Item.itemindex]; comm.Parameters. Add (PARM1); Comm.Parameters.Add (PARM2); Comm.Parameters.Add (PARM3); conn.open (); comm.ExecuteNonQuery (); conn.Close (); this.DataGrid1.EditItemIndex = -1; SetBind ();}} private void DataGrid1_ItemCreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ... {if (E.Item.itemType == ListiteMType.Item || E.Item.itemType == ListItemType.AlternatingItem || E.Item.itemType == ListItemType.editItem) ... {(LinkButton) E.Item.cells [ 7] .controls [0]). Attributes.Add ("onclick", "Return Confirm ('</p> <p>DELETE? '); ");}} private void button2_click (object sender, system.eventargs e) ... {if (this.textBox3.text ==") ... {setbind ();} else .. . {SetBind (this.TextBox3.Text);}} private void DataGrid1_SelectedIndexChanged (object sender, System.EventArgs e) ... {} private void Button3_Click (object sender, System.EventArgs e) ... {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from tb1", conn); DataSet ds = new DataSet (); da.Fill (ds, "table1") ; DataTable dt = ds.Tables [ "table1"];. string name = System.Configuration.ConfigurationSettings.AppSettings [ "downloadurl"] ToString () DateTime.Today.ToString ( "yyyyMMdd") new Random (DateTime.Now .Milliseco Nd) .Next (10000) .tostring () ". csv"; filestream fs = new filestream (name, filemode.create, fileaccess.write); streamwriter sw = new streamwriter (fs, system.text.encoding.getencoding) "GB2312")); sw.writeline ("Auto Number, Name, Age"); Foreach (Datarow Dr In Dt.Rows) ... {sw.writeLine (DR ["ID"] "," DR [ "VNAME"] "," DR ["IAGE"]);} sw.close (); response.addheader ("Content-Disposition", "Attachment; FileName =" Server.urlencode (Name));</p> <p>Response.ContentType = "application / ms-excel"; Response.WriteFile (name); Response.End ();} private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) ... {if ( E.Item.itepe.Item || E.Item.itemType == ListItemType.AlternatingItem || E.Item.itemType == ListiteMTYPE.EDITITEM) ... {IF (Convert.Toint16 (DataBinder.eval (eVal (E .Item.dataitem, "IAGE")) <30) E.ITEM.BACKCOLOR = color.pink;}} protected string myfunc (object s) ... {string _s; if (s.toString () == "Zhu ") _S =" <b> " S.toString () " </ b> "; else _s = S.toString (); return_s;} protected string myfunc2 (Object S1, Object S2) ... {RETURN "newpage.aspx? Name =" S1.toString () "& agn =" s2.toString ();} private void button4_click (object sender, system.eventargs e) ... { IF (Request.form ["RAD"]! = null) ... {rd.value = Request.form ["rad"]. TOSTRING (); this.label1.text = "Select the line number:" Request .Form ["rad"]. TOSTRING ();}} private void button5_click (object sender, system.eventargs e) ... {this.label1.text = ""; foreach (DataGridItem di in this.DataGrid1.Items) ... {IF ((Checkbox) Di.FindControl ("ChKExport")). Checked ==</p> <p>True) ... {this.label1.text = this.dataGrid1.datakeys [INT.PARS ((HTMLINPUTHIDEN) DI.FINDCONTROL ("SELECTEDID")). Value)] "<br>";}} Void Button6_Click (Object Sender, System.EventArgs E) ... {Foreach (DataGridItem di in this.dataGrid1.items) ... {((CheckBox) Di.FindControl ("ChKExport")). Checked = true;}} Private Void Button7_Click (Object Sender, System.Eventargs E) ... {Foreach (DataGridItem di in this.dataGrid1.items) ... {(Checkbox) Di.FindControl ("chkexport")). Checked = false;} }} sorry vs.net eaten up some of the events registered on: 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.ItemDataBound = new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound) ;</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-52317.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="52317" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.033</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'lYRJZfLFOP7wO_2F3xg8Rtnb77NFCRvjspGJbVcyAq6min2qWJafOI0FJRmTAv3Nm_2BWVpMLCbSGrfAx0RG'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>