| Ajax programming memo-ComboBox refreshing |
|
The programming logic is as follows:
1: The background obtains the corresponding lower-level department based on the ID of the higher-level department sent from the front-end, and the composition format is
Department code 1-department name 1 | Department Code 2-department name 2 | department code 3-department name 3 .....
The front-end client splits the string and re-organizes the ComboBox of the lower-level department with JavaScript.
2: Use the request ['comboboxid'] method to retrieve the ComboBox value in the background, without the need for ComboBox. Value
[Article 2nd applies to Asp.net]
JavaScript code:
// After the higher-level department is modified, the lower-level department must follow the change
Function departmentchange ()
{
VaR objdept = Document. All ['cmbdemotid'];
VaR objsubdept = Document. All ['cmbsubdemotid'];
If (objdept & objsubdept)
{
// Ajax call
VaR response = register. refilldept (objdept. value ,'-');
If (response. value! = "")
{
VaR length = 1;
While (length! = 0)
{
VaR length = objsubdept. Options. length;
For (VAR I = 0; I <length; I ++)
Objsubdept. Options. Remove (I );
Length = length/2;
}
VaR stritem = response. value. Split ('| ');
If (stritem. length> 0)
{
For (VAR n = 0; n <stritem. length; n ++)
{
VaR option = Document. createelement ("option ");
Option. value = stritem [N];
Option. Text = stritem [N];
// Alert (option. value );
Objsubdept. Options. Add (option );
}
}
}
}
} |
Asp.net background server code:
/// <Summary>
/// Obtain the information of the subordinate department after the Department changes
/// </Summary>
/// <Param name = "strdept"> department code-department name </param>
/// <Param name = "strseparetor"> shard symbol </param>
/// <Returns> subordinate department. Format: department 1 | Department 2 | department 3 </returns>
[Ajax. ajaxmethod ()]
Public String refilldept (string strdept, string strseparetor)
{
String strdeptstring = "";
Try
{
String [] stritem = strdept. Split (strseparetor. tochararray ());
If (stritem. length> 1)
{
Olexamdepartment objolexamdepartment = new olexamdepartment ();
Dataset dstdept = objolexamdepartment. getdepartinfo (stritem [0]);
If (dstdept! = NULL)
{
If (dstdept. Tables [0]. Rows. Count> 0)
{
// Returns a string.
For (INT I = 0; I <dstdept. Tables [0]. Rows. Count; I ++)
{
Strdeptstring = strdeptstring + dstdept. tables [0]. rows [I] ["deptcode"]. tostring () + "-" + dstdept. tables [0]. rows [I] ["deptname"]. tostring () + "| ";
}
}
}
If (strdeptstring. length> 0)
{
Strdeptstring = stritem [0] + "-Headquarters |" + strdeptstring. substring (0, strdeptstring. Length-1 );
}
Else
{
Strdeptstring = stritem [0] + "-Headquarters ";
}
}
Return strdeptstring;
}
Catch (exception ex)
{
Return "";
}
}