Wonderful recommendation:. 10 Tips for NET programming

Source: Internet
Author: User
Tags config soap unique id web services win32 client wsdl
Programming | skills. NET programming 10 points skill
--------------------------------------------------------------------------------

Whether you are interested in Windows Forms, ASP. NET, Web services, or interested in the. NET framework, these tips help you explore new. NET technology.
by Dino Esposito


The. NET framework is larger than ever and contains a large number of classes and methods, but the development community has not yet explored and understood most of the incredible software features that may have started as bugs or design flaws, and may be considered a major improvement the second time they occur. There is more thinking.

Following this inevitable process of optimization, sharing skills with other developers is one that allows you to build your first. NET application, although it is far from being able to provide the definitive solution you are looking for. I have summed up what can make you. NET development is more efficient 10 techniques. To help developers as much as possible, these techniques cover the entire range of the technology, from Ado.net to ASP.net, from the CLR to the framework, from Windows Forms to Web Services. These tips, plus what I can expect, will have a big impact on developers.


#1建立你的DataGrid的页脚

The footer of the DataGrid control plays an important role-that is, the footer summarizes some of the content in the page. The footer is invisible unless you set Showfooter to True. Once you've set it up, the footer will appear, but it has the same number of columns as the other rows. If you think this can be, then there is no problem. Otherwise, you'll need to add fields to the footer or remove the existing fields.

Onitemcreated is a critical invocation event. It is the beginning of an event handler: ListItemType itemType = _e.item.itemtype;
if (ItemType = = Listitemtype.footer)
{
E.item.cells.removeat (1);
E.item.cells.removeat (1);
Cellsummary.columnspan = 3;
E.item.cells[0]. Text = "...";
}



Make sure that you can intervene when the footer is established, and then get the instance displayed in the footer row. This allows you to increase and decrease the unit at will. Don't forget to set the ColumnSpan property to the initial value of the column to avoid the problem that is displayed, such as the footer line is longer or shorter than the other grid.


#2 use the Cache object instead of the Application object

In ASP, you use the Application object to discover global events and store global scope data. Assuming that many different users have access to the same data, you must use the lock and unlock methods to achieve continuous access and avoid access conflicts or other unpredictable results. In ASP.net, with the Application object, and the cache object, it and application have the same characteristics, but in the end can be in more than one occasion to do application alternatives.

Both the cache object and the Application object are collection data containers, and their contents can span the page and the session domain. Neither of them supports Web farm and Web garden scenarios. Web Farm is a network server structure in which multiple servers run an application. Web Garden is a program that runs on multiple processes on the same server.

Unlike application, the cache object is not instantiated the first time the application is tuned. It is created only when you use it. In addition, the cache object can reduce the amount of memory space its data items occupy as much as possible. You can set a relative or absolute expiration date for the cache, or you can set the priority and decay factor. Then you have greater control over your global data state, and you can perform special programs that are moderately degraded in low memory conditions. Also, the cache object is thread safe and does not require locking and unlocking.


#3 call service with GET or post

A Web service is fundamentally a URL that you connect to via HTTP. When you create a proxy class for a given network service, unless otherwise explained, command-line tool Wsdl.exe provides you with a class that uses the SOAP protocol to make it possible to invoke a network service remotely.

Although the SOAP protocol is considered a standard for calling a remote program, nothing prevents you from using a simple get or post method to invoke the. NET Network Service. By the way, the post command passes the SOAP payload.

You can create this feature directly in the proxy class, as long as you convert the protocol to HttpGet or httppost when the Wsdl.exe device is invoked. The corresponding adjustment of the source code of the package class. You can also use XMLHttpRequest objects and simple scripts or compiled code to invoke network services (see Resources for more information): Set http = _createobject ("Microsoft.XMLHTTP")
Http.open "Get", "Http://server/service.asmx/MethodName", False
Http.send ""
MsgBox Http.responsetext



By using XMLHttpRequest, you can connect network services through WIN32, scripts, and, in general, non. NET code.

#4 Use cookieless Sessions

In ASP.net, the session object refers to a software configuration entity where you can set up a process or out-of-process pattern. The session object reads many run-time settings, which you can either exist in the installation directory of the Web server's framework or the bin directory of the application that is derived from the Web.config file. Web.config files to determine the settings for many ASP.net components. If you put a copy of Config.web in your application's local subdirectory, this setting overrides the default setting in the Framework directory.

You want to configure whether the session manager can recognize the client's session using cookies. Set the following session manager in the application's Config.web file to work without using cookies. <sessionstate cookieless= "true"/>




#5 use a custom grid paging

The DataGrid control for the Web form has built-in support paging. It automatically displays a page of pagination, with almost no code to write, to display the size of a given record. However, by default, all required records are hidden in the DataSource property of the DataGrid object. This means that you have to retrieve all the data at once. Although this is not a problem when there are few records, it becomes a problem when there are thousands of records. In this case, you have to manually retrieve the records and put them in the disk instead of the database management system.

You must inform the DataGrid of this particular behavior. Fortunately, the DataGrid provides a Allowcustompagination property that, when set to true, greatly alters the built-in behavior of the control. At this point, the grid always reads the records found from the DataSource property from beginning to end. You should supplement this container with data that belongs to the current page. In general, grid itself takes only the records from the DataSource property that belong to this page.


