Data Binding of dropdownlist

Source: Internet
Author: User

To use dropdownlist in the DataGrid, you must use

1. Datagrid_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs e) event of the DataGrid

Itemdataboundz occurs when the item is bound to the DataGrid Control

2. Listtypeitem Enumeration

Specifies the type of items in the list control.

member name description
alternatingitem alternating (an even index starting from scratch) items in cells. It is bound to Data.
edititem items in edit mode in the list control. It is bound to Data.
footer footer of the list control. It is not bound to Data.
header the header of the list control. It is not bound to Data.
item items in the list control. It is bound to Data.
pager page navigation: displays the controls that are located on different pages associated with the DataGrid Control. It is not bound to Data.
selecteditem selected items in the list control. It is bound to Data.
separator delimiter between items in the list control. It is not bound to Data.

Example:

ASPX page --- only returns the DataGrid part of the page

<Asp: DataGrid id = "mydatagrid" runat = "server">
<Columns>
<Asp: templatecolumn headertext = "transaction">
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "transaction") %>
</Itemtemplate>
<Edititemtemplate>
<Asp: dropdownlist id = dropdown runat = "server"> </ASP: dropdownlist> // The tag of dropdownlist determines the selection of the listtypeitem to be bound to Data.
</Edititemtemplate>
</ASP: templatecolumn>
</Columns> <br> </ASP: DataGrid> </P>

. CS File

Private void mydatagrid_itemdatabound (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)

{

If (E. Item. itemtype = listitemtype. edititem )? // If"The instance where the object reference is not set to the objectThe listitemtype is incorrect.

{
Sqlconnection conn = new sqlconnection ("database = test; uid = sa; PWD; server = (local)");

Sqldataadapter da = new sqldataadapter ("select * from users", Conn );

Dataset DS = new dataset ();

Da. Fill (DS );

Dropdownlist drop = (dropdownlist) E. Item. findcontrol ("dropdown");

Drop. datatextfield = "ID ";

Drop. datavaluefield = "cusname ";

Drop. datasource = Ds. Tables [0]. defaultview;

Drop. databind ();

}

}

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.