ASP.NET development notes - data binding and operation of text box

zhaozj2021-02-16  77

Recently, when you develop a bank's project, you need to use the text box to bind the data and implement a general action, including paging, add, delete, update the functions of the record, as shown in the figure:

The source code is existing for 9CBS netizens reference and correction.

Private void page_load (object sender, system.eventargs e) {// Place the user code here to initialize the page if (! ispostback) {dataset ds = new dataset ();

String sconn = "server = localhost; uid = sa; pwd = sa; initial catalog = TEMP;"

String Stext = "SELECT * from WJPZ";

SqlDataAdapter cmd = new SqldataAdapter (Stext, Sconn);

cmd.fill (DS, "Emptable");

Session ["MyData"] = DS;

Session ["currentrecord"] = 0;

Session ["RecordCount"] = ds.tables ["emptable"]. ROWS.COUNT-1;

Refresh ();

Disenable ();

}

}

// Method for binding text boxes

Public String getBoundData (String FieldName)

{

DataSet DS = (DataSet) session ["mydata"];

DataTable DT = DS.TABLES ["EMPTABLE"];

INT nROWPOS = (int) session ["currentrecord"];

String buf = dt.rows [nROWPOS] [FieldName] .tostring ();

Return BUF;

}

// Use session to move forward

Public Void Forward (Object Sender, Eventargs E)

{

INT nROWPOS = (int) session ["currentrecord"];

INT ncount = (int) session ["recordcount"];

IF (NROWPOS

{

NROWPOS ;

Session ["CurrentRecord"] = nROWPOS;

Refresh ();

}

Return;

}

// move backward

Public Void Back (Object Sender, Eventargs E)

{

INT nROWPOS = (int) session ["currentrecord"];

IF (NROWPOS> 0)

{

NROWPOS -

Session ["CurrentRecord"] = nROWPOS;

Refresh ();

}

Return;

}

// Edit button event

Public void DataEdit (Object Sender, Eventargs E)

{

ENABLE (); // Activate text box

lnkupdate.visible = true;

LNKCANCEL.Visible = true;

Lnkdel.visible = false;

LinkButton1.enabled = false;

LinkButton2.enabled = false;

}

// Remove button event

Public void DataDelete (Object Sender, Eventargs E) {

String sconn = "server = localhost; uid = sa; pwd = sa; initial catalog = TEMP;"

SqlConnection conn = new sqlconnection (sconn);

String stext = "delete from wjpz where id =" txtid.text;

Sqlcommand cmd = new SQLCOMMAND (Stext, Conn);

Cn.open ();

Try

{

cmd.executenonquery ();

}

Catch

{

Throw;

}

Finally

{

CONN.CLOSE ();

}

Refresh ();

}

// Update button event

Public void DataUpdate (Object Sender, Eventargs E)

{

INT IDS = int32.parse (txtid.text); // Take the ID value of the row to modify, and convert to int type

Up_date (ids); // call Updata

// Refresh data

// refresh ();

Disenable ();

Lnkupdate.visible = false;

Lnkcancel.visible = false;

Lnkdel.visible = true;

LinkButton1.enabled = true;

LinkButton2.enabled = true;

}

Private void loadingagain ()

{

}

Private void up_date (int IDs)

{

String connStr = "Server = localhost; uid = sa; pwd = sa; initial catalog = TEMP;";

SqlConnection conn = new SqlConnection (connStr);

String SQL = "SELECT * from WJPZ";

SqlDataAdapter Ada = New SqldataAdapter (SQL, CONN);

/ / The COMMANDBUILDER object here must not forget, generally written behind the DataAdapter definition

SQLCommandbuilder CB = New SQLCOMMAndBuilder (ADA);

DataSet DS = New Dataset ();

Cn.open ();

Ada.fill (DS, "MEMS");

Conn.close (); // Note Timely closed connection

DataTable DT = DS.TABLES ["MEMS"]; // Establish a DataTable object, easy to operate

Dt.primaryKey = new datacolumn [] {dt.columns ["id"]}; // create a primary key

DataRow DR = DT.ROWS.FIND (IDS); // Find the row that needs to be modified according to ID parameters

DR ["Type"] = txttype.text;

DR ["Submark"] = txtsub.text;

DR ["Type Description"] = TXTTYPEDEScrip.Text;

DR ["file path"] = txtFilepath.Text;

DR ["" type number "] = txttypessign.text;

Ada.Update (DS, "MEMS"); // With DataAdapter's Update () method for database updates}

// Cancel the update button event

Public Void Datacancel (Object Sender, Eventargs E)

{

Disenable ();

Lnkupdate.visible = false;

Lnkcancel.visible = false;

Lnkdel.visible = true;

LinkButton1.enabled = true;

LinkButton2.enabled = true;

}

Public void refresh ()

{

Foreach (Control C in this.Controls [1] .controls)

{

IF (c is system.web.ui.webControls.textbox)

{

Textbox TB = (TextBox) C;

//tb.text="aaron ";

tb.databind ();

}

}

}

Public void disenable ()

{

Foreach (Control C in this.Controls [1] .controls)

{

IF (c is system.web.ui.webControls.textbox)

{

Textbox TB = (TextBox) C;

//tb.text="aaron ";

Tb.enabled = false;

}

}

}

Public void enable ()

{

Foreach (Control C in this.Controls [1] .controls)

{

IF (c is system.web.ui.webControls.textbox)

{

Textbox TB = (TextBox) C;

//tb.text="aaron ";

TB.enabled = True;

}

}

}

#Region web form designer generated code

Override protected void oninit (Eventargs E)

{

//

// Codegen: This call is necessary for the ASP.NET Web Form Designer.

//

InitializationComponent ();

Base.onit (E);

}

///

/// Designer supports the required method - do not use the code editor to modify

/// This method is content.

///

Private vidinitiRizeComponent ()

{

This.Lnkdel.Click = new system.eventhandler (this.DataDelete);

This.lnkadd.click = new system.eventhandler (this.lnkadd_click);

THIS.LNKCONFIGURE.CLICK = New System.EventHandler (this.lnkconfigure_click);

This.Load = New System.EventHandler (this.page_load);

}

#ndregion

// Add button event code

Private void LNKADD_Click (Object Sender, System.Eventargs E)

{

Foreach (Control C in this.Controls [1] .controls)

{

IF (c is system.web.ui.webControls.textbox)

{

TEXTBOX TB = (TextBox) C; TB.Text = ""

}

}

ENABLE ();

Lnkupdate.visible = false;

LNKCANCEL.Visible = true;

Lnkdel.visible = false;

LinkButton1.enabled = false;

LinkButton2.enabled = false;

Lnkconfigure.visible = true;

}

Private void lnkconfigure_click (Object Sender, System.Eventargs E)

{

Disenable ();

Lnkupdate.visible = false;

Lnkcancel.visible = false;

Lnkdel.visible = true;

LinkButton1.enabled = true;

LinkButton2.enabled = true;

Lnkconfigure.visible = false;

String connStr = "Server = localhost; uid = sa; pwd = sa; initial catalog = TEMP;";

SqlConnection conn = new SqlConnection (connStr);

String SQL = "SELECT * from WJPZ";

SqlDataAdapter Ada = New SqldataAdapter (SQL, CONN);

/ / The COMMANDBUILDER object here must not forget, generally written behind the DataAdapter definition

SQLCommandbuilder CB = New SQLCOMMAndBuilder (ADA);

DataSet DS = New Dataset ();

Cn.open ();

Ada.fill (DS, "MEMS");

Conn.close (); // Note Timely closed connection

DataRow Dr = DS.TABLES ["MEMS"]. Newrow ();

DR ["Type"] = txttype.text;

DR ["Submark"] = txtsub.text;

DR ["Type Description"] = TXTTYPEDEScrip.Text;

DR ["file path"] = txtFilepath.Text;

DR ["file extension"] = txtfileext.text;

DR ["Data Name"] = txttablename.text;

DR ["Background Table"] = txtbackname.text;

DR ["optional processing"] = txtOptionhand.text;

DR ["Processing Sign"] = txtSign.Text;

DR [对帐 合并 "= txtmager.text;

DR ["Backup File"] = txtBake.Text;

DR ["Capsule"] = txtmoney.text;

DR ["Document Date"] = txtdate.text;

DR [Add Date] = TxtAdddate.Text;

DR ["settlement date" = txtenddate.text;

DR ["Date Range" = txtdatefan.text;

DR ["field separator" = txtsep.text;

DR ["Field Date type"] = txtdatezidu.text; DR ["Type Serial"] = "99";

DR ["ID"] = "99";

DS.Tables ["MEMS"]. Rows.Add (DR);

Ada.Update (DS, "MEMS"); // With DataAdapter's update () method for updates to the database

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-12474.html

New Post(0)