[Reproduced] (ASP.NET) Modify and delete DataGrid line - Database Access

xiaoxiao2021-03-06  15

(ASP.NET) Modify and delete DataGrid line - Database Access

This program involves the addition, modification, and delete operations of the database.

It's too lazy to write, sticked the interface, and painted by the interface. This example database: The PUBS database comes with the SQL Server2000, look at the connection string is very clear. If you want to run on this machine, change the UID and PWD to your own SQL login username and password.

Create a web page, named: add.aspx.

Interface design is shown

Add.aspx code:

<% @ Page language = "c #" codebehind = "add.aspx.cs" autoeventwireup = "false" inherits = "tEachshow.charpter7.accessDatabase.add"%>

add </ title></p> <p><Link href = "../../ style.css" type = "text / css" rel = "stylesheet"></p> <p><meta content = "Microsoft Visual Studio .NET 7.1" Name = "Generator"></p> <p><meta content = "c #" Name = "code_language"></p> <p><meta content = "javascript" name = "vs_defaultclientscript"></p> <p><meta content = "http://schemas.microsoft.com/intellisense/ie5" Name = "vs_targetschema"></p> <p></ HEAD></p> <p><body ms_positioning = "gridLayout"></p> <p><form id = "form1" method = "post" runat = "server"></p> <p><div align = "center"></p> <p><center></p> <p><table class = "smallblack" height = "318" cellspacing = "0" cellpadding = "0" width = "429" border = "0"></p> <p><tr></p> <p><td class = "title" Valign = "TOP" width = "429" colspan = "2" height = "31"> Add a new distributor </ td></p> <p></ TR></p> <p><tr></p> <p><TD Valign = "TOP" width = "79" height = "23"> issuer ID: </ td> <td valign = "TOP" width = "350" height = "23"> <asp: textbox id = "TextBox1" runat = "server" height = "18px" cssclass = "smallred"> </ asp: textbox> <font face = "Song body"> (head in 99, a total of 4 digits) </ font> </ td ></p> <p></ TR></p> <p><tr></p> <p><TD Valign = "TOP" width = "79" height = "23"> <font face = "Song"> Name: </ font> </ td></p> <p><TD Valign = "TOP" width = "350" Height = "23"> <asp: textbox id = "textbox2" runat = "server" height = "18px" cssclass = "smallred"> </ asp: textbox> < / TD></p> <p></ TR></p> <p><tr></p> <p><TD Valign = "TOP" width = "79" height = "23"> <font face = "Song"> City: </ font> </ td></p> <p><TD Valign = "TOP" width = "350" Height = "23"> <asp: textbox id = "textbox3" runat = "server" height = "18px" cssclass = "smallred"> </ asp: textbox> < / TD></p> <p></ TR></p> <p><tr></p> <p><TD Valign = "TOP" width = "79" height = "23"> <font face = "Song"> province: </ font> </ td></p> <p><TD Valign = "TOP" width = "350" Height = "23"> <ask: TextBox ID = "TextBox4" runat = "server" height = "18px" cssclass = "smallred"> </ asp: textbox> < Font Face = "Song"> (2 characters) </ font> </ td></p> <p></ TR> <TR></p> <p><TD VALIGN = "TOP" width = "79" height = "24"> <font face = "Song"> Country: </ font> </ td></p> <p><TD Valign = "TOP" width = "350" Height = "24"> <asp: textbox id = "textbox5" runat = "server" height = "18px" cssclass = "smallred"> </ asp: textbox> < / TD></p> <p></ TR></p> <p><tr></p> <p><TD Valign = "TOP" align = "center" width = "429" colspan = "2" height = "24"> <ask: linkbutton id = "linkbutton1" runat = "server"> Submitted to Database </ asp: LinkButton> </ TD></p> <p></ TR></p> <p><tr></p> <p><td width = "429" Height = "147" Valign = "TOP" colspan = "2"></p> <p><ask: DataGrid ID = "DATAGRID1" runat = "server" height = "120px" cssclass = "general" width = "428px"></p> <p><ItemStyle width = "50px"> </ itemstyle></p> <p><Columns></p> <p><asp: editcommandcolumn buttonType = "LinkButton" UpdateText = "update" canceltext = "Cancel" edittext = "Edit"></p> <p><Headerstyle width = "60px"> </ headerstyle></p> <p></ asp: editcommandcolumn></p> <p><asp: buttoncolumn text = "Delete" CommandName = "delete"> </ asp: buttoncolumn></p> <p></ Columns></p> <p></ asp: datagrid> </ td></p> <p></ TR></p> <p></ TABLE></p> <p></ center></p> <p></ div></p> <p></ form></p> <p></ body></p> <p></ Html></p> <p>Add.asp.cs code:</p> <p>Using system;</p> <p>Using system.collections;</p> <p>Using system.componentmodel;</p> <p>Using system.data;</p> <p>Using system.data.sqlclient;</p> <p>Using system.drawing;</p> <p>Using system.Web;</p> <p>Using system.Web.SessionState;</p> <p>Using system.Web.ui;</p> <p>Using system.Web.ui.webcontrols;</p> <p>Using system.Web.ui.htmlcontrols;</p> <p>Namespace TEACHSHOW.CHARPTER7.ACCESSDATABASE</p> <p>{</p> <p>/// <summary></p> <p>/// add a summary description of the Add.</p> <p>/// </ summary></p> <p>Public class address: system.Web.ui.page</p> <p>{</p> <p>protected system.web.ui.webcontrols.textbox textbox1;</p> <p>protected system.web.ui.webcontrols.textbox textbox2;</p> <p>protected system.web.ui.webcontrols.textbox textbox3;</p> <p>protected system.web.ui.webcontrols.textbox textbox4;</p> <p>Protected system.web.ui.webcontrols.linkbutton linkbutton1;</p> <p>Protected system.web.ui.webcontrols.dataGrid DataGrid1;</p> <p>protected system.web.ui.webcontrols.textbox textbox5;</p> <p>Private Void Page_Load (Object Sender, System.EventArgs E)</p> <p>{</p> <p>/ / Place the user code here to initialize the page</p> <p>IF (! this.ispostback)</p> <p>{</p> <p>this.bindgrid ();</p> <p>}</p> <p>}</p> <p>#Region web form designer generated code</p> <p>Override protected void oninit (Eventargs E)</p> <p>{</p> <p>//</p> <p>// Codegen: This call is necessary for the ASP.NET Web Form Designer.</p> <p>//</p> <p>InitializationComponent ();</p> <p>Base.onit (E);</p> <p>}</p> <p>/// <summary></p> <p>/// Designer supports the required method - do not use the code editor to modify</p> <p>/// This method is content.</p> <p>/// </ summary></p> <p>Private vidinitiRizeComponent ()</p> <p>{</p> <p>This.LinkButton1.Click = new system.eventhandler (this.LinkButton1_click);</p> <p>This.DataGrid1.disposed = new system.eventhandler (this.DataGrid1_disposed);</p> <p>This.DataGrid1.cancelcommand = new system.Web.ui.WebControls.DataGridCommandeventHandler (this.DataGrid1_cancelcommand);</p> <p>This.DataGrid1.editcommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_editCommand);</p> <p>This.DataGrid1.updateCommand = new system.web.ui.webcontrols.dataGridCommandeventHandler (this.DataGrid1_updateCommand);</p> <p>this.DataGrid1.DeleteCommand = new System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_DeleteCommand); this.DataGrid1.SelectedIndexChanged = new System.EventHandler (this.DataGrid1_SelectedIndexChanged);</p> <p>This.Load = New System.EventHandler (this.page_load);</p> <p>}</p> <p>#ndregion</p> <p>Private Void LinkButton1_Click (Object Sender, System.Eventargs E)</p> <p>{</p> <p>AddPublisher ();</p> <p>}</p> <p>/// <summary></p> <p>/// Add to</p> <p>/// </ summary></p> <p>Private void addpublisher ()</p> <p>{</p> <p>String SQL = "INSERT INTO PUBLISHERS (Pub_ID, Pub_Name, City, State, Country) VALUES (@ pubid, @ public, @ city, @ state, @ country);</p> <p>SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS");</p> <p>SQLCommand cmd = new SQLCOMMAND (SQL, CON);</p> <p>Cmd.Parameters.Add (New Sqlparameter ("@ pubid", sqldbtype.char, 4));</p> <p>CMD.Parameters ["@ pubid"]. value = this.textbox1.text;</p> <p>cmd.Parameters.Add (New Sqlparameter ("@ Pubname", SqldbType.varchar, 40));</p> <p>CMD.Parameters ["@ Pubname"]. Value = this.TextBox2.Text;</p> <p>CMD.Parameters.Add (New Sqlparameter ("@ city", sqldbtype.char, 20));</p> <p>Cmd.Parameters ["@ City"]. Value = this.TextBox3.Text;</p> <p>Cmd.Parameters.Add (New Sqlparameter ("@ State", SqldbType.char, 2));</p> <p>CMD.Parameters ["@ State"]. Value = this.TextBox4.Text;</p> <p>Cmd.Parameters.Add (New SqlParameter ("@ country", sqldbtype.varchar, 30));</p> <p>Cmd.Parameters ["@ country"]. value = this.textbox5.text;</p> <p>cmd.connection.open ();</p> <p>cmd.executenonquery ();</p> <p>cmd.connection.close ();</p> <p>THIS.TEXTBOX1.TEXT = "";</p> <p>THIS.TEXTBOX2.TEXT = "";</p> <p>THIS.TEXTBOX3.TEXT = ""</p> <p>This.TextBox4.text = ""; this.textbox5.text = ";</p> <p>this.bindgrid ();</p> <p>}</p> <p>Private void bindgrid ()</p> <p>{</p> <p>SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS");</p> <p>Sqldataadapter mysqlcom = new sqldataadapter ("SELECT * FROM PUBLISHERS WHERE PUB_ID LIKE '99% '", CON);</p> <p>DataSet myds = new dataset ();</p> <p>MySQLCOM.FILL (MyDS, "Publishers");</p> <p>This.DataGrid1.datasource = myds.tables ["publishers"]. defaultview;</p> <p>THIS.DATAGRID1.DATABIND ();</p> <p>}</p> <p>/// <summary></p> <p>// // click to trigger events when editing</p> <p>/// </ summary></p> <p>/// <param name = "source"> </ param></p> <p>/// <param name = "e"> </ param></p> <p>Private void DataGrid1_EditCommand (Object Source, System.Web.ui.WebControls.DataGridCommandEventArgs E)</p> <p>{</p> <p>This.DataGrid1.edititeMindex = (int) E.Item.ItemIndex;</p> <p>this.bindgrid ();</p> <p>}</p> <p>/// <summary></p> <p>/// Click Cancel to trigger the event.</p> <p>/// </ summary></p> <p>/// <param name = "source"> </ param></p> <p>/// <param name = "e"> </ param></p> <p>Private void DataGrid1_cancelcommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)</p> <p>{</p> <p>This.DataGrid1.columns [0] .Headertext = "Caquered";</p> <p>THIS.DATAGRID1.EDITITEMINDEX = -1;</p> <p>this.bindgrid ();</p> <p>}</p> <p>Private void DataGrid1_selectedIndexchanged (Object Sender, System.EventArgs E)</p> <p>{</p> <p>}</p> <p>/// <summary></p> <p>// / Trigger the event when you click Update.</p> <p>/// </ summary></p> <p>/// <param name = "source"> </ param></p> <p>/// <param name = "e"> </ param></p> <p>Private void DataGrid1_UpdateCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventArgs E)</p> <p>{</p> <p>SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS"); sqlcommand selectcmd = con.createCommand ();</p> <p>Selectcmd.commandtype = commandtype.text;</p> <p>Selectcmd.commandtext = "SELECT * from Publishers where Pub_id Like '99% '"</p> <p>SqlDataAdapter SQLADATPER = New SqlDataAdapter ();</p> <p>SQLADATPER.SELECTCOMMAND = SELECTCMD;</p> <p>DataSet DS = New DataSet ();</p> <p>C.Open ();</p> <p>SQLADATPER.FILL (DS, "Publishers");</p> <p>C. close ();</p> <p>Sqlcommand updatecmd = con.createcommand ();</p> <p>Updatecmd.commandtext = "Update Publishers Set Pub_Name = @ Pubname, City = @ City, State = @ State, Country = @ Country WHERE PUB_ID = @ Pub_ID";</p> <p>Sqlparameter PubnamePar = New SqlParameter ("@ Pubname", SqldbType.varchar, 40, "Pub_Name");</p> <p>Updatecmd.Parameters.Add (PubnamePar);</p> <p>SQLParameter Citypar = New Sqlparameter ("@ City", Sqldbtype.varchar, 20, "City");</p> <p>Updatecmd.Parameters.Add (CityPar);</p> <p>SQLParameter StatePar = New SqlParameter ("@ State", Sqldbtype.char, 2, "State");</p> <p>Updatecmd.Parameters.Add (statepar);</p> <p>SQLParameter CountryPar = New Sqlparameter ("@ country", sqldbtype.varchar, 30, "country);</p> <p>Updatecmd.Parameters.Add (CountryPar);</p> <p>SQLParameter PubidPar = New SqlParameter ("@ pub_id", sqldbtype.char, 4, "pub_id");</p> <p>PubidPar.SourceVersion = DATAROWVERSION.ORIGINAL;</p> <p>Updatecmd.Parameters.Add (PubidPar);</p> <p>SQLADATPER.UPDATECOMMAND = UpdateCMD;</p> <p>DataTable Table = DS.TABLES ["Publishers"];</p> <p>Table.primaryKey = new datacolumn []</p> <p>{</p> <p>Table.columns ["pub_id"]</p> <p>}</p> <p>DataRow Row = Table.Rows.Find ((TextBox) (E.Item.cells [2] .controls [0])). Text);</p> <p>ROW ["pub_name"] = ((TextBox) (E.Item.cells [3] .controls [0])). Text; row ["city"] = ((TextBox) (E.Item.cells [4] .Controls [0])).</p> <p>ROW ["State"] = ((TextBox) (E.Item.cells [5] .controls [0])).</p> <p>ROW ["country"] = ((TextBox) (E.Item.cells [6] .controls [0])).</p> <p>C.Open ();</p> <p>SQLADATPER.UPDATE (TABLE);</p> <p>C. close ();</p> <p>THIS.DATAGRID1.EDITITEMINDEX = -1;</p> <p>this.bindgrid ();</p> <p>}</p> <p>/// <summary></p> <p>// / Trigger the event when you click delete</p> <p>/// </ summary></p> <p>/// <param name = "source"> </ param></p> <p>/// <param name = "e"> </ param></p> <p>Private void DataGrid1_DeleteCommand (Object Source, System.Web.ui.WebControls.DataGridCommandeventargs E)</p> <p>{</p> <p>SqlConnection Con = New SqlConnection ("Server = Accp-lzh; UID = SA; PWD = Sasa; Database = PUBS");</p> <p>SQLCommand selectcmd = con.createcommand ();</p> <p>Selectcmd.commandtext = "SELECT * from Publishers where Pub_id Like '99% '"</p> <p>SQLCommand deletecmd = con.createcommand ();</p> <p>Deletecmd.commandtext = "delete from public" where pub_id = @ pub_id ";</p> <p>SQLParameter PubidPar = New SqlParameter ("@ pub_id", sqldbtype.char, 4, "pub_id");</p> <p>PubidPar.SourceVersion = DATAROWVERSION.ORIGINAL;</p> <p>Deletecmd.Parameters.Add (PubidPar);</p> <p>SqlDataAdapter SQLADAPTER = New SqlDataAdapter ();</p> <p>SQLADAPTER.SELECTCOMMAND = SELECTCMD;</p> <p>SQLADAPTER.DELETECMMMAND = deletecmd;</p> <p>DataSet DS = New DataSet ();</p> <p>C.Open ();</p> <p>SQLADAPTER.FILL (DS, "Publishers");</p> <p>DataTable Table = new dataable ();</p> <p>Table = DS.TABLES ["Publishers"];</p> <p>Table.primarykey = new datacolumn [] // Defines the primary key to facilitate finding</p> <p>{</p> <p>Table.columns ["pub_id"]</p> <p>}</p> <p>DataRow Row = Table.Rows.Find (E.Item.cells [2] .text);</p> <p>Row.delete ();</p> <p>SQLADAPTER.UPDATE (TABLE); con.close ();</p> <p>THIS.DATAGRID1.EDITITEMINDEX = -1;</p> <p>this.bindgrid ();</p> <p>}</p> <p>Private void DataGrid1_disposed (Object Sender, System.EventArgs E)</p> <p>{</p> <p>}</p> <p>}</p> <p>}</p> <p>The following is a screenshot of modification:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-46498.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="46498" 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 = 'ygIt_2BvxWfI_2FKYYYbuT0BLGEwfhkuMzE_2FPruRVgtOnTQmqK5BCkicKOy2FxApC7f2hvrc_2FIWt38F3jzUo34hjAw_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>