How do I Insert a ListItem into a DropDownList?

3 replies. Last post: February 14, 2010 10:07 PM by Glenn Layaar
Tags :
  • (None)
  • New Discussion
  • New Question
  • New Product Feedback

How do I Insert a ListItem into a DropDownList for a WebGridColum of EditType DropDownList.

With an asp:DropDownList I can use it's OnPreRender Event to Insert "--- PLEASE SELECT ---" at the top of List retrieved from the Database as in the code below.  I have not been able to figure out how to get a reference to the generated DropDownList Server Side to do this. 

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.Data" %>


<%@ Register assembly="ISNet.WebUI.WebGrid" namespace="ISNet.WebUI.WebGrid" tagprefix="ISWebGrid" %>

<!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></title>
    
	<script runat="server">
		
		protected void _WebGrid_InitializeLayout(object sender, LayoutEventArgs e)
		{
			ISNet.WebUI.WebGrid.WebValueList Priority_WebValueList = _WebGrid.GetTableByName("").Columns.GetNamedItem("Priority_WebGridColumn").ValueList;

			SortedList Priority_SortedList = new SortedList();

			Priority_SortedList.Add("ASSESS - 1", "1");
			Priority_SortedList.Add("ASSESS - 2", "2");
			Priority_SortedList.Add("ASSESS - 3", "3");
			Priority_SortedList.Add("DISPOSE", "D");
			Priority_SortedList.Add("UNDECIDED", "");

			Priority_WebValueList.DataSource = Priority_SortedList;
			Priority_WebValueList.DataTextField = "Key";
			Priority_WebValueList.DataValueField = "Value";

			RefurbPriority_DropDownList.DataSource = Priority_SortedList;
			RefurbPriority_DropDownList.DataTextField = "Key";
			RefurbPriority_DropDownList.DataValueField = "Value";

			RefurbPriority_DropDownList.SelectedValue = "";

			RefurbPriority_DropDownList.DataBind();
		}


		protected void Company_DropDownList_PreRender(object sender, EventArgs e)
		{
			DropDownList _DropDownList = (DropDownList) sender;
			_DropDownList.Items.Insert(0, "--- PLEASE SELECT ---");
		}
				
	</script>

</head>

<body>
    <form id="form1" runat="server">
    <div>

	<table>
		<tr>
			<td>PRIORITY</td>
			<td>COMPANY</td>
		</tr>
		<tr>
			<td>
				<ASP:DROPDOWNLIST 
					ID="RefurbPriority_DropDownList" 
					runat="server"
				>
				</ASP:DROPDOWNLIST>
			</td>
			<td>
				<ASP:DROPDOWNLIST 
					ID="Company_DropDownList" 
					runat="server" 
					DataValueField="CustomerID"
					DataTextField="CompanyName"
					DataSourceID="Company_SqlDataSource"
					onPreRender="Company_DropDownList_PreRender"
				>
				</ASP:DROPDOWNLIST>
				
			</td>
		</tr>
		<tr>
			<td colspan="2">
				<ISWebGrid:WebGrid 
					ID="_WebGrid"
					runat="server"
					DataSourceID="Orders_SqlDataSource"
					Height="250px"
					UseDefaultStyle="True"
					DefaultStyleMode="Win7"
					OnInitializeLayout="_WebGrid_InitializeLayout"
				>
					<LayoutSettings
						AllowEdit="Yes"
						AllowSorting="Yes"
						AllowBatchUpdate="true"
					>
						<BatchUpdateSettings
							AllowReviewChanges="true"
							AutomaticObjectUpdate="false"
						/>
					</LayoutSettings>
					<RootTable
						DataKeyField="OrderID"
					>
						<Columns>
							<ISWebGrid:WebGridColumn
								Name="Priority_WebGridColumn"
								Bound="True"
								DataMember="REFURB_PRIORITY"
								EditType="DropdownList"
								Caption="PRIORITY"
								Width="100px"
								NullText=""
								DefaultValue=""
								InputRequired="true"
								InputRequiredErrorText="Priority Needed"
							>
							</ISWebGrid:WebGridColumn>
							<ISWebGrid:WebGridColumn
								Name="Company_WebGridColumn"
								Bound="True"
								EditType="DropdownList"
								Caption="Company"
								Width="100px"
								InputRequired="false"
							>
								<ValueList
									DataSourceID="Company_SqlDataSource"
									DataTextField="CompanyName"
									DataValueField="CustomerID"
								>
								</ValueList>
							</ISWebGrid:WebGridColumn>
							<ISWebGrid:WebGridColumn
								Caption="Order ID"
								EditType="NoEdit"
								DataMember="OrderID"
								Name="OrderID_WebGridColumn"
								Width="100px"
							>
								<CellStyle ForeColor="Black"/>
							</ISWebGrid:WebGridColumn>

						</Columns>
					</RootTable>
				</ISWebGrid:WebGrid>
				</td>
		</tr>
	</table>
	
	<asp:SqlDataSource
		ID="Company_SqlDataSource"
		runat="server"
		ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2005 %>"
		SelectCommand="SELECT [CustomerID], [CompanyName] FROM [Customers] ORDER BY [CustomerID]"
	></asp:SqlDataSource>

	<asp:SqlDataSource
		ID="Orders_SqlDataSource"
		runat="server"
		ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString2005 %>"
		SelectCommand="SELECT TOP 5 [OrderID], [CustomerID], '' AS REFURB_PRIORITY, '' AS GRAPHICS_NEW FROM [Orders] ORDER BY [OrderID]"
	></asp:SqlDataSource>

    </div>
    </form>
</body>
</html>
All times are GMT -5. The time now is 8:42 PM.
Previous Next