One, increase the Enhancedlistbox client function
In order to use client code to implement the reordering of items in Enhancedlistbox, you must use JavaScript scripts and attach them to the Enhancedlistbox two buttons. For this reason, I suggest you use the "think back" approach. Just like writing an old-fashioned ASP's previous Web page, first write some JavaScript that generates HTML files. The best way to do this is to run the control, then observe its source code and copy its HTML to an editor, and then add JavaScript. Listing 2 (see download source code) shows the original form of JavaScript you need to add to your control. Then, with the help of Stringbuilder/stringwriter technology (Reference source list 3), the control builds this part of the code. The JavaScript code consists of a two-part function: To receive an HTML control (in this case, a control), to use the Select Index, and to move it up or down in the list (basically the same as I did with server code at the beginning of this article). Now, you have to understand where you add the JavaScript code to the Web control. In order to implement multiple Enhancedlistbox controls on a Web form, the JavaScript code is not duplicated and you need to make the Output it using the RegisterClientScriptBlock method of the Page.clientscript object.
To make this method work, you must call it in the overloaded OnInit event (see listing 4 in source code).
Finally, to make the button work correctly, you need to attach the added client method to it. In the code in Listing 1, you'll see a reference to a method renderbuttons. Although I did not list the code here (refer to the source in this article), it is able to use the technology generation button I described in my previous article. At that time, before the actual HTML tag was generated, the label properties were stored in stacks using the AddAttribute method. Here, you use the same technique to attach the client method to your button.
string s_MoveUp = "MoveItemUp(document.all." +this.ClientID + ");
output.AddAttribute(HtmlTextWriterAttribute.OnClick, s_MoveUp);
Remember, Moveitemup is one of the JavaScript functions you've written successfully. The code will store these JavaScript commands on the stack before generating the buttons for sorting. For down buttons, you use the same technique. Note that I use ClientID to represent the ID of the generated control, but when the control is inside a composite control, this property should consider using the parent control's name.
You can now successfully apply the control to a Web form. You can add items to it in the same way that you manipulate a standard listbox control.
In fact, this is a drop-position placeholder (or asp.net listbox control) entirely. When you use the Reorder button, you will see that the items in the list change the order accordingly. Now let's make a note of this problem first. If you drag a button onto a Web form (you don't need to add code for it) and perform a postback, what do you think will happen? Exactly as I have described before; any reordering changes you make with the Reorder button will revert to the state that the control looks like before the most recent postback. So let's revise the question.
First, I'll add some JavaScript (source list 5). Note that this part of the code is added to the overloaded OnInit method and built using the stringbuilder/stringwriter technique, and the name of the JavaScript method is builditemlist. This function is responsible for building a string description of the full contents of the list box and placing the string in the Value property of an HTML element to be passed to the function. You can think of this as a serialization of the contents of a list. The serialized output style will vary according to your own design. Calling this JavaScript function requires attaching to other code on the button.
string s_MoveUp = "MoveItemUp(document.all." + this.ClientID + "); ";
string s_BuildItemList ="BuildItemList(document.all." + this.ClientID +
",document.all.__" + this.ClientID + "); ";
output.AddAttribute(HtmlTextWriterAttribute.Onclick,MoveUp + " " + BuildItemList);
Now, let's analyze the two parameters you sent to the Builditemlist function. The first parameter corresponds to the ID of the generated control (the label). The second parameter is another ID, consistent with the previous one, but preceded by a "__". This is one that you still need to add to your Web control's hidden text box, which will act as a placeholder for the list of "serialized" items. I want to register this hidden text field in the OnPreRender event.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if(Page != null)
{
Page.ClientScript.RegisterHiddenField("__" + this.ID, "");
}
}
Notice that I have used the ID of our control to identify the hidden text field.
So far, you've got a fully functional Web control, where client JavaScript is bound to two buttons. The JavaScript succeeds in reordering the items in the listbox and serializing their contents into a string; The string is then stored in a hidden text field. All of these occur on the client. If a postback occurs, no reordering occurs because the item server property of the control does not receive any changes you made to it when it is reordered, but fortunately, this change occurred in a serialized snapshot of the form in the hidden text field. Now you have the content that you can use with the Item property. So, next, how do you do that?