Introduction:
Here I will explain how to set dropdownlist selected value on page load or code behind in asp.net using c#, vb.net or set dropdownlist selected item by value or text in asp.net using c#, vb.net. In dropdownlist we can set selected item by value or text using FindByValue and FindByText properties in asp.net using c#, vb.net.
Description:
In previous articles I explained dropdown validation using javascript in html, populate dropdownlist from database in asp.net, cascading dropdownlist in asp.net gridview, country, city, state cascading dropdownlist in asp.net and many articles relating to dropdownlist, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to set dropdownlist selected value in asp.net using c#, vb.net.
In previous articles I explained dropdown validation using javascript in html, populate dropdownlist from database in asp.net, cascading dropdownlist in asp.net gridview, country, city, state cascading dropdownlist in asp.net and many articles relating to dropdownlist, gridview, asp.net, c#,vb.net and jQuery. Now I will explain how to set dropdownlist selected value in asp.net using c#, vb.net.
To
set dropdownlist selected value we have two different methods those are
Set Dropdownlist Selected Value Based
on value
using
FindByValue property we can set
dropdownlist selected value
// set dropdownlist selected based on value
ddlusers.Items.FindByValue("2").Selected
= true;
|
Set Dropdownlist Selected value based
on Text
using
FindByText property we can set
dropdownlist selected value
// set dropdownlist selected based on text
ddlusers.Items.FindByText("Rohini").Selected
= true;
|
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 dropdownlist selected based on text
ddlusers.Items.FindByText("Rohini").Selected
= true;
}
}
|
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 dropdownlist selected based on text
ddlusers.Items.FindByText("Rohini").Selected
= True
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.