I remember that when I used ASP, I often encountered multi-level linkage in the drop-down box. For example, when registering, I needed to select a province and a city. This requires second-level linkage, at that time, a common practice was to use JavaScript scripts to read data from the database and put it into the Javascript array. When the onchange event in the drop-down box is triggered, you can directly read the desired data from the Javascript array. Later in Asp.net
This method is also common during development in 1.0. However, if you want to use multiple levels, there is a major drawback: it needs to read all the data into the Javascript array at a time, if the data is very large, this part of the overhead is very large. Sometimes, the data is not put into the data, but directly written in Javascript, which is not easy to maintain, and
Code It is too long. It is a headache if you want to develop an infinitely cascade function. Since ASP, Net
After the advent of 2.0, I began to learn new technologies and found that ASP and net2.0 have not only undergone major changes in controls and functions, but also become more powerful in calling Javascript client scripts, in particular, Ajax is available, that is, the client callback function (of course, this is only a small Ajax function. If you want to implement more powerful functions, you can refer to Atlas ), with this feature, you can easily discover the unlimitedly linked drop-down box effects.
1. First, create the corresponding data table in the database as follows:
2. Create a function to read data tables. I will not write it here. I think everyone will have different ways to implement this function.
3. Next, we can set the page. First, we will create a second-level linkage effect, and place two drop-down box controls on the page. The above one is testdropdownlist, And the next one is Item1, add an objectdatasource control to read all records whose parentid is 0 and bind them to the testdropdownlist drop-down box, for example:
To use client callback, you must implement the system. Web. UI. icallbackeventhandler interface.
1 public partial class test: system. Web. UI. Page,
System. Web. UI. icallbackeventhandler
2 {
There are two methods for this interface:
A), void system. Web. UI. icallbackeventhandler. raisecallbackevent (string
Eventarguent)
This method receives the parameter enentarguent passed by the client. This parameter seems to be only string type and has no return value. We can execute the corresponding function based on the passed parameter, here we cannot re-bind the page, but can interact with the database, such as reading, deleting, and inserting operations. In this example, here, the value of parentid is transmitted from the client, and all records whose parentid is the corresponding value are read from the database. The specific code is as follows:
1 void
System. Web. UI. icallbackeventhandler. raisecallbackevent (string
Eventarguent)
2 {
3 dataset DS = new
Getdata (). testbyparentid (convert. toint32 (eventarguent ));
4
Foreach (datarow row in DS. Tables [0]. Rows)
5 {
6 if
(Result. Equals (string. Empty ))
7 {
8 result =
Row ["pkid"] + "@" + row ["name"]. tostring ();
9}
10
Else
11 {
12 result = Result + "," +
Row ["pkid"] + "@" + row ["name"]. tostring ();
13}
14
}
15}
Here, we use the testbyparentid (convert. toint32 (enentarguent) function to read the corresponding data and process the obtained data. Why do we need to do this? Please continue. The result is a string variable.
B), string system. Web. UI. icallbackeventhandler. getcallbackresult ()
This is the second method. This method has no parameters and only returns the string type. This method is used for data callback. The above method receives the parameters passed by the client. After processing, it also transmits the processed data back to the client for execution,. We can also see that this method can only return the string type, but the drop-down box must have two values: datavalue and datatext. Therefore, in the above method, combine these two values through the @ symbol to facilitate the combination on the clientProgram. The function of this method is very simple, that is, to return the result, as follows:
1 string
System. Web. UI. icallbackeventhandler. getcallbackresult ()
2 {
3
Return result;
4}
Now we have implemented all the methods under the System. Web. UI. icallbackeventhandler interface. Next we can write the client program and use JavaScript to implement it. There are also two methods here. The first method is used to accept the onchange event trigger response of the drop-down box control, and the other method is used to receive the string
System. Web. UI. icallbackeventhandler. getcallbackresult () method to pass the result value, and perform corresponding processing to bind the data to the second drop-down box.
Secondary linkage. The main code is as follows:
A) the first method
1 function callserver (input, context)
2 {
3
Context. Length = 0;
4 context. Options [context. Length] = new
Option ("reading data", "0 ");
5
6 Arg = input. value;
7
<% = Callback %>
8}
There are two parameters. input is the name of the drop-down box control used to trigger onchange, and context is the name of the drop-down box to be cascaded. Before data is bound, the cascade drop-down box displays "reading data.... Arg receives the value from the input drop-down box. This value will be passed to the eventarguent parameter above. The <% = callback %> of this value will be introduced below.
B) the second method
1 function exploreserverdata (result, context)
2
{
3 context. Length = 0;
4 var arrdata =
Result. Split (",");
5 For (VAR I = 0; I <arrdata. length;
I ++)
6 {
7 var DATA =
Arrdata [I]. Split ("@");
8 context. Options [context. Length] = new
Option (data [1], data [0]);
9}
10
11}
Here there are two parameters, the result is to receive the result value mentioned above, and the context is the same as the context of the first method. Here, we use the JS split method to process the data and then bind it to the context control. Through this, we can see the corresponding value in the second drop-down box.
This is basically done, but there is still a last step. This is also the most important step. What we are doing now is that the client is the client, and the server method is the server method, so how can we associate these two items? This is also the key part for implementing the client callback. here we need to use page. clientscript. the getcallbackeventreference method. The following is an excerpt from the clientscript on msdn2. getcallbackeventreference (......) .
Public String getcallbackeventreference (Control, string
Argument, string clientcallback, string context)
Parameters:
Parameter Function
Control the server that processes the client callback. This control must be implemented
Icallbackeventhandler interface and provides the raisecallbackevent method.
Argument
Transmits a parameter from the client script to the raisecallbackevent method on the server.
Clientcallback
The name of a client event handler that receives the results returned by server events.
Context
Client script information on the client before the callback is started. The script result is returned to the client event handler.
Return the name of the client function that calls the client callback.
The following is an overloaded list of the clientscriptmanager. getcallbackeventreference method.
Name
Description
Clientscriptmanager. getcallbackeventreference (control, String, string,
String) to obtain a reference to the client function. When this function is called, a client callback for server-side events is started. The client function of this overload method contains the specified controls, parameters, client scripts, and context.
Clientscriptmanager. getcallbackeventreference (control, String, string,
String, Boolean)
Gets a reference to a client function. When this function is called, a client callback for server-side events is started. The client function of this overload method contains the specified controls, parameters, client scripts, context, and boolean values.
Clientscriptmanager. getcallbackeventreference (control, String, string,
String, String, Boolean)
Gets a reference to a client function. When this function is called, a client callback for server-side events is started. The client functions of this overload method include the specified controls, parameters, client scripts, context, error handlers, and boolean values.
Clientscriptmanager. getcallbackeventreference (string, String, string,
String, String, Boolean)
Gets a reference to a client function. When this function is called, a client callback for server-side events is started. The client function of this overload method contains the specified target, parameter, client script, context, error handler, and Boolean value.
The main implementation is as follows:
Add the following statement to page_load:
1 callback = page. clientscript. getcallbackeventreference (this,
"Arg", "receiveserverdata", "context ");
Arg is used to pass parameters from the client and finally to enentarguent. receiveserverdata is a client method used to receive data from the server callback.
Put the callback string below the first client method (see the first client method above)
After this is done, we only need to add the onchange client event in the first drop-down box, and add it in page_load.
1testdropdownlist. attributes ["onchange"] =
"Javascript: callserver (testdropdownlist, Item1 )";
After all this is done, the program should be able to work normally.
Now that the second-level interaction has been successful, how can we achieve multi-level interaction? In fact, it is not difficult to find that when I touch an onchange event in the drop-down box and read the value through callserver, then start with <% callback %>
Void system. Web. UI. icallbackeventhandler. raisecallbackevent (string
Eventarguent ).
System. web. UI. icallbackeventhandler. the getcallbackresult () method is returned, and then the receiveserverdata client binds the data to the corresponding original drop-down box. The two interactive drop-down boxes here are transmitted through parameters,
Testdropdownlist. attributes ["onchange"] =
"Javascript: callserver (testdropdownlist,
Item1) "; in this way, we can change the name of the control parameter passed here, and we can reuse these four methods, that is, we can achieve unlimited linkage. Let's add another drop-down box named item2. We just need to add the following statement to page_load:
1item1. attributes ["onchange"] = "javascript: callserver (Item1, item2 )";
Three levels of interaction can be achieved. If four or five levels are required, you only need to add the corresponding drop-down box and add the corresponding onchange event trigger to page_load.
However, this method has two major disadvantages:
1. That is, when the first option is selected, the corresponding value will appear in the second drop-down box, and the third option and the following items will not change. Only after the value of the second drop-down box is selected, the third option will change.
2. Due to the problem 1, if there is only one value in the second drop-down box, the Third and the following cannot be changed. I wonder if you have any good solutions, I believe there should be solutions.
Due to my limited level, there may be some improper points. I hope you can correct them. Thank you!