2016-03-30 4 views
1

こんにちは私はドロップダウンリストコードを持っています。ドロップダウンリストにプレースホルダを追加するにはどうすればいいですか?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> 

私はあなただけではHTMLでそれを行うことはできませんenter image description here

答えて

1

このように、あなたはこれを達成するために、HTML + jQueryのを必要とするだろう見せたいです。この後

<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> 

、あなたは削除し、プレースホルダを取り付け直すことで魔法を行うには、あなたのjQueryを必要としています。

<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> 

だけnugetパッケージとしてインストールするか、手動でダウンロードして、ASPXで宣言を追加し、あなたがこれを使用するjQueryのを必要とすることに注意してください。

<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

ブラザーこれを試みる....

<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

プレースホルダは、それが唯一の理由は、あなたが宣言した最初のオプションで示し、文句を言わない仕事を属性。 – DevEstacion

+0

それから何が効きますか? – amit

+1

私のために働いています....試してみてください –

0

多くの場合、最初の項目として空またはダミーアイテムを有することが許容されるであろう。 'Empty'は値が空であることを意味し、テキストは任意のものにすることができます。このような

ので:

<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> 
関連する問題