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.
Description:
In previous articles I explained populate dropdownlist from database in asp.net, filter gridview with dropdownlist using filter expression in asp.net, get selected values from dropdownlist in angularjs, jQuery show asp.net exception message in ajax calls, cascading dropdownlist in gridview using asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to set default value in dropdownlist in asp.net using c#, vb.net.
In previous articles I explained populate dropdownlist from database in asp.net, filter gridview with dropdownlist using filter expression in asp.net, get selected values from dropdownlist in angularjs, jQuery show asp.net exception message in ajax calls, cascading dropdownlist in gridview using asp.net and many articles relating to gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to set default value in dropdownlist 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
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. |
|||
|
|||
0 comments :
Note: Only a member of this blog may post a comment.