<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Page maintainscrollpositiononpostback="true" %>
<html>
<head>
<title>Using a DropDownList in an Editable DataList</title>
<link rel="stylesheet" href="/shared/netdemos.css">
<style type="text/css">
<!--
table, div {font-size: 10pt}
-->
</style>
<script language="C#" runat="server" src="fetchData_oledb.cs" />
<script language="C#" runat="server">
string planType;
void Page_Load ( Object src, EventArgs e ) {
if ( !IsPostBack ) {
string query = "SELECT DISTINCT Design FROM Plans";
lstDesigners.DataSource = fetchReader ( query, "plans" );
lstDesigners.DataBind ( );
getRecSet ( null, null );
}
}
void getRecSet ( Object src, EventArgs e ) {
// if a row is in edit mode, set edit mode off
if ( myList.EditItemIndex > -1 ) myList.EditItemIndex = -1;
// fetch and store data into Session
string query = "SELECT TOP 10 * FROM Plans WHERE Design='" +
lstDesigners.SelectedItem.Text + "' AND NOT Type='' ORDER BY Cost desc";
Session [ "myTable" ] = ( fetchData ( query, "plans" ) ).Tables [ 0 ];
bindList ( );
}
void bindList ( ) {
// bind list to current data in Session
myList.DataSource = Session [ "myTable" ];
myList.DataBind ( );
}
void myListEditHandler ( Object src, DataListCommandEventArgs e ) {
planType = ( string ) ( ( DataTable ) Session [ "myTable" ] ).Rows [ e.Item.ItemIndex ] [ "Type" ];
myList.EditItemIndex = e.Item.ItemIndex;
bindList ( );
}
void myListCancelHandler ( Object src, DataListCommandEventArgs e ) {
myList.EditItemIndex = -1;
bindList ( );
}
void myListUpdateHandler ( Object src, DataListCommandEventArgs e ) {
// load data from Session
DataTable myTable = ( DataTable ) Session [ "myTable" ];
// nothing really happens for now
// ... code to update data source here ...
// refresh data in Session
Session [ "myTable" ] = myTable;
// set edit mode off
myList.EditItemIndex = -1;
bindList ( );
}
void setSelectedType ( Object src, DataListItemEventArgs e ) {
if ( e.Item.ItemType == ListItemType.EditItem ) {
DropDownList lstTypes = ( DropDownList ) e.Item.FindControl ( "lstTypes" );
string query = "SELECT DISTINCT Type FROM Plans WHERE NOT Type=''";
lstTypes.DataSource = fetchData ( query, "plans" );
lstTypes.DataBind ( );
lstTypes.SelectedIndex = lstTypes.Items.IndexOf (
lstTypes.Items.FindByValue ( planType ) );
}
}
</script>
</head>
<body>
<!-- #include virtual="~/shared/top.inc" -->
<div class="header"><h2>Using a DropDownList in an Editable DataList</h2></div>
<!-- #include virtual="~/shared/viewsrc_top.inc" -->
<hr size=1 width=92%>
<div align="center">
<form runat="server">
Select Designer: <asp:dropdownlist id="lstDesigners" runat="server"
datatextfield="Design" onSelectedIndexChanged="getRecSet" autopostback />
<p>
<asp:datalist id="myList" runat="server"
width=94% repeatcolumns=2
cellpadding=10 cellspacing=10
onItemDataBound="setSelectedType"
onEditCommand="myListEditHandler"
onUpdateCommand="myListUpdateHandler"
onCancelCommand="myListCancelHandler">
<itemstyle backcolor="ghostwhite"
width="50%" verticalalign="top" />
<alternatingitemstyle backcolor="beige" />
<edititemstyle backcolor="palegoldenrod"
borderwidth=1 borderstyle="inset" />
<itemtemplate>
<table cellpadding=5 width=100%>
<tr valign="top">
<td>
<b>Category:</b> <asp:label id="planType" runat="server"
text='<%# Eval ( "type" ) %>' />
<br>
<b>Model:</b> <%# Eval ( "model" ) %>
<p align="center">
<asp:imagebutton commandname="edit" runat="server"
imageurl="~/shared/images/button_edit.gif" />
</td>
<td align="right">
<asp:hyperlink runat="server"
navigateurl='<%# Eval (
"PlanID", "details_plans.aspx?id={0}" ) %>'>
<img width=125 border=0 runat="server"
src='<%# Eval (
"PlanID", "~/shared/images/plans/thumbs/{0}.jpg" ) %>'
alt='<%# Eval ( "Model" ) %>' />
</asp:hyperlink>
</td>
</tr>
</table>
<div><b>Description:</b> <%# Eval ( "description" ) %><br>
<b>Avg. Cost To Build:</b> <%# Eval ( "cost", "{0:c}" ) %></div>
</itemtemplate>
<edititemtemplate>
<img align="right" width=75 runat="server"
src='<%# Eval ( "PlanID", "~/shared/images/plans/thumbs/{0}.jpg" ) %>' >
<table width=90% cellpadding=3>
<col width=15%>
<tr valign="top">
<td align="right"><b>Category:</b></td>
<td>
<asp:dropdownlist runat="server" id="lstTypes"
datatextfield="Type" />
</td></tr>
<tr valign="top">
<td align="right"><b>Model:</b></td>
<td>
<asp:textbox id="model" runat="server"
text='<%# Eval ( "model" ) %>' />
</td></tr>
<tr valign="top">
<td align="right"><b>Description:</b></td>
<td><asp:textbox id="desc" runat="server"
textmode="multiline" rows=5 columns=50 font-name="verdana"
text='<%# Eval ( "description" ) %>' />
</td></tr>
<tr valign="top">
<td align="right"><b>Cost:</b></td>
<td><asp:textbox id="cost" runat="server" size=10
text='<%# Eval ( "cost" ) %>' /></td></tr>
</table>
<p align="center">
<asp:imagebutton commandname="update" runat="server"
imageurl="~/shared/images/button_update.gif" />
<asp:imagebutton commandname="cancel" runat="server"
imageurl="~/shared/images/button_cancel.gif"
causesvalidation=false />
</edititemtemplate>
</asp:datalist>
</form>
</div>
<hr size=1 width=92%>
<!-- #include virtual="~/shared/viewsrc.inc" -->
</body>
</html>