WEBSWAPP Silverlight ASP.NET 3.5 ASP.NET 2.0 ASP.NET 1.0
Cascading Lists
ITemplate
DDL in GridView
G/V MultiSelect
Nested GridViews
RegExp
DataGrid Reorder
Events fire twice
Hyperlink Field
TextBox
UpdateParameters
MultiView
TreeView
C#-FormView VB-FormView C#-GridView VB-GridView C#-DetailsView VB-DataList
    
<%@ Page Language="C#" AutoEventWireup="false" %> <%@ Import Namespace="System.Data" %> <script language="C#" runat="server"> protected void ddlProvince_DataBound(object sender, EventArgs e) { DropDownList ddl = (DropDownList)sender; //add an empty item on top of the list AddEmptyItem(ddl); DetailsView det = (DetailsView)ddl.NamingContainer; if (det.DataItem != null) { //Let's pull the province value from the databound item. The data //in my application is supplied by BLL as a dataview. Therefore each //item bound to the DetailsView is of type DataRowView. So let's cast //that DataItem to the appropriate type to be able to use it string strProvinceID = ((DataRowView)det.DataItem)["ProvinceID"].ToString(); ddl.ClearSelection(); //be careful of the possibility that the value saved on the //database does not exist in the valid selections that are displayed //on the list System.Web.UI.WebControls.ListItem li = ddl.Items.FindByValue(strProvinceID); if (li != null) li.Selected = true; } //since the city selection is dependent on the province, we //have to databind the city list after we changed the selection for the province ddl = (DropDownList)det.FindControl("ddlCity"); if (ddl != null) ddl.DataBind(); Page.Error += new EventHandler(Page_Error); } void Page_Error(object sender, EventArgs e) { Response.Write("Error" + e.ToString()); } protected void ddlCity_DataBound(object sender, EventArgs e) { DropDownList ddl = (DropDownList)sender; DetailsView det = (DetailsView)ddl.NamingContainer; if (det.DataItem != null) { string strCityID = ((DataRowView)det.DataItem)["CityID"].ToString (); ddl.ClearSelection(); System.Web.UI.WebControls.ListItem lm = ddl.Items.FindByValue(strCityID); if (lm != null) lm.Selected = true; } //add an empty item on top of the list AddEmptyItem(ddl); } void DetailsView1_ItemUpdating(object sender, DetailsViewUpdateEventArgs e) { if (Page.IsValid) { string strCityID = ((DropDownList)((DetailsView)sender).FindControl("ddlCity")).SelectedValue; e.NewValues["CityID"] = strCityID; } } void DetailsView1_ItemCommand(object sender, DetailsViewCommandEventArgs e) { if (e.CommandName == "Cancel") DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); } void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) { if (Page.IsValid) { //we will apply the same logic as above while inserting a new record string strCityID = ((DropDownList)((DetailsView)sender).FindControl("ddlCity")).SelectedValue; e.Values["CityID"] = strCityID; } } void odsAddresses_Inserted(object sender, ObjectDataSourceStatusEventArgs e) { //handled any exceptions that occured while inserting if (e.Exception != null) { lblStatus.Text = e.Exception.Message; e.ExceptionHandled = true; //prevent the exception from bubbling up } //set the current page to the newly inserted record after inserting //by using the returnValue (which in my Business Logic Layer is returning //the position of the newly inserted record which is the page number on the DetailsView) DetailsView1.PageIndex = Convert.ToInt16(e.ReturnValue.ToString()); } void AddEmptyItem(DropDownList ddl) { System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem("Make a Selection", ""); ddl.Items.Insert(0, li); } void DetailsView1_PageIndexChanging(object sender, DetailsViewPageEventArgs e) { ClientScriptManager cs = Page.ClientScript; Type cstype = this.GetType(); if (DetailsView1.CurrentMode == DetailsViewMode.Edit) { // Check to see if the startup script is already registered. if (!cs.IsStartupScriptRegistered(cstype, "AlertMessage")) { String cstext = "alert('You cannot navigate to another page while in Edit mode. Please complete the Edit operation first.');"; cs.RegisterStartupScript(cstype, "AlertMessage", cstext, true); } lblStatus.Text = "Your last operation (navigating to another page) has failed because you are in Edit mode."; e.Cancel = true; } } </script> <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="MainContent"> <table> <tr> <td> <h3> Demo for 2-way databinding cascading lists within a DetailsView</h3> <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="odsAddresses" AllowPaging="True" OnItemUpdating="DetailsView1_ItemUpdating" OnItemInserting="DetailsView1_ItemInserting" DataKeyNames="PK_ID" CssClass="DetailsView1" OnItemCommand="DetailsView1_ItemCommand" OnPageIndexChanging="DetailsView1_PageIndexChanging" AutoGenerateRows="false"> <EditRowStyle CssClass="DetailsViewEdit" /> <InsertRowStyle CssClass ="DetailsViewInsert" /> <HeaderStyle CssClass ="DetailsView1Header" /> <FooterStyle CssClass ="DetailsViewFooter" /> <Fields> <asp:CommandField ButtonType="link" ShowCancelButton="true" ShowEditButton="true" ShowInsertButton="true" ControlStyle-CssClass ="DetailsViewFooter" /> <asp:TemplateField> <ItemTemplate> Readonly Mode </ItemTemplate> <EditItemTemplate> Edit Mode </EditItemTemplate> <InsertItemTemplate> Insert Mode </InsertItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Eval("PK_ID") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Eval("PK_ID") %>'></asp:Label> </EditItemTemplate> <InsertItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%# Eval("PK_ID") %>'></asp:Label> </InsertItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Company Name"> <ItemTemplate> <asp:Label ID="lblCompany" runat="server" Text='<%# Eval("Company") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox MaxLength="50" Width="300" ID="txtCompany" runat="server" Text='<%# Bind("Company") %>'></asp:TextBox> <%-- Let's add a requiredfieldvalidator to ensure that edited values are not empty but notice to set the CausesValidation=false on the Cancel CommandButton --%> <asp:RequiredFieldValidator ID="valCompany" runat="Server" ControlToValidate="txtCompany" ForeColor="white" ErrorMessage="Cannot leave the company name empty" Text="*" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox MaxLength="50" Width="300" ID="txtCompany" runat="server" Text='<%# Bind("Company") %>'></asp:TextBox> <%-- Let's add requiredfieldvalidators to ensure that newly inserted records receive values but notice to set the CausesValidation=false on the Cancel CommandButton --%> <asp:RequiredFieldValidator ID="valCompany" runat="Server" ControlToValidate="txtCompany" ForeColor="white" ErrorMessage="Cannot leave the company selection empty" Text="*" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Street Address"> <ItemTemplate> <asp:Label ID="lblStreet" runat="server" Text='<%# Eval("Street") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtStreet" MaxLength="50" Width="300" runat="server" Text='<%# Bind("Street") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="valStreet" runat="Server" ControlToValidate="txtStreet" ForeColor="white" ErrorMessage="Cannot leave the street address empty" Text="*" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator> </EditItemTemplate> <InsertItemTemplate> <asp:TextBox ID="txtStreet" MaxLength="50" Width="300" runat="server" Text='<%# Bind("Street") %>'></asp:TextBox> <asp:RequiredFieldValidator ID="valStreet" runat="Server" ControlToValidate="txtStreet" ForeColor="white" ErrorMessage="Cannot leave the street selection empty" Text="*" SetFocusOnError="true" Display="Dynamic"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Country"> <ItemTemplate> <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("Country") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:ObjectDataSource ID="odsCountries" runat="server" SelectMethod="CountryList" TypeName="WEBSWAPP_BLL.Demos"></asp:ObjectDataSource> <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountries" AutoPostBack="True" SelectedValue='<%# Eval("CountryID") %>' AppendDataBoundItems="true" DataTextField="Description" DataValueField="PK_ID"> <asp:ListItem Value="">Select a Country</asp:ListItem> </asp:DropDownList> </EditItemTemplate> <InsertItemTemplate> <asp:ObjectDataSource ID="odsCountries" runat="server" SelectMethod="CountryList" TypeName="WEBSWAPP_BLL.Demos"></asp:ObjectDataSource> <%-- when we insert a new record there is no country value to be bound upon displaying the empty record. Therefore let's add a default System.Web.UI.WebControls.ListItem with a null value so that we can validate it using a RequiredFieldValidator to ensure that the user will enter a value in it. I will use here the AppendDataBoundItems property of the dropdownlist so that any databound items to be created will not replace the default System.Web.UI.WebControls.ListItem that I just created. --%> <asp:DropDownList ID="ddlCountry" runat="server" DataSourceID="odsCountries" AutoPostBack="True" SelectedValue='<%# Eval("CountryID") %>' AppendDataBoundItems="true" DataTextField="Description" DataValueField="PK_ID"> <asp:ListItem Value="">Select a Country</asp:ListItem> </asp:DropDownList> <asp:RequiredFieldValidator ForeColor="white" ID="valddlCountry" runat="server" ErrorMessage="Cannot leave the country blank" Text="*" Display="Dynamic" ControlToValidate="ddlCountry"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Province/State"> <ItemTemplate> <asp:Label ID="lblProvince" runat="server" Text='<%# Eval("Province") %>' DataTextField="Description" DataValueField="PK_ID"></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:ObjectDataSource ID="odsProvinces" runat="server" SelectMethod="ProvincesList" TypeName="WEBSWAPP_BLL.Demos"> <SelectParameters> <asp:ControlParameter ControlID="ddlCountry" Name="CountryID" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:ObjectDataSource> <asp:DropDownList ID="ddlProvince" runat="server" DataSourceID="odsProvinces" AutoPostBack="true" OnDataBound="ddlProvince_DataBound" DataTextField="Description" DataValueField="PK_ID"> </asp:DropDownList> <asp:RequiredFieldValidator ForeColor="white" ID="valddlProvince" runat="server" ErrorMessage="Cannot leave the province selection blank" Text="*" Display="Dynamic" ControlToValidate="ddlProvince"></asp:RequiredFieldValidator> </EditItemTemplate> <InsertItemTemplate> <asp:ObjectDataSource ID="odsProvinces" runat="server" SelectMethod="ProvincesList" TypeName="WEBSWAPP_BLL.Demos" > <SelectParameters> <asp:ControlParameter ControlID="ddlCountry" Name="CountryID" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:ObjectDataSource> <asp:DropDownList ID="ddlProvince" runat="server" DataSourceID="odsProvinces" AutoPostBack="true" OnDataBound="ddlProvince_DataBound" DataTextField="Description" DataValueField="PK_ID"> <asp:ListItem Value="">Select a Province</asp:ListItem> </asp:DropDownList> <asp:RequiredFieldValidator ForeColor="white" ID="valddlProvince" runat="server" ErrorMessage="Cannot leave the province selection blank" Text="*" Display="Dynamic" ControlToValidate="ddlProvince"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <ItemTemplate> <asp:Label ID="lblCity" runat="server" Text='<%# Eval("City") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:ObjectDataSource ID="odsCities" runat="server" SelectMethod="CitiesList" TypeName="WEBSWAPP_BLL.Demos"> <SelectParameters> <asp:ControlParameter ControlID="ddlProvince" Name="ProvinceID" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:ObjectDataSource> <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="odsCities" DataTextField="Description" DataValueField="PK_ID" OnDataBound="ddlCity_DataBound"> </asp:DropDownList> <asp:RequiredFieldValidator ForeColor="white" ID="valddlCity" runat="server" ErrorMessage="Cannot leave the city selection blank" Text="*" Display="Dynamic" ControlToValidate="ddlCity"></asp:RequiredFieldValidator> </EditItemTemplate> <InsertItemTemplate> <asp:ObjectDataSource ID="odsCities" runat="server" SelectMethod="CitiesList" TypeName="WEBSWAPP_BLL.Demos"> <SelectParameters> <asp:ControlParameter ControlID="ddlProvince" Name="ProvinceID" PropertyName="SelectedValue" Type="String" /> </SelectParameters> </asp:ObjectDataSource> <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="odsCities" DataTextField="Description" DataValueField="PK_ID" OnDataBound="ddlCity_DataBound"> <asp:ListItem Value="">Select a City</asp:ListItem> </asp:DropDownList> <asp:RequiredFieldValidator ForeColor="white" ID="valddlCity" runat="server" ErrorMessage="Cannot leave the city selection blank" Text="*" Display="Dynamic" ControlToValidate="ddlCity"></asp:RequiredFieldValidator> </InsertItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> <%-- The object Datasource definition for the FormView is declared outside of the FormView --------------------------------------------------------------------------------------------------%> <asp:ObjectDataSource ID="odsAddresses" runat="server" SelectMethod="Contacts" TypeName="WEBSWAPP_BLL.Demos" UpdateMethod="UpdateContact" InsertMethod="InsertContact" OnInserted="odsAddresses_Inserted" > <UpdateParameters> <asp:Parameter Name="PK_ID" Type="Int32" /> <asp:Parameter Name="Company" Type="String" /> <asp:Parameter Name="Street" Type="String" /> <asp:Parameter Name="CityID" Type="Int32" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="Company" Type="String" /> <asp:Parameter Name="Street" Type="String" /> <asp:Parameter Name="CityID" Type="Int32" /> </InsertParameters> </asp:ObjectDataSource> <%-- let's add a summary validation control to display a pop up message --%> <asp:ValidationSummary ID="valSumm" runat="server" ShowMessageBox="true" ShowSummary="false" /> <%-- Let's add a label to display any status--%> <asp:Label ID="lblStatus" runat="server" EnableViewState="false" CssClass="ErrMessage"></asp:Label> </td> </tr> <tr> <td> </td> </tr> </table> </asp:Content>