Monday 4 November 2013

Dropdownlist items find by partial value in ASP.NET using C#/VB.NET

Hi friends, in this article I will explain about Dropdownlist items find by partial value in ASP.NET using C#/VB.NET.
I already explained in the previous articles about How do I extract a string of text that lies between two (parenthesis) in ASP.NET using C#.NET/VB.NETSorting GridView By Columns Header In Asp.Net Ascending and Descending in ASP.NET using C#/VB.NET and Ajax ModalPopUpExtender Example to edit the GridView row values in ASP.NET

In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Dropdownlist items find by partial value in ASP.NET using C#/VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox runat="server" ID="txtSearch"></asp:TextBox>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="txtSearch" style="color:Red"></asp:RequiredFieldValidator>
        <asp:DropDownList ID="DdlValue" runat="server">
            <asp:ListItem Value="0">--Select--</asp:ListItem>
            <asp:ListItem Value="1">A(Kishore)</asp:ListItem>
            <asp:ListItem Value="2">B(SaiR)</asp:ListItem>
            <asp:ListItem Value="3">C(Anil)</asp:ListItem>
            <asp:ListItem Value="4">D(Krish)</asp:ListItem>
            <asp:ListItem Value="5">E(Satyam)</asp:ListItem>
            <asp:ListItem Value="6">F(Nag)</asp:ListItem>      
        </asp:DropDownList>
      
           <asp:Button runat="server" ID="btnSubmit" Text="Submit"
            onclick="btnSubmit_Click" />
    </div>
    </form>
</body>
</html>

In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DdlPartial : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
       for (int i = DdlValue.Items.Count - 1; i > 0; i--)
        {
            string s = DdlValue.Items[i].Text;

            if (DdlValue.Items[i].Text.Contains(txtSearch.Text))
            {
                DdlValue.ClearSelection();
                DdlValue.Items[i].Selected = true;
                break;
            }
        }

    }
}


In VB.NET:
Imports System.Web
Partial Class DdlPartial
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For i As Integer = DdlValue.Items.Count - 1 To 1 Step -1
            Dim s As String = DdlValue.Items(i).Text
            If DdlValue.Items(i).Text.Contains(txtSearch.Text) Then
                DdlValue.ClearSelection()
                DdlValue.Items(i).Selected = True
                Exit For
            End If
        Next
    End Sub
End Class



The output of the above code as shown in the below figure.

When you enter the value in the textbox and click on Submit button then it will the select the corresponding value in the Dropdownlist .

No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.