Two Methods for binding The ListBox control to a database

Source: Internet
Author: User

Implementation Method 1: bind a data source

Using system. Data;

Using system. Data. sqlclient;

..................

// Establish a connection. The content of the connection string depends on the actual situation. Here only an example of connecting to the local machine is provided.

Sqlconnection con = new sqlconnection ("Server = (local); database = Department; uid = sa; Pwd = ");

Con. open ();

Sqlcommand CM = new sqlcommand ("select empid, empname from EMP", con );

Sqldatareader DR = cm. executereader ();

// Bind

This. lbxemp. datasource = Dr; // lbxemp is a ListBox object.

This. lbxemp. datatextfield = "empname ";

This. lbxemp. datavaluefield = "empid ";

This. lbxemp. databind ();

Dr. Close ();

Con. Close ();

Implementation Method 2: Add a set

Using system. Data;

Using system. Data. sqlclient;

..................

// Establish a connection. The content of the connection string depends on the actual situation. Here only an example of connecting to the local machine is provided.

Sqlconnection con = new sqlconnection ("Server = (local); database = Department; uid = sa; Pwd = ");

Con. open ();

Sqlcommand CM = new sqlcommand ("select empid, empname from EMP", con );

Sqldatareader DR = cm. executereader ();

While (dr. Read ())

{

This. lbxemp. Items. Add (New listitem (dr. getstring (1), dr. getint32 (0). tostring ()));

}

 

Dr. Close ();

Con. Close ();

 

***********************************-----------------

. Attribute list:

Select Type of entries in the selectionmode component, that is, multiple and single)
Total number of rows displayed in the rows list box
Selected check whether the entry is selected
The returned type of selecteditem is listitem.
Total number of entries in the count list box
Index value of the selected item in the selectedindex list box
Items refers to all items in the list box. each item is of the listitem type.

2. Select the selected value from the list box.

ListBox. selectedvalue

3. dynamically add items in the list box:

ListBox. Items. Add ("item to be added ");

4. Remove a specified item:

// First determine whether the items in the list box are greater than 0
If (ListBox. Items. Count> 0)
{
// Remove the selected item
ListBox. Items. Remove (ListBox. selecteditem );
}

5. Clear all items:

// First determine whether the items in the list box are greater than 0
If (ListBox. Items. Count> 0)
{
// Clear all items
ListBox. Items. Clear ();
}

6. You can select multiple items in the list box at a time:

You only need to set the attribute selectionmode = "multiple" of the list box, and press Ctrl to select multiple

7. Two lists are linked, that is, two levels of linked menus.

// Judge the selected value in the first list box
Switch (listbox1.selectvalue)
{
// If it is "A", add these in the second list box:
Case ""
Listbox2.items. Clear ();
Listbox2.items. Add ("A1 ");
Listbox2.items. Add ("A2 ");
Listbox2.items. Add ("A3 ");
// If it is "B", add these in the second list box:
Case "B"
Listbox2.items. Clear ();
Listbox2.items. Add ("B1 ");
Listbox2.items. Add ("B2 ");
Listbox2.items. Add ("B3 ");
}

8. Shift the items in the list box
That is, shift up and down
The specific idea is to create a ListBox object and put the items to be moved in this object for the moment.
If it is an upward shift, the previous value of the selected item is assigned to the selected item, and then
Attaches the value of the newly added object to the first item of the selected item.
The Code is as follows:
// Define a variable for shift
Index =-1;
// Save the text and values of the current entry to a temporary variable.
Listitem lT = new listitem (ListBox. selecteditem. Text, ListBox. selectedvalue );
// The value of the selected item is equal to the value of the previous or next item.
ListBox. items [ListBox. selectedindex]. Text = ListBox. items [ListBox. selectedindex + Index]. text;
// The value of the selected item is equal to the value of the previous or next item.
ListBox. items [ListBox. selectedindex]. value = ListBox. items [ListBox. selectedindex + Index]. value;
// Replace the previous or next value of the selected item with a temporary variable
ListBox. items [ListBox. selectedindex]. test = lt. test;
// Replace the previous or next value of the selected item with a temporary variable
ListBox. items [ListBox. selectedindex]. value = lt. value;
// Place the mouse pointer on the moving item
ListBox. items [ListBox. selectedindex]. value = lt. value;

9. Move the pointer to the specified position:

(1) Move to the first entry
// Set the index of the selected item to 0.
ListBox. selectindex = 0;
(2) Move to the end
// Set the index of the selected item to ListBox. Items. Count-1.
ListBox. selectindex = ListBox. Items. Count-1;
(3). Previous
// Subtract 1 from the selected index
ListBox. selectindex = ListBox. selectindex-1;
(4). Next
// Add 1 with the selected index
ListBox. selectindex = ListBox. selectindex + 1;

From 51cto. com blog

This. listbox1.items. insertat (3, new listitem ("inserted after row 3rd ",""));

This. listbox1.items. insertat (index, listitem)

Listbox1.items. insert (0, new listitem ("text", "value "));

 

Add the ListBox control to ASP. NET and set the attribute to multiple. You can select Multiple widgets.
Take adding multiple projects between two listboxes as an example.
The two controls are listboxleft, and listboxright defines a dynamic array for intermediate storage. The Code is as follows:

