Practical Tips series (3): Initialize the server data to the client Array

Source: Internet
Author: User

To enhance user experience and reduce server-side sending back operations, the interaction between the server and the client is becoming more and more widely used. The emergence of various Ajax products conforms to this trend.

Here, I am not talking about any profound things. I just share some practical things I encountered during development with you. I hope there will be no heavy things.

Case:

Now I want to read a data list from the database and initialize the list to an array on the client. This is a typical interaction between the client and the server. As soon as you see this, there are many ways to implement it. Here I will help you sort it out.

Solution:

1. Ajax implementation

Many people may choose this method. It is very convenient to use this method. For Ajax in Asp.net, an array can be directly returned from the server.

2. Implement loop marking on the server side on the client side

This method is similar to the following:Code:

<SCRIPT>

VaR list = [];

<% Server-side loop start %>

List. Push (<% loop Value %> );

<% End of server loop %>

</SCRIPT>

3. Use the method in page. clientscript to implement

Define the following methods on the client:

<SCRIPT>

VaR list = [];

Function addobject (object)

{

List. Push (object );

}

</SCRIPT>

On the server side:

For Loop

{

Page. clientscript. registerstartupscript (this. GetType (), "DD", "addobject (" + loop value + ");", true );

}

4. I think this method is clever and I often use it.

Client:

<SCRIPT>

VaR list = [];

Function addobject (object)

{

List. Push (object );

}

<Asp: literal id = "userobjectutil" runat = "server"> </ASP: literal>

</SCRIPT>

Note: A server control is added to the <SCRIPT> </SCRIPT> tag. <asp: literal id = "userobjectutil" runat = "server"> </ASP: literal>, microsoft's resolution is strong enough, so it can be used on the server.

Server:

Stringbuilder sb = new stringbuilder (200 );

For Loop

{

SB. appendformat ("addobject ('{0}'); \ n", cyclic value );

}

Userobjectutil. Text = sb. tostring ();

I usually use these methods, but I don't know what other good methods are available. Because such applications should be simpler and better, and there is no need to make them more complicated.

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.