In the past two days, a management tool for the main site web was evicted. Its main functions are to manage the existing main site web, including site management, channel management, and user management. The function is very simple and there are few pages. Because it is the second web project, the process or some gains, or some problems need to be further studied, in short, first recorded, so as not to forget for a long time.
1. Use css to control the interface appearance
In the previous web project, the layout mainly depends on the table. One or more tables are used to divide the page into multiple areas and set different styles for each area. Before doing this, I checked some information on the Internet and found that css is more popular to control the output of the interface, and in fact it is easier to control, because I split the interface performance and content, the style definition is placed in a separate css file, and the html file code looks clearer.
I found an entry-level article on the Internet, which uses css + div for webpage layout. It is also good to see it as an entry-level document. I also refer to this article for a lot of code this time:
Http://www.tblog.com.cn/article.asp? Id = 283
2. cache data on pages
Global variables on a page. If the page is postback, the variable data will be lost. The data on the page control still exists after the postback, depending on the viewstate. However, the variables defined by myself cannot be remembered through viewstate, so we need to solve them through other methods.
In the past, one of my methods was to use session. However, this method has not been used to verify its reliability and efficiency on a large scale. I don't know if it will cause problems in many places. (Previously, I used vb to access the session. How can I do this in c ?)
This time I used a method to convert the global variable to be remembered into a string for expression (because the type of this global variable is relatively simple, each attribute is connected using a string, the intermediate interval is signed, which is easy to convert and restore), and then the string is assigned to an attribute of a page control (my value is the value of each item in listbox ), in this way, the value of postback is not lost because of the viewstate Of The listbox. I don't know if this is very unprofessional.
In addition, you should try to encapsulate this mechanism and store it with other controls. For example, the hiddenfield interface should also be more common, so it is more convenient to use it.
This is used to store data on a page. If you want to store a connection, you only know to use the session (profile? It may be similar to session ). The Session status should be stored in two places: client and server. The client is only responsible for saving the SessionID of the corresponding website, while other Session information is stored on the server. In ASP, the SessionID of the client is actually stored as a Cookie. If the user chooses to disable cookies in the browser settings, then he will not be able to enjoy the convenience of the Session, or even access some websites. To solve the above problems, the Session information storage methods of the client in ASP. NET are divided into Cookie and Cookieless.
3. data transmission between pages
The session application is mentioned above. Suddenly there is a way to transmit data between pages in asp.net, one of which is session.
The method is as follows:
A. session
Session access can be directly accessed using Session on the page. In other custom classes, you can access it through HttpContext. Current. Session ["CustomerID. (What is the difference between the two ?)
If you want to access the session in various parts of the project to directly use the session, it may lead to a loss of control (such as session name management ).
A static class is created to save the data in the entire session:
Class Definition:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public static class DataCache
{
Public static string CustomerID
{
Get
{
Return (string) HttpContext. Current. Session ["mermerid"];
}
Set
{
HttpContext. Current. Session ["mermerid"] = value;
}
}
}
Assignment
DataCache. CustomerID = this. TextBox1.Text. Trim ();
Access
This. Label1.Text = "Welcome" + DataCache. CustomerID;
In this way, the information that DataCache saves data through session is hidden from the outside code. You can even make the DataCache information storage mechanism into another one without affecting external calls (such as frofile)
Since DataCache is a static class, all sessions will use the same DataCache. Will there be a critical section problem?
It should not. Although DataCache is the same, each Session actually accessed by DataCache is independent, so no.
B. querystring
C. Use server. transfer
Not very convenient
4. Use of the GridView
GirdView is added to aps. net2.0 and has powerful functions.
1. Bind dataview to the gridview (dataview is better than the able, because dataview has better control)
First, construct a datatable and add data rows:
Private void MakeTable ()
{
// Create a new DataTable.
System. Data. DataTable table = new DataTable ("MyTable ");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to able.
Column = new DataColumn ();
Column. DataType = System. Type. GetType ("System. Int32 ");
Column. ColumnName = "Column 1 ";
Column. ReadOnly = true;
Column. Unique = true;
// Add the Column to the DataColumnCollection.
Table. Columns. Add (column );
// Create second column.
Column = new DataColumn ();
Column. DataType = System. Type. GetType ("System. String ");
Column. ColumnName = "Column 2 ";
Column. AutoIncrement = false;
Column. Caption = "Column 2 ";
Column. ReadOnly = false;
Column. Unique = false;
// Add the column to the table.
Table. Columns. Add (column );
// Make the ID column the primary key column.
DataColumn [] PrimaryKeyColumns = new DataColumn [1];
PrimaryKeyColumns [0] = table. Columns ["Column 1"];
Table. PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
DataSet = new DataSet ();
// Add the new DataTable to the DataSet.
DataSet. Tables. Add (table );
// Create three new DataRow objects and add
// Them to the DataTable
For (int I = 0; I <= 2; I ++)
{
Row = table. NewRow ();
Row ["Column 1"] = I;
Row ["Column 2"] = "name" + I;
Table. Rows. Add (row );
}
}
BIND in pageload:
MakeTable ();
View = new DataView (dataSet. Tables ["MyTable"]);
GridView1.DataSource = view;
GridView1.DataBind ();
DataSet is a global variable.
2. Add a Custom column (TemplateField)
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<Asp: TextBox ID = "TextBox1" runat = "server" Text = "Please edit"> </asp: TextBox>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
Access this column in c:
Then access this column in the server code as follows:
TextBox txt = (TextBox) GridView1.Rows [0]. Cells [0]. Controls [1];
Or
TextBox txtBOX = (TextBox) GridView1.Rows [0]. FindControl ("TextBox1 ");
To access a normal boundfield, you only need GridView1.Rows [0]. Cells [0]. text.
You can also bind the templatefield column to the database data:
<Columns>
<Asp: TemplateField>
<ItemTemplate>
<Asp: TextBox ID = "TextBox1" runat = "server" Text = '<% # Bind ("Country") %>'> </asp: TextBox>
</ItemTemplate>
</Asp: TemplateField>
</Columns>
You can also bind a database (datatable) in the c # code, and use the code to add a templatefield and display the Binding data:
1. Define a class to implement the ITemplate Interface
Public class LabelTemplateField: ITemplate
{
// A variable to hold the type of ListItemType.
ListItemType _ templateType;
// A variable to hold the column name.
String _ columnName;
// Constructor where we define the template type and column name.
Public LabelTemplateField (ListItemType type, string colname)
{
// Stores the template type.
_ TemplateType = type;
// Stores the column name.
_ ColumnName = colname;
}
Void ITemplate. InstantiateIn (System. Web. UI. Control container)
{
Switch (_ templateType)
{
Case ListItemType. Header:
Break;
Case ListItemType. Item:
Label lb = new Label ();
Lb. ID = "Label1 ";
Lb. DataBinding + = new EventHandler (lb_DataBinding); // Attaches the data binding event.
Container. Controls. Add (lb); // Adds the newly created textbox to
Container.
Break;
Case ListItemType. EditItem:
Break;
Case ListItemType. Footer:
Break;
}
}
Void lb_DataBinding (object sender, EventArgs e)
{
Label txtdata = (Label) sender;
GridViewRow container = (GridViewRow) txtdata. NamingContainer;
Object dataValue = DataBinder. Eval (container. DataItem, _ columnName );
If (dataValue! = DBNull. Value)
{
Txtdata. Text = dataValue. ToString ();
}
}
}
2. Bind data and add the templatefield column (in the pageload event)
GridView1.DataSource = view;
TemplateField tf1 = new TemplateField ();
Tf1.HeaderText = "Column 1 ";
Tf1.ItemTemplate = new LabelTemplateField (ListItemType. Item, "Column 1 ");
GridView1.Columns. Add (tf1 );
GridView1.DataBind ();
3. Use hyperlinkfield
Join hyperlinkfield and navigate:
<Asp: hyperlinkfield datatextfield = "UnitPrice"
Datatextformatstring = "{0: c }"
Datanavigateurlfields = "ProductID"
Datanavigateurlformatstring = "~ \ Details. aspx? ProductID = {0 }"
Headertext = "Price"
Target = "_ blank"/>
Access it in c # code:
HyperLink href = (HyperLink) row. Cells [0]. Controls [0];
String txt = href. Text;
4. Refresh the bound data
If it is just a simple page delivery, data will not be refreshed in the gridview.
You must call gridview. databind on the server to refresh the data.
5. Create an IIS virtual website
Because this web management tool has high permissions, You need to deploy it in the Intranet environment for security reasons, and the Internet users cannot access it.
Therefore, you need to create a new website in IIS (cannot create multiple websites in IIS that comes with XP ?).
First, create a local directory where the page files are placed. Create a website. There are several default parameters that need to be changed. Otherwise, asp.net access is not allowed.
In the home directory of a website, you must check the following information: Reading, directory browsing, record access, and indexing resources.
Select "tutorial-only" in the execution permission.
The access address is as follows: http: // 192.168.1.2: 5150, which does not need to be followed by the virtual website name.
6.asp.net script callback
The asp.net script callback provides a non-page refresh and data exchange mechanism.
Steps
A. Interface for page implementation that requires script callback: ICallbackEventHandler.
If this interface is implemented, two methods are required:
(1) string ICallbackEventHandler. GetCallbackResult ()
(2) void ICallbackEventHandler. RaiseCallbackEvent (string eventArgument)
B. use javascript to write local code in the page source:
<Script type = "text/javascript">
SetTimeout ("timetick()", 1000 );
Function TimeTick ()
{
CallServer ("refresh ");
SetTimeout ("timetick()", 1000 );
}
Function CallServer (obj)
{
Context = gridspan;
Arg = obj;
<% = ClientScript. GetCallbackEventReference (this, "arg", "eseserverdata", "context") %>;
}
Function compute eserverdata (rValue, context)
{
If (rValue! = '')
Context. innerHTML = rValue;
}
</Script>
In the preceding javascript, timetick is a timer that regularly calls the CallServer function. CallServer called
<% = ClientScript. GetCallbackEventReference (this, "arg", "eseserverdata", "context") %>;
In the input parameters, arg is the parameter passed to the server to tell the server what to do.
Eseserverdata is the name of the callback function, and context is the name of the input control. You can access this context in the callback function. Generally, span is used.
C. Execution Process:
First, request initiated by the client's javascript (B) to enter the server's RaiseCallbackEvent function, where a series of processing is performed.
In this example, the callback mechanism will automatically call GetCallbackResult. This function returns a string, which can be a common string or an html Write.
Used to control the client display interface.
D. GetCallbackResult returns the html string, which is used to control the display of the interface:
Public string RenderControl (Control control)
{
System. IO. StringWriter writer1 = new System. IO. StringWriter
(System. Globalization. CultureInfo. InvariantCulture );
HtmlTextWriter writer2 = new HtmlTextWriter (writer1 );
Control. RenderControl (writer2 );
Writer2.Flush ();
Writer2.Close ();
Return writer1.ToString ();
}
There is a problem: the efficiency is not high, and the data volume for callback to the client is large.