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.