Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Set Default Value in Dropdownlist in Asp.net using C#, VB.NET

Jun 3, 2015
Introduction

Here I will explain how to set default value in dropdownlist in
asp.net using c#, vb.net or setting default value for dropdownlist in asp.net using c#, vb.net. In dropdownlist we can set default value by using selectedindex and selectedvalue properties in asp.net using c#, vb.net.


To set default value in dropdownlist we need to write the code like as shown below

Method 1

using selectedindex property we can set dropdownlist default value


// set dropdown default value using selectedindex
ddlusers.SelectedIndex = 2;

Method 2

using selectedvalue property we can set dropdownlist default value


// set dropdown default value using selectedvalue
ddlusers.SelectedValue = 2;

If you want to check it in complete example open your aspx page and write following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>set dropdownlist default value in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlusers" runat="server">
<asp:ListItem Value="1">Suresh</asp:ListItem>
<asp:ListItem Value="2">Rohini</asp:ListItem>
<asp:ListItem Value="3">Madhav</asp:ListItem>
<asp:ListItem Value="4">Mahendra</asp:ListItem>
<asp:ListItem Value="5">Honey</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>

Now add following namespaces in code behind

C# Code


using System;

After completion of adding namespaces you need to write the code like as shown below


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// set dropdown default value using selectedvalue
ddlusers.SelectedValue = "2";
}
}

VB.NET Code


Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
' set dropdown default value using selectedvalue
ddlusers.SelectedValue = "2"
End If
End Sub
End Class

Demo


Set Default Value in Dropdownlist in Asp.net using C#, VB.NET

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

0 comments :

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.