asp+| Note 5). Pagelet Control
The easiest way to make your own controls is to pagelet the control, with a suffix of. ASPC, which is included with the Register directive in the WebForm:
<%@ Register tagprefix= "ACME" tagname= "message" src= "PAGELET1.ASPC"%>
TagPrefix is the Pagelet namespace definition, tagname is the name given to Pagelet. The SRC attribute is a virtual directory.
Use of Pagelet:
<acme:message runat= "Server"/>
。 Exposing Pagelet methods, defining attributes like a class, for example:
<script language= "C #" runat= "Server" >
Public String address{
get{
return txtaddress.value;
}
set{
Txtaddress.value=value;
}
}
</script>
<input id= "txtaddress" runat= "Server" >
。 Encapsulating events
Slightly
As you can see, pagelet can replace the role of include files.
。 To create a Pagelet object programmatically
You can use the program to generate an instance of the Pagelet control, such as:
Control Cl=loadcontrol ("PAGELET2.ASPC");
((PAGELET2_ASPC) CL). category= "Business";
PAGE.CONTROLS.ADD (CL);
Note that because the LoadControl function returns a System.Web.UI.Control object, it requires styling. Pagelet
Type is the filename and the dot is underlined.
6). Data Help Control
In asp+, not only can you help set the database, you can also help to set a simple set of attributes, or even the result of a method call, such as:
CustomerID: <%# CustID%>
<asp:listbox id= "List1" datasource= <%# myarray%> ' runat= ' server ' >
<%# (Customer.firstname + customer.lastname)%>
<%# getbalance (CustID)%>
It looks like the <%=%> in ASP, but the ASP is replaced with Response.Write, while Asp+ is in DataBind ()
method is called to help set the. The data Help control and Page objects have DataBind () methods.
Note that the asp+ is strongly typed, so:
<%# count. ToString ()%> is only right.
。 DataBinder.Eval ()
A static method, supported by the asp+, allows the data to be fixed and optionally converted to a string, which can be
For templates. If you do not need to eval, then like:
<%# String.Format ("{0:c}", ((DataRowView) container.dataitem) ["Someinteger"])%>
You can use eval:
<%# DataBinder.Eval (Container.DataItem, "Someinteger", "{0:c}")%>
7). Server-side data access
。 connections, commands, and Datasets (datasets)
NGWS provides a series of managed data access APIs that introduce namespaces:
<%@ Import namespace= "System.Data"%>
<%@ Import namespace= "System.Data.SQL"%>
a). Generate SqlConnection
b). Structural Sqldatasetcommand
c). FillDataSet
If it is an insert, replace the Sqldatasetcommand with SqlCommand.
Remember: Always close the connection after you run out of connections! Otherwise, the resource will not be released by the garbage collection mechanism until the page is released.
。 Help set the data to the DataGrid, for example:
....
DataSet ds = new DataSet ();
Mycommand.filldataset (ds, "Authors");
Mydatagrid.datasource = ds. tables["Authors"]. DefaultView;
Mydatagrid.databind ();
。 parameterized queries
Sqldatasetcommand support parameter, "@" as parameter prefix, example:
MYCOMMAND.SELECTCOMMAND.PARAMETERS.ADD (New SqlParameter ("@state", sqldatatype.varchar,2));
mycommand.selectcommand.parameters["@state"]. Value=newvalue;
Note that if the DataGrid is to rotate between the client and the server, be sure to set the Maintainstate property to False.
Otherwise, performance is affected, such as:
<asp:datagrid id= "Mydatagrid" runat= "Server"
Maintainstate= "false"/>
。 Inserting data
You should guarantee that it is not null and you can use the Try/catch block.
Cases:
String insertcmd = "INSERT into Authors values (@id, @lname)"
SQLCommand Myc = new SQLCommand (insertcmd,myconn);
Myc. parameters["@id"]. value=cu_id. Value;
Myc. parameters["@lname"]. Value=au_lname. Value;
Myc. Activeconnection.open ();
try{
Myc. Execute ();
}catch (SQLException e) {
if (e.number==2627)
.//PRIMARY KEY repeat
}
Myc. Activeconnection.close ();
。 Update records
The DataGrid provides some intrinsic support, with an integer attribute, EditItemIndex, indicating which row can be edited, one but set,
The text input box for the row is displayed, not the static text of the province, as-1 means no rows can be edited.
The DataGrid can contain a control, editcommandcolumn, which will cause three events for the DataGrid, EditCommand,
UpdateCommand, and CancelCommand. EditCommandColumn are defined in attributes with columns, such as:
<asp:datagrid id= "Mydatagrid" runat= "Server"
......
Oneditcommand= "Mydatagrid_edit"
Oncancelcommand= "Mydatagrid_cancel"
Onupdatecommand= "Mydatagrid_updat
[1] [2] Next page