#6 mount keyword information from a database

The DataTable object lets you set the primary key in the memory of a particular table. This behavior accelerates the search between the DataTable and the associated DataView object. Another advantage is to prevent repetitive entry, which interferes with the integrity of the table. In this way, you have enough time to fix the table before the data is submitted to the database for a batch update, and even give the user the appropriate warning.

You can manually set this information in batch code://DS is an existing dataset
Datacolumn[] keys = new datacolumn[1];
DataTable dt = ds. tables["MyTable"];
Keys[0] = dt. columns["ID"];
Dt. PrimaryKey = keys;



You can automatically set and detect primary key information when populating a dataset. It does this: you set the MissingSchemaAction property of the data adapter to perform the following query code to automatically diagnose the information. SqlDataAdapter da = new SqlDataAdapter (Strcmd, strconn);
DataSet ds = new DataSet ();
Da. MissingSchemaAction = MissingSchemaAction.AddWithKey;
Da. Fill (ds, "MyTable");




#7 checkboxes to choose from

In ASP, you have a few checkboxes, all have the same name: <input type= "checkbox" Name= "foo" value= "..." >



You can get the corresponding value of the checkbox from the following line code: <% a = Split (Request.Form ("foo"), ",")%>



Request.Form ("foo") returns a comma-isolated string that is formed from the string values of all the selections. You give this string a separate function in VBScript, and you get an easy to manage structure, like an array.

The same code will have no way to run in asp.net. If you use a <asp:checkbox> server-side control. To make it work, use the HtmlInputCheckBox control, write the code as follows: <type= "checkbox" runat= "Server" Name= "foo" value= "..." >



Even if the asp:checkbox and input type= "checkbox" service end tags are equivalent to the same HTML code, ASP. NET ensures that the unique ID and name of the label for the other controls in the Asp:checkbox and <asp> namespaces are output.

#8 Automatic master Table/detail Table browsing

If you use the DataGrid control in a Windows Forms application to display the primary table/detail table. The change is in the framework for you to automatically display the synchronization.

The key is to establish a combination of data associations between the two tables and a data source that defines the detailed table. Let's say you have two tables: Customers and Orders, all of which have custid fields. You establish an association with the ADO. NET DataRelation object://DS is an existing DataSet
Dim DC1, DC2 as DataColumn
DC1 = ds. Tables ("Customers"). Columns ("CustID")
DC2 = ds. Tables ("Orders"). Columns ("CustID")
Dim R as DataRelation
r = New DataRelation ("ordersByCustomer", DC1, DC2)
Ds. Relations.Add (R)



Whenever you use a DataRow object to represent a row in the primary table (Customers), in this case, you can use the DataRow getchildrows method to get the data of the child table based on the association. In the case of Windows Forms, this programming process uses the following code: Dgrid.datasource = ds
Dgrid.datamember = "Customers.ordersbycustomer"



When you use an expression such as a mastertable.relation connection detail child table, its contents are automatically updated correctly.


#9 implement file change notification with class

. NET to the Win32 file notification class function integration into the FileSystemWatcher class. These core classes are primarily responsible for notifying the client application when a change in the system file layer has been detected. When a file is created, modified, renamed, or deleted, the notification object sends a signal describing the application state, according to specific parameters.

For WIN32 programs, it is difficult to understand the name of the file under Windows NT and indows 2000 and the cause of the related events. All of this in. NET is replaced by the FileSystemWatcher class. FileSystemWatcher watcher = new FileSystemWatcher ();
Watcher. Path = "c:\\";
Watcher. Filter = "*.txt";
Watcher. NotifyFilter = Notifyfilters.lastwrite;



Once the object is configured, you start detection: Watcher. EnableRaisingEvents = true;



Any detected event activates the application event. You can record events like the following: Watcher. Changed + = new FileSystemEventHandler (onchanged);



The event handler parameter provides all the files and information you need.


#10 Compiling code

The. NET Framework provides classes that allow you to compile your code in the language you specify. They are wrapped in System.CodeDom.Compiler namespaces. The following code fragment shows how to get an instance of the C # compiler running in memory: CSharpCodeProvider csc = new CSharpCodeProvider ();
ICodeCompiler ICC = csc. CreateCompiler ();



Next, you will set some input parameters for the CompilerParameters class: CompilerParameters co = new CompilerParameters ();
Co. outputassembly = "Foo.EXE";
Co. Referencedassemblies.add ("System.dll");



You must specify at least the executable name, set GenerateExecutable to False if you want a DLL, and add a list of collections for reference: Icc.compileassemblyfromfile (Co,csfile);

To run the compilation process, use Compileassemblyfromfile () to pass the parameter and the source file name to it. You can use the class CompilerResults to learn more about the newly generated collections.

About the Author:
Dino Esposito is a major working trainer and consultant in Rome, Italy. He is also the author of a ado.net book to be released by Microsoft Publishing House, and spends most of his time teaching asp.net and ado.net courses for the Wintellect website. If you have some technical problems, you can contact him by dinoe@wintellect.com e-mail address.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.