Using DropDownList binding Data method in ASP.net listview

Source: Internet
Author: User

Asp. NET provides most data-bound controls that use additional markup to automatically encapsulate display data, for example, the GridView control displays its data in an HTML table (<table>), one row (<tr>) for each record, Each field is displayed as a cell (<td>), although you can customize the appearance of the GridView using the TemplateField component, but the output of the GridView is still limited to a table component. But sometimes you want to completely control the appearance of HTML tags produced by data-bound controls, which is the advantage of the ListView control, which is not using additional markup to encapsulate its output, but rather the exact HTML description you specify, You can specify precise markup by using a built-in template for the ListView control, and Table 1 lists the templates that are supported by the ListView control.

The code in the ASPX page:

<EditItemTemplate>
<asp Tutorial: DropDownList id= "DropDownList1" runat= "Server" >
<asp:listitem value= "Men" > Men </asp:ListItem>
<asp:listitem value= "female" > Female </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

Adding events: ItemUpdating and ItemCreated events

protected void Listview1_itemupdating (object sender, Listviewupdateeventargs e)
{
Bind the DropDownList before updating
DropDownList Ddgender = (DropDownList) Listview1.items[e.itemindex]. FindControl ("DropDownList1");
e.newvalues["Gender"] = Ddgender.selectedvalue;
}

protected void listview1_itemcreated (object sender, ListViewItemEventArgs e)
{
if (E.item.itemtype = = Listviewitemtype.dataitem)
{
DropDownList Ddlgender = (DropDownList) e.item.findcontrol ("DropDownList1");
if (Ddlgender!= null)
{
Listviewdataitem Lvdataitem = (listviewdataitem) e.item;
DataRowView Rowview = (DataRowView) Lvdataitem.dataitem;
if (Rowview!= null)
{
var userrow = (datasetusers.usersrow) Rowview.row;
Ddlgender.selectedvalue = Userrow.gender;
}
}
}
}

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.