) Asp.net Summary

Source: Internet
Author: User

1. String concatenation:

String msg = "Hello," + thisUser. Name + ". Today is" + DateTime. Now. ToString ());

More efficient writing:

String msg = string. Format ("Hello, {0}. Today is {1}", thisUser. Name, DateTime. Now. ToString ());

2. Use StringBuilder to splice large amounts of data.

3. String comparison:

If (str! = "") And if (str = "")

More efficient writing:

If (str. Length! = 0) and if (str. Length = 0)

Efficient and readable Syntax:

If (str! = String. Empty) And if (str. Equals (string. Empty)

4. Avoid box and unbox whenever possible. Therefore, if you frequently update the Set content when using a set, we recommend that you use a generic type.

1. Eliminate repeated items in the array.

Recently, when I was working on a project, I required to eliminate repeated items in the array. For example, if there is no LINQ, it seems that I want to write a function to implement this function. However, with linq, this function is quite simple, as shown in the following code:

 

Code
String [] stringArray = {"a", "B", "c", "a", "B", "e "};

Console. WriteLine (stringArray. Distinct (). ToArray (). Length );
Foreach (string s in stringArray. Distinct (). ToArray ())
Console. Write (s );
Console. ReadLine ();

 

Execution result: 4, abce. In this way, only Array. Distinct (). ToArray () is required to eliminate repeated items in the Array. This is a good thing.

 

Ii. readonly and disabled attributes of Textbox

These two attributes are frequently used in projects. The differences are as follows:

1. readonly attribute

Code: <input type = "text" value = "fisker" onclick = "alert (this. value);" readonly/>

The input box is read-only and cannot be edited. values can be obtained in the same form.

3, disabled

Code: <input type = "text" value = "fisker" onclick = "alert (this. value);" disabled/>

The input box is gray and cannot be edited to use JS to change or obtain its value. However, this value is not submitted at the time of submission.

 

3. A problem in cross-page Transmission

Cross-page transfer refers to submitting a form (for example, Page1.aspx) and transferring the value of this form and all controls to another page (Page2.aspx ). To achieve this function, add the PostbackUrl attribute on the control Button.

However, when you click the Button, you must first execute a piece of javascript code and then implement cross-page transfer. You cannot use the Button to complete this function. At this time, you need another control LinkButton, then, disguise the LinkButton as a Button. Haha. The following code:

 

Code
<Asp: LinkButton ID = "LinkButtonSentEmail" runat = "server" OnClientClick = "getEmailString (); return true ;"
PostBackUrl = "~ /EmailEdit. aspx "Width =" 70px "> send email </asp: LinkButton>

 

 

The executed javaxcript is:

 

Code
<Script type = "text/javascript" language = "javascript">
Function getEmailString ()
{
Var gHiddenFieldEmailAddress;
GHiddenFieldEmailAddress = $ get ("HiddenFieldEmailAddress ");
GHiddenFieldEmailAddress. value = "";

Var elements = document. getElementsByTagName ("input ");
For (var I = 0; I <elements. length; I ++ ){
Var e = elements [I];
If (e. type = "checkbox" & e. checked & e. id! = "CheckAll ")
If (e. value. trim ()! = "")
GHiddenFieldEmailAddress. value + = e. value + ",";
}
}
</Script>

 

However, the following CSS code can solve the problem by disguising the LinkButton as a Button:

 

Code
<Style type = "text/css">
# LinkButtonSentEmail
{
Padding: 3px;
Border: 2px outset;
Cursor: pointer;
Background-color: # FFFFFF;
Text-align: center;
Height: 14px;
Text-decoration: none;
Color: #000000;
Clear: both;
}
</Style>

 

This looks like the cross-page transfer from the button.

The above three problems are the minor problems I encountered when I was working on the project. It feels good to write them down.

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.