Monday, 4 November 2013

How do I extract a string of text that lies between two (parenthesis) in ASP.NET DropDownList using C#.NET/VB.NET

Hi Friends,in this article i will explain about  How do I extract a string of text that lies between two (parenthesis) in ASP.NET using C#.NET/VB.NET.
I already explained in the previous articles about Sorting GridView By Columns Header In Asp.Net Ascending and Descending in ASP.NET using C#/VB.NET , Ajax ModalPopUpExtender Example to edit the GridView row values in ASP.NET and ASP.NET: Move Items from One ListBox to Another ListBox using C#/VB.NET

In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How do I extract a string of text that lies between two (parenthesis) using .NET?</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DdlExtract" 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:DropDownList>    
         <asp:Button runat="server" ID="btnSubmit" Text="Submit"
            onclick="btnSubmit_Click" />
          <asp:Label ID="lblSelect" runat="server"></asp:Label>
    </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 Ddl : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string ddlapp = DdlExtract.SelectedItem.ToString();
        int start = ddlapp.IndexOf("(");
        int end = ddlapp.IndexOf(")");
        string result = ddlapp.Substring(start + 1, end - start - 1);
        lblSelect.Text = result;
        lblSelect.ForeColor = System.Drawing.Color.Red;
    }
}

In VB.NET:
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class DdlTest
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim ddlapp As String = DdlExtract.SelectedItem.ToString()
        Dim start As Integer = ddlapp.IndexOf("(")
        Dim [end] As Integer = ddlapp.IndexOf(")")
        Dim result As String = ddlapp.Substring(start + 1, [end] - start - 1)
        lblSelect.Text = result
        lblSelect.ForeColor = System.Drawing.Color.Red
    End Sub


End Class




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

When you select the value DropDownList and click on Submit button then it will show the value in the parenthesis value in the selectedItem in the DropDownList. 


 "If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook pageAspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

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