(Original) Some cooperation with DataGrid and DropDownList and custom DataGrid using CSS

xiaoxiao2021-03-06  20

Sometimes we need (1) use the drop-down box when edited, and the default is the content of the database (2) Use the drop-down box filtering data (3) Use the CSS unified DataGrid to give the code: Data structure: Table DEP: Depid (Identify Primary Key), DepName (College Name) Table Stu: Stuid (Identifier Primary Key), Stuname (Student Name), StudiePid (College ID = Table Dep.DEPID) Front Desk: <% @ Page Language = "CODEBEHIND =" Webform28.aspx.cs "autoeventwireup =" false "inherits =" 9cbs.webform28 "%> WebForm28 </ title> <meta name = "GENERATOR" Content = "Microsoft Visual Studio .NET 7.1"> <meta name = "CODE_LANGUAGE" Content = "C #"> <meta name = "vs_defaultClientScript" content = "JavaScript"> < Link href = "css.css" rel = "stylesheet" type = "text / css"> <meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5> </ head> <body> <form id = "form1" method = "post" runat = "server"> <ask = "dropdownload1" runat = "server" autopostback = "true"> </ ask: DropDownList> <asp: DataGrid ID = "DataGrid1" runat = "server" autogeneratecolumns = "false" cellspacing = "1" borderwidth = "0px" cellpadding = "5" cssclass = "border" OneDitcom Mand = "edit" oncancelcommand = "cancel" onupdateCommand = "Update" Datakeyfield =</p> <p>"Stuid"> <ItemStyle CssClass = "item"> </ ItemStyle> <HeaderStyle CssClass = "header"> </ HeaderStyle> <Columns> <asp: TemplateColumn HeaderText = "Name"> <ItemTemplate> <% # DataBinder.Eval (Container.DataItem, "stuname")%> <edititemtemplate> <edititemtemplate> <ask: textbox id = "name" runat = "server" text = '<% # databinder.eval (container.DataItem, "stuname") %> 'Width = "88px"> </ asp: textbox> </ edititemtemplate> </ ask: templateColumn> <ask: templatecolumn headertext = "college"> <itemtemplate> <% # databinder.eval (container.DataItem, " depname ")%> </ ItemTemplate> <EditItemTemplate> <asp: DropDownList ID =" dep "Runat =" server "> </ asp: DropDownList> </ EditItemTemplate> </ asp: TemplateColumn> <asp: EditCommandColumn ButtonType =" PushButton "UpdateText =" Update "CancelText =" Cancel "EditText =" Edit "> </ asp: EditCommandColumn> </ Column> </ asp: DataGrid> </ form> </ body> </ html> Backstage: USING SYSTEM Using system.collections; using system.componentmodel; u sing System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web. Ui.htmlControls;</p> <p>Namespace 9cbs {/// <summary> /// WebFORM28 summary description. /// </ summary> public class WebForm28: System.Web.UI.Page {protected System.Web.UI.WebControls.DropDownList DropDownList1; protected System.Web.UI.WebControls.DataGrid DataGrid1; private void Page_Load (object sender, System.Eventargs e) {// Place the user code here to initialize the page if (! Ispostback) {setbind (); setbind2 ();}} protected void setbind ()}</p> <p>SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from stu, dep where stu.studepid = dep.depid", conn); DataSet ds = new DataSet (); da.fill (DS, "Table1"); this.dataGrid1.datasource = ds.tables ["Table1"]; this.dataGrid1.databind ();}</p> <p>protected void setbind2 () {</p> <p>SqlConnection conn2 = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da2 = new SqlDataAdapter ( "select * from dep", conn2); DataSet ds2 = new DataSet (); da2.Fill (ds2, "table1"); this.DropDownList1.DataSource = ds2.Tables [ "table1"]; this.DropDownList1.DataTextField = "depname"; this.DropDownList1.DataValueField = "depid"; this.DropDownList1.DataBind (); this. DropdownList1.Items.Insert (0, New ListItem ";</p> <p>protected void SetBind3 () {string s = this.DropDownList1.SelectedValue; SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlCommand comm = new SqlCommand (); comm.Connection = conn; if (S! = "") {Comm.commandtext = "SELECT * from STU, Dep where stu.studepid = Dep.Depid and depid = @ depid"; sqlparameter parm1 = new sqlparameter ("@ depid", sqldbtype.It); parm1.Value = s; comm.Parameters.Add (parm1);} else comm.CommandText = "select * from stu, dep where stu.studepid = dep.depid"; SqlDataAdapter da = new SqlDataAdapter (); da.SelectCommand = Comm; DataSet DS = New Dataset (); DA.FILL (DS, "Table1"); this.dataGrid1.datasource = DS.TABLES ["Table1"]; this.dataGrid1.database ();} #Region Web Form The code generated by the designer Override protected void Oninit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// </ summary> private void InitializeComponent () {this.DataGrid1.ItemDataBound = new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound); this.DropDownList1.SelectedIndexChanged = new System.EventHandler (this. DropDownList1_selected; this.load = new system.eventhandler (this.page_load);} #ENDREGION</p> <p>private void DataGrid1_ItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ "conn"]); SqlDataAdapter da = new SqlDataAdapter ( "select * from dep ", conn); DataSet DS = New Dataset (); DA.FILL (DS," Table1 "); if (E.Item.itemType == ListItemType.editItem) {DropDownList DDL = (DropDownList) E.Item.FindControl "dep"); ddl.datasource = ds.tables ["Table1"]; ddl.datatextfield = "depname"; DDL.DataValuefield = "depid"; ddl.database (); ddl.items.FindByValue (Convert.Tostring DataBinder.eval ("DEPID"))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))). Item.itemindex; if (this.dropdownList1.selectedValue == "") setbind (); else setbind3 ();</p> <p>Protected Void Cancel (Object Sender, DataGridCommandeventArgs E) {this.dataGrid1.editItemIndex = -1; if (this.dropdownList1.SelectedValue == ") setbind (); else setbind3 ();}</p> <p>protected void update (object sender, DataGridCommandEventArgs e) {if (e.Item.ItemType == ListItemType.EditItem) // pressed to edit only after submission {SqlConnection conn = new SqlConnection (System.Configuration.ConfigurationSettings.AppSettings [ " conn "]); SqlCommand comm = new SqlCommand (" update stu set stuname = @ name, studepid = @ depid where stuid = @ id ", conn); SqlParameter parm1 = new SqlParameter (" @ name ", SqlDbType.NVarChar, 50 ); Parm1.value = ((TextBox) E.Item.FindControl ("name"))). Text; sqlparameter parm2 = new sqlparameter ("@ depid", sqldbtype.int); parm2.value = ((DropDownList) E. Item.FindControl ("dep")). SelectedValue; 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; if; if (this.dropdownList1.selectedValue == "") setbind (); else setbind3 (); // Selected filtered using SetBind3 ()}} private void DropDownList1_SelectedIndexChanged (object sender, System.EventArgs e) {SetBind3 ();}}} css: .border {background-color: # 00496C;} header {font-family:. " Song Song ", SANS-Serif; Font-Size: 10pt; Font-Weight: bold; color: #fffff; Background-Color: # 0080c0; Text-Align: center;}. Item {font-family:" Song ", SANS -serif; font-size: 9pt; font-weight: Normal; Color: # 0080c0; Background-Color: #fffff; Text-Align: center;</p> <p>The code is relatively simple, and the following is briefly explained: (1) setbind () is the basic binding; setbind2 () is the DROPDOWNLIST that is bound to be outside; setbind3 () is the binding of DataGrid after filtering after the drop-down box ( 2) Use the CSS to implement the table border to utilize CellSpacing, so this value is the width of the border, using Background-Color in the CSS of the Table Border to describe the color of the border. Below is some index of DataGrid this site: DataGrid implementation to delete (with prompt) to change the paging binding Dynamically changed DataGrid DataGrid in DataGrid Setting multiple parameters DataGrid Implement Self-amplification, radio, multi-selection DataGrid data export Several methods of downloading the Excel files DataGrid and DropDownList's cooperation and using CSS custom DataGrid</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-43372.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="43372" 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.042</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 = '07OSjyUQujsGZhSxRSf06jbL78C21nJU3bYeMlNma84sjtu5BpAljsRg5DdhxeD1lYyqAQkh_2F0Cgm_2Be7IlMGnA_3D_3D'; 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>