// Read the selected project on the right
Arraylist arrright = new arraylist ();
Foreach (listitem item in this. listboxright. Items) // read the selected items in ListBox by type listitem
{
If (item. Selected) // determines whether to select
{
Arrright. Add (item );
}
}

// Remove the selected project from the right and add it on the left.
Foreach (listitem item in arrright)
{
This. listboxleft. Items. Add (item );
This. listboxright. Items. Remove (item );
}
You cannot add or delete an item directly in if (item. Selected) {} because an error occurs after the project is removed.

 

Move the item of ListBox

. Aspx

--------------------------------

<Asp: ListBox id = "listbox1" style = "Z-INDEX: 105; left: 96px; position: absolute; top: 344px" runat = "server">
<Asp: listitem value = "A1"> A1 </ASP: listitem>
<Asp: listitem value = "A2"> A2 </ASP: listitem>
<Asp: listitem value = "A3"> A3 </ASP: listitem>
</ASP: ListBox>

----------------------------------

. CS

//

Private void button#click (Object sender, system. eventargs e ){
If (this. listbox1.selectedindex> = 0)
{
Int I = listbox1.selectedindex;
If (I> 0)
{
String values = This. listbox1.items [I]. text;
This. listbox1.items [I]. Text = This. listbox1.items [I-1]. text;
This. listbox1.items [I-1]. Text = values;
}
}

}

Implement double click events in ListBox in ASP. NET

In the use of The ListBox control in ASP. NET, many people find that the ListBox mouse double-click event in winform does not exist.

This will bring a lot of inconvenience to our development.

I also read a lot of methods for implementing double-click events using JS on csdn, which are incomplete. Finally, I found that the following methods are the most effective.

First, add JS scripts to the web page and store the hidden input boxes of ListBox events.
Add the ASP. Net Control to ListBox and double-click the event declaration.
<HTML>

<Head>

<Script language = "JavaScript">

Function listbox#doubleclick (){

/* We will change value of this hidden field so

That in

Page load event we can identify event.

*/

Document. Forms [0]. listbox1hidden. value = "doubleclicked ";

Document. Forms [0]. Submit ();

}

</SCRIPT>

</Head>

<Body>

<Form runat = "server">

<Div> double click on ListBox

<Br/>

<Asp: ListBox id = "listbox1"

Ondblclick = "listbox#doubleclick ()" runat = "server">

<Asp: listitem value = "1"> one </ASP: listitem>

<Asp: listitem value = "2"> two </ASP: listitem>

<Asp: listitem value = "3"> three </ASP: listitem>

<Asp: listitem value = "4"> four </ASP: listitem>

</ASP: ListBox>

<Input type = "hidden" name = "listbox1hidden"/>

</Div>

<Div> click on button

<Br/>

<Asp: button id = "button1" onclick = "button#click"

Runat = "server" text = "button"/>

</Div>

</Form>

</Body>

</Html>

 

 

Finally, you can double-click the items in ListBox to execute the following code during web form loading.

 

Void page_load (Object sender, eventargs e ){

If (request. Params ["listbox1hidden"]! = NULL

& (String) request. Params ["listbox1hidden"] = "doubleclicked "{

// This means it was double click

Response. Write ("Double Click was fired selected item is"

+ Listbox1.selecteditem. Text );

 

// You can add the code to run here

}

}

 

I hope this article will be helpful to you and will not be misled by the post on csdn.

The basic function of ListBox is to add a list item first. The client-side implementation code is added in the middle of the ListBox instantiation code, for example:
<Asp: listitem value = "value" selected = true> text </ASP: listitem>

If it is implemented on the server side, to avoid adding list items during each load, the above Code is included in the following code:
If (! Ispostback)
{
}

Two ListBox (listbox1 and lixtbox2) and two command buttons must be added to the webform page. listbox1 is not empty. To add a list item from listbox1 to listbox2, you must call the add method in the button1 click event:
Listbox2.items. Add (listbox1.selectedvalue );

To delete a list item from listbox2, you must call the Remove Method in the button2 click event:
Listbox2.items. Remove (listbox2.selectedvalue );

After the list item is added from listbox1 to listbox2, the list item is deleted from listbox1:
Int I = 0;
While (I <listbox1.items. Count)
{
If (listbox1.items [I]. Selected = true)
{
Listbox2.items. Add (listbox1.items [I]);
Listbox1.items. Remove (listbox1.items [I]);
}
Else
I + = 1;
}

In this way, only one item can be added. To add multiple items, first set the value of the selectionmode attribute of listbox1 to multiple. listbox1 allows multiple items to be selected.

In button1, click the event to add
Foreach (listitem myitem in listbox1.items)
If (myitem. Selected = true)
Listbox2.items. Add (myitem );

To clear all options in listbox2 at a time, call the clear method in the button2 click event,
Listbox2.items. Clear ();

If the list item has been added and cannot be added again, the code in the button1 click event is included in:
If (listbox2.items. findbyvalue (listbox1.selectedvalue) = NULL)
{
}

When ListBox is bound to a database, its datasource and datatextfield attributes are specified,
Listbox2.datasource = data source;
Listbox2.datatextfield = "field name ";
Listbox2.databind ();

 

In the background
Autopostback = true
Then add an event (selectedindexchanged) to the first ListBox)
Then a traversal is available.

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.