2016-03-30 26 views
1

Merhaba Açılır liste kodum var Yer tutucu eklemek istiyorum nasıl ekleyebilirim?Aşağı açılır liste içinde yer tutucu nasıl eklenir C#

<asp:DropDownList id="ddlyear" runat="server" > 
        <asp:ListItem >Experience</asp:ListItem> 
        <asp:ListItem>Fresher</asp:ListItem> 
        <asp:ListItem>1</asp:ListItem> 
        <asp:ListItem>2</asp:ListItem> 
        <asp:ListItem>3</asp:ListItem> 
</asp:DropDownList> 

Bunu başarmak için Html + jQuery gerekirdi yalnız HTML bunu yapamaz bu enter image description here

cevap

1

gibi göstermek istiyorum. Bundan sonra

<asp:DropDownList ID="ddlyear" runat="server"> 
    <asp:ListItem>Experience</asp:ListItem> 
    <asp:ListItem>Fresher</asp:ListItem> 
    <asp:ListItem>1</asp:ListItem> 
    <asp:ListItem>2</asp:ListItem> 
    <asp:ListItem>3</asp:ListItem> 
</asp:DropDownList> 

, sen kaldırarak büyü yapmak için jQuery ihtiyaç ve yer tutucu ekleyerek yeniden. jQuery sadece Nuget paket olarak yüklemek veya manuel olarak indirmek, bunu kullan ve aspx bir deklarasyon eklemek için gereken

<script> 
var isChanged = false; 
$(function() { 
    $('#ddlyear').focusin(function() { 
     if (!isChanged) { 
// this removes the first item which is your placeholder if it is never changed 
      $(this).find('option:first').remove(); 
     } 
    }); 
    $('#ddlyear').change(function() { 
// this marks the selection to have changed 
     isChanged = true; 
    }); 
    $('#ddlyear').focusout(function() { 
     if (!isChanged) { 
// if the control loses focus and there is no change in selection, return the first item 
      $(this).prepend('<option selected="selected" value="0">Experience</option>'); 
     } 
    }); 
}); 
</script> 

dikkate alın.

<head runat="server"> 
    <title></title> 
// Sample only, you can place it in any location or use any version 
    <script src="../scripts/jquery-2.2.2.min.js"></script> 
</head> 
0

@amit

Kardeş bu deneyin ....

<select placeholder="select your beverage"> 
<option value="" default="" selected="">select your beverage</option> 
<option value="tea">Tea</option> 
<option value="coffee">Coffee</option> 
<option value="soda">Soda</option> 
</select> 
+1

tutucudur sadece çünkü ilan ilk seçeneğin gösterir, alışkanlık işi bağlıyor. – DevEstacion

+0

sonra ne işe yarıyor? – amit

+1

benim için çalışıyor .... bir deneyin –

0

Pek çok durumda, birinci öğe olarak boş veya sahte ürünlere sahip olduklarını kabul edilebilir. 'Boş', değerin boş olduğu anlamına gelir, metin istediğiniz herhangi bir şey olabilir. Böyle

Yani:

<asp:DropDownList id="ddlyear" runat="server"> 
    <asp:ListItem Value="">Select</asp:ListItem> 
    <asp:ListItem Value="Experience">Experience</asp:ListItem> 
    <asp:ListItem Value="Fresher">Fresher</asp:ListItem> 
    <asp:ListItem Value="1">1</asp:ListItem> 
    <asp:ListItem Value="2">2</asp:ListItem> 
    <asp:ListItem Value="3">3</asp:ListItem> 
</asp:DropDownList> 
İlgili konular