Pages

Thursday, 29 September 2011

How to get (search, find) array index position by array element value in asp.net(Array.IndexOf)


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    private string[] controls = { "SqlDataSource", "AccessDataSource", "ObjectDataSource", "XmlDataSource", "LinqDataSource", "EntityDataSource", "SiteMapDataSource" };
    protected void Page_Load(object sender, System.EventArgs e) {
        if(!this.IsPostBack)
        {
            Label1.Text = "String array created successfully!<br />Array elements:<br /><br />";
            foreach (string element in controls)
            {
                Label1.Text += element + "<br />";
            }
        }
    }
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        int indexOf = Array.IndexOf(controls, "LinqDataSource");
        Label2.Text ="We Find [LinqDataSource] At the index position: " + indexOf.ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to get (search, find) array index position by array element value in asp.net(Array.IndexOf)</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net Array.IndexOf() example:<br />Search Array Index Number By Element Value</h2>
        <asp:Label
             ID="Label1"
             runat="server"
             Font-Size="Large"
             ForeColor="SeaGreen"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br />
        <asp:Label
             ID="Label2"
             runat="server"
             Font-Size="Large"
             ForeColor="DodgerBlue"
             Font-Bold="true"
             Font-Italic="true"
             >
        </asp:Label>
        <br /><br />
        <asp:Button
             ID="Button1"
             runat="server"
             OnClick="Button1_Click"
             Font-Bold="true"
             Text="Search Array [LinqDataSource] Index Position"
             ForeColor="SeaGreen"
             />  
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment