ตัวอย่างการทำ combobox ให้ดึงข้อมูลจาก datasource และ add new item ลงไปด้วยในส่วน code behind
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <%@ Register Assembly="DevExpress.Web.ASPxEditors.v12.1, Version=12.1.6.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ASPxComboBox: Add an item to the ItemCollection after data-bound and perform custom client-side validation</title> </head> <body> <form id="form1" runat="server"> <div> <table class="style1"> <tr> <td> <strong>Products:</strong></td> </tr> <tr> <td> <dx:ASPxComboBox ID="cmbox" runat="server" ClientInstanceName="cmbox" TextField="ProductName" ValueField="ProductID" DataSourceID="AccessDataSource1" ondatabound="cmbox_DataBound"> <ClientSideEvents Validation="function(s, e) { if (s.GetSelectedIndex()==0) { e.isValid = false; e.errorText = "You should Select One of the Products"; } }" /> <ValidationSettings ValidateOnLeave="False"> </ValidationSettings> </dx:ASPxComboBox> </td> </tr> <tr> <td> <dx:ASPxButton ID="btn" runat="server" AutoPostBack="False" ClientInstanceName="btn" Text="Validate"> <ClientSideEvents Click="function(s, e) { cmbox.Validate(); }" /> </dx:ASPxButton> </td> </tr> </table> </div> <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/Northwind.mdb" SelectCommand="SELECT [ProductID], [ProductName] FROM [Products]"></asp:AccessDataSource> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using DevExpress.Web.ASPxEditors; public partial class _Default : System.Web.UI.Page { protected void cmbox_DataBound(object sender, EventArgs e) { ListEditItem defaultItem = new ListEditItem("------Select One------------", -1); cmbox.Items.Insert(0, defaultItem); cmbox.SelectedIndex = 0; } }
สำหรับตัวอย่าง Add Month , Year ใน Combobox
int j = 0; for (int i = 2556; i <= Convert.ToInt16(DateTime.Now.ToString("yyyy", CultureInfo.GetCultureInfo("th-TH"))); i++) { ListEditItem defaultItem = new ListEditItem(i.ToString(), i); cbbYear.Items.Insert(j, defaultItem); cbbYear.SelectedIndex = j; j++; } // Get the en-US culture. CultureInfo ci = new CultureInfo("th-TH"); // Get the DateTimeFormatInfo for the en-US culture. DateTimeFormatInfo dtfi = ci.DateTimeFormat; int jMonth = 0; foreach (string name in dtfi.MonthGenitiveNames) { ListEditItem defaultItem = new ListEditItem(name, (jMonth+1)); cbbMonth.Items.Insert(jMonth, defaultItem); cbbMonth.SelectedIndex = 0; jMonth++; }