Drop Down List

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Drop Down List as PDF for free.

More details

  • Words: 734
  • Pages: 5
Dropdownlist: 29.1 I have 3 DropDownLists on the page : one for a Month, second one for a Day, and the last one for a Year. How can I let the user choose the data and enter it to 'datetime' column in the database. VB.NET

Dim dt as DateTime = DateTime.Parse(ddlMonth.SelectedItem.Text & "/" & ddlDay.SelectedItem.Text & "/" & ddlYear.SelectedItem.Text )

C#

DateTime dt = DateTime.Parse(ddlMonth.SelectedItem.Text & "/" & ddlDay.SelectedItem.Text & "/" & ddlYear.SelectedItem.Text );

29.2 How to apply css style to each item of the dropdownlist or listbox control? The dropDownList has a bug which prevent us from assigning the style property to each item in the DropDownList. This bug confirmed by Microsoft in Microsoft Knowledge Base Article - 309338 For the workaround use a HTML dropdownlist with runat=server tag

<SELECT id="DropDownList1" runat="server" >

Use namespace System.Reflection VB.NET

If Not Page.IsPostBack Then Dim col As FieldInfo For Each col In GetType(KnownColor).GetFields If col.FieldType Is GetType(Drawing.KnownColor) Then DropDownList1.Items.Add(New ListItem(col.Name, col.Name)) End If Next End If Dim i As Integer For i = 0 To DropDownList1.Items.Count - 1 DropDownList1.Items(i).Attributes.Add("style", "background-color:" + DropDownList1.Items(i).Text) Next

C#

if (!IsPostBack) { foreach(FieldInfo col in typeof(KnownColor).GetFields() )

{

if (col.FieldType == typeof(KnownColor) ) { DropDownList1.Items.Add(new ListItem(col.Name ,col.Name)); }

} } for (int i= 0 ;i < DropDownList1.Items.Count;i++) { DropDownList1.Items[i].Attributes.Add("style", "background-color:" + DropDownList1.Items[i].Text); }

29.3 How to concate and display 2 fields in a dropdownlist? Method1

"Select FirstName + ' ' + LastName as fullname from Employees"

then use 'fullname' as the datatextfield for the dropdownlist Method2

Dim newColumn As New DataColumn() With newColumn .ColumnName = "fullname" .DataType = System.Type.GetType("System.String") .Expression = "LastName+' '+FirstName" End With ds.Tables(0).Columns.Add(newColumn) ddlName.DataTextField = "FullName" ddlName.DataValueField = "employeeid" ddlName.DataSource = ds.Tables(0).DefaultView ddlName.DataBind()

C#

DataColumn newColumn =new DataColumn(); newColumn.ColumnName = "fullname"; newColumn.DataType = System.Type.GetType("System.String"); newColumn.Expression = "LastName+' '+FirstName"; ds.Tables[0].Columns.Add(newColumn); DropDownList1.DataTextField = "FullName"; DropDownList1.DataValueField = "employeeid"; DropDownList1.DataSource = ds.Tables[0].DefaultView; DropDownList1.DataBind();

29.4 I have a page with a Dropdownlist and some simple code for the SelectedIndexChanged but it does not do a postback? If you want the dropdownlist to cause a postback when selection changes set AutoPostBack property to "True" for the dropdownlist control.

29.5 How to add an extra item to the DropDownList filled with data from a database? VB.NET

'DropDownList1.Items.Insert(0, "Please Select") DropDownList1.Items.Insert(0, new ListItem("", ""))

C#

//DropDownList1.Items.Insert(0, "Please Select"); DropDownList1.Items.Insert(0, new ListItem("", ""));

This should be done after .DataBind of dropdownlist 29.6 Can I use a dropdownlist to allow user to select multiple items from a list of available items? No. Dropdownlist Web Control allows user to select only one item from a list of items. 29.7 Why do I get System.Data.DataRowView/System.Data.Common.DbDataRecord as the item in a dropdownlist? This is probably because you have not set the DataTextField/ DatavalueField property of the dropdownlist. VB.NET

'.. DropDownList1.DataSource = ds.Tables(0) DropDownList1.DataTextField = "CategoryName" DropDownList1.DataValueField = "CategoryId" DropDownList1.DataBind()

C#

//.. DropDownList1.DataSource =ds.Tables[0]; DropDownList1.DataTextField = "CategoryName"; DropDownList1.DataValueField = "CategoryId"; DropDownList1.DataBind();

29.8 How can I display a Tool tip for a dropdownlist?

The ToolTip property is inherited from the WebControl class and is not applicable to the DropDownList control. This implementation of the ToolTip property does not allow you to set a value and returns String. In a word, if we can make this (a dropdown with ToolTip) work with plain HTML and client side script, we should be able to emit the necessary code from ASP.NET. More Details at DropDownList.ToolTip Property 29.9 How to select a specific Item in a DropDownList? Method 1: VB.NET

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue()) 'DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText())

C#

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue()); //DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText());

Method 2: VB.NET

DropDownList1.Items.FindByText("").Selected = true 'DropDownList1.Items.FindByValue("").Selected = true

C#

DropDownList1.Items.FindByText("").Selected = true ; //DropDownList1.Items.FindByValue("").Selected = true ;

29.10 How to display multiple spaces in a dropdownlist?



VB.NET

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim i As Integer For i = 0 To 10 DropDownList1.Items.Add("Item Number" + SpaceDDL(10) + i.ToString) Next End Sub

Private Function SpaceDDL(ByVal numberOfSpaces As Integer) As String Dim Spaces As String Dim i As Integer For i = 0 To numberOfSpaces Spaces &= " " Next Return Server.HtmlDecode(Spaces) End Function

C#

private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here for(int i = 0 ;i<=10;i++) { DropDownList1.Items.Add("Item Number" + SpaceDDL(10) + i.ToString()); } } string SpaceDDL(int numberOfSpaces ) { string Spaces=""; for(int i = 0 ;i<=numberOfSpaces;i++) { Spaces += " "; } return Server.HtmlDecode(Spaces); }

Related Documents

Drop Down List
November 2019 15
Dynamic Drop Down
November 2019 13
Edit Drop Down Box
November 2019 15
Cara Membuat Menu Drop Down
November 2019 40