How to Display Selected Text in Label from DropdownList Control in ASP.Net

Here in this asp.net tutorials we are going to learn how to display a value in Label control from DropDownList Control in asp.net c#.
Here we design a page with one DropdownlList control, one Button Control and a Label Control. In this asp.net Example  we will display the Selected Text of DropdownList in Label Control while clicking the Button Control.

First Design the ASP.Net we form by writing below HTML code:

<table>
<tr>
<td colspan=”3″>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
Select Value :</td>
<td style=”width: 100px”>
<asp:DropDownList ID=”Drpvalue” runat=”server” Width=”128px”>
<asp:ListItem Value=”0″>SELECT</asp:ListItem>
<asp:ListItem Value=”1″>Meera</asp:ListItem>
<asp:ListItem Value=”2″>Academy</asp:ListItem>
<asp:ListItem Value=”3″>ASP</asp:ListItem>
<asp:ListItem Value=”4″>PHP</asp:ListItem>
<asp:ListItem Value=”5″>SQL</asp:ListItem>
</asp:DropDownList></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
</td>
<td style=”width: 100px; text-align: left;”>
<asp:Button ID=”btnanswer” runat=”server” Font-Bold=”True” Height=”32px” OnClick=”btnanswer_Click”
Text=”Answer” Width=”80px” /></td>
<td style=”width: 100px; text-align: left”>
</td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
</td>
<td colspan=”2″ style=”text-align: left”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True”></asp:Label></td>
</tr>
</table>

The ASP.Net dropdownlist example output is:

Dropdownlist control example in asp.net

Dropdownlist control example in asp.net

Now, after doing this write the below code on button click events:

protected void btnanswer_Click(object sender, EventArgs e)
{
if (Drpvalue.SelectedItem.Text == “SELECT”)
{
Label1.Text = “Select First Value !!”;
}
else
{
Label1.Text = “You selected value = “+Drpvalue.SelectedItem.Text;
}
}

Now, If you Not Selected any value from dropdownlist and click the button you will have error message on label like below:

Dropdownlist control example in asp.net

Dropdownlist control example in asp.net

and if you select any value of dropdownlist then you will get value in Label like below screen of asp.net.

Dropdownlist control example in asp.net

Dropdownlist control example in asp.net

 

I hope you will enjoy this asp.net tutorials of dropdownlist control.

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 3 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">