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 Dropdownlist Selected Value (Text) in Asp.net on Page Load / Code Behind

Jun 3, 2015
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.

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.

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

0 comments :

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

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