How to make the ASPX page use the extension method of the business logic layer

Source: Internet
Author: User

In the vs environment, we occasionally or even frequently write extension methods in the application so that we can "click" until our goal, which saves time and effort, although there is a small amount of resource consumption, this is very useful for me, a lazy person !!!

 

For example, I often extend the string method, for example, directly intercepting the string method:

Public static string getsubstr (this string orgstr, int length ){
Return orgstr. length> length? Orgstr. substring (0, length): orgstr;
}

Or determine whether it is a numeric method.

Public static bool isnumeric (this string orgstr ){
If (orgstr. isnullorempty ()){
Return false;
}
Foreach (char s in orgstr ){
If (S <48 | S> 57 ){
Return false;
}
}
Return true;
}

Alternatively, you can use the following method to determine whether the string is null:

Public static bool isnullorempty (this string orgstr ){
Return string. isnullorempty (orgstr );
}

...... And so on.

However, this is not always a problem. I am in trouble to use these methods on the ASPX page. for example, when you bind a data list, we usually want to display it in a row for the sake of appearance. If there are too many words, we can write the following statement in repeater:

Traditional (with a limit of 30 characters ):

<% # Eval ("subject"). tostring (). length> 30? (String) eval ("subject"). substring (0, 30): eval ("subject") %>

After using the extension method:

<% # Eval ("subject"). tostring (). getsubstr (30) %>

The number of words is simplified. If there are n lists on the homepage of an enterprise site, you can see that many words are saved!

However, the problem is that the extension method is not defined. In theory, the ASPX page inherits the Aspx. CS class, so Aspx. the CS class references XXXX. bll namespace, so aspx should also be able to request, but in fact the request is not available, so we have to add the following code on the second line at the top of the page: <% @ import namespace = "XXXXX. bll "%>

Debugging and running, successful!

 

Related Article

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.