On the page of a project, you will often encounter interface values and values. In particular, when there are many items on the page, values and values are usually physical.
A simple helper is added to the component. The value is implemented through request. From [""], and document. getelementbyid (""). value is implemented when the value is assigned.
Let's take a look at the sample code:
Example: Products table of the northwind database
Page code:
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
Productname: <asp: textbox runat = "server" id = "txt_productname"> </ASP: textbox> <br/>
Supplierid: <asp: textbox runat = "server" id = "txt_supplierid"> </ASP: textbox> <br/>
Categoryid: <asp: textbox runat = "server" id = "txt_categoryid"> </ASP: textbox> <br/>
Quantityperunit: <asp: textbox runat = "server" id = "txt_quantityperunit"> </ASP: textbox> <br/>
Unitprice: <asp: textbox runat = "server" id = "txt_unitprice"> </ASP: textbox> <br/>
Unitsinstock: <asp: textbox runat = "server" id = "txt_unitsinstock"> </ASP: textbox> <br/>
Unitsonorder: <asp: textbox runat = "server" id = "txt_unitsonorder"> </ASP: textbox> <br/>
Reorderlevel: <asp: textbox runat = "server" id = "txt_reorderlevel"> </ASP: textbox> <br/>
Discontinued: <asp: checkbox id = "txt_discontinued" runat = "server"/> <br/>
<Asp: button id = "button1" runat = "server" text = "Submit Insert" onclick = "button#click"/>
<Asp: gridview id = "gridview" runat = "server">
</ASP: gridview>
</Form>
</Body>
</Html>
Background assignment code:
Products product = DbSession.Default.From<Products>().ToFirst();EntityUtils.SetDocumentValue<Products>(product, "txt_");
Then let's look at the execution results:
The value has been assigned successfully.
The specific implementation is to assign values through JS:
var txt_ProductID=document.getElementById('txt_ProductID');if(txt_ProductID)txt_ProductID.value='1';
var txt_ProductName=document.getElementById('txt_ProductName');if(txt_ProductName)txt_ProductName.value='char\'\\\\s\'';
var txt_SupplierID=document.getElementById('txt_SupplierID');if(txt_SupplierID)txt_SupplierID.value='1';
var txt_CategoryID=document.getElementById('txt_CategoryID');if(txt_CategoryID)txt_CategoryID.value='1';
var txt_QuantityPerUnit=document.getElementById('txt_QuantityPerUnit');if(txt_QuantityPerUnit)txt_QuantityPerUnit.value='10 boxes x 20 bags';
var txt_UnitPrice=document.getElementById('txt_UnitPrice');if(txt_UnitPrice)txt_UnitPrice.value='150.8000';
var txt_UnitsInStock=document.getElementById('txt_UnitsInStock');if(txt_UnitsInStock)txt_UnitsInStock.value='39';
var txt_UnitsOnOrder=document.getElementById('txt_UnitsOnOrder');if(txt_UnitsOnOrder)txt_UnitsOnOrder.value='0';
var txt_ReorderLevel=document.getElementById('txt_ReorderLevel');if(txt_ReorderLevel)txt_ReorderLevel.value='10';
var txt_Discontinued=document.getElementById('txt_Discontinued');if(txt_Discontinued){try{txt_Discontinued.checked=true;}catch(err){txt_Discontinued.value='1'}}
In fact, not all of them are suitable for use.
Click Submit to add a record.
The background code of the button:
Products Product = new products (); // obtain the entityutils value entered on the page. updatemodel <Products> (product, "TXT _"); // Add dbsession. default. insert <Products> (product );
The running result is as follows:
A piece of data has been successfully added and queried.
The "TXT _" parameter of the value assignment and value is the prefix of the control. In this way, the control can be distinguished, and multiple entities can coexist on one page, and values are assigned to each other, does not affect each other.
You can also assign values based on specific fields. For details, see other heavy loads of the setdocumentvalue method.
Download