Solved

Help with my custom listbox code please!

Posted on 2008-10-29
5
649 Views
Last Modified: 2013-12-17
I am working on a  custom listbox  and using the code from http://www.codeproject.com/KB/aspnet/CustomListBox.aspx which is great but its done with an array giving the info to the listbox i need it to instead read from a textfile and populate the list box via that and based on a if condition insert the correct image behind each line of text. I got the code for reading from the text file figured out just not sure how to put it in.

How would i change this code around to do that? Thanks!
Here is my code i have it spearated into 2 parts my design source code and my C# code:
 

Design Source Code:
 

<html>

<head>

    <title>untitled</title>

    <link href="MyStyle.css" type="text/css" rel="stylesheet" />

    <script language="javascript">

var selected = null;
 

function setColor(elem, col)

{

	if(elem == selected || elem == null)

		return;

	elem.style.background = col;

	elem.style.cursor='default';
 

}

function select(elem)

{
 

	if(elem == selected || elem == null)

		return;

	else if(selected != null)

	{

	   selected.style.background = "white";

	   selected.style.color = "black";
 
 

	}

	setColor(elem, "blue");

	selected = elem;

	elem.style.color = "white";
 
 

	var val = elem.innerHTML;

    document.getElementById("selVal").value = val;
 

}
 

</script>

</head>

<body bgcolor="#c5dcf6">

    <form id="form_1" runat="server">

        <font color="blue"><font size=+2>List box</font> 

        <br />

        </font> 

        <div style="OVERFLOW: auto; WIDTH: 100px; HEIGHT: 100px">

            <select style="OVERFLOW: hidden; HEIGHT: 100%" size="100" name="ServerList" noscroll="noscroll">

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

                <option value="abcdefghijklmnopqrestuvwxyz">abcdefghijklmnopqrestuvwxyz</option>

            </select>

        </div>

        <br />

        <br />

        <font color="blue" size=+2>Custom list box</font> 

        <br />

        <div style="BORDER-RIGHT: gray thin groove; BORDER-TOP: gray thin groove; OVERFLOW: auto; BORDER-LEFT: gray thin groove; WIDTH: 345px; BORDER-BOTTOM: gray thin groove; HEIGHT: 334px"><% Response.Write(getList());%>

        </div>

        <p>

            <font color="red">Server Side Code</font> 

        </p>

        <p>

            <input id="selVal" type="hidden" runat="server" />

        </p>

        <p>

            See selected value --&gt; ( <asp:Label id="lblInfo" runat="server"></asp:Label>&nbsp;)

            &nbsp;<asp:Button id="cmdOK" onclick="cmdOK_Click" runat="server" text="OK"></asp:Button>

        </p>

        <script language="javascript">

select(document.getElementById("selected"),"blue");
 

</script>

    </form>

</body>

</html>
 

C# code:
 

 private string Render(ArrayList items)

    {

     String HtmlList = String.Empty;

    

    try

        {

            HtmlList = HtmlList + "<TABLE  width=\"100%\" bgColor=\"white\" border=\"0\" cellpadding =\"0\" cellspacing=\"0\" > <TBODY class=\"list\">";

    

            for(int i=0; i<items.Count; i++)

            {

                string[] item = (string[]) items[i];

    

                string imgPath = item[0];

                HtmlList += "<tr>";

                HtmlList += "<td>" ;

                if(imgPath != "")

                    HtmlList += "<img src=\"" + imgPath + "\">"; // add image if item contains it.

                HtmlList += "</td>" ;

    

                HtmlList += "<td onmouseover=\"setColor(this, 'LightGreen');\" onmouseout=\"setColor(this, 'white');\" onclick=\"select(this);\"> " + item[1] +"</td>" ;

    

                for(int j=2;j<item.Length; j++) // to add sub items of each item and behave like list view.

                {

                    HtmlList += "<td > " + item[j] +"</td>" ;

    

                }

    

                HtmlList += "</tr>";

            }

            HtmlList = HtmlList + "</tbody></TABLE>";

            return HtmlList;

        }

        catch(Exception ex)

        {

            HtmlList = HtmlList + "</tbody></TABLE>";

            return ex.Message;

        }

    }

    

    public  string getList()

    {

        String HtmlList = String.Empty;

        ArrayList al = new ArrayList();

    

        for(int i=0; i<10; i++)

        {

            string[] item = new string[2];

    

            item[0] = "ServerDown.jpg"; // for a list box with images

    

            // Main item to select

            item[1] = "MainItem_" + i.ToString();

        

            al.Add(item); //add item in array list

        }

        string[] bigItem = new string[2];

        bigItem[0] = "";

        bigItem[1] = "abcdefghijklmnopqrestuvwxyz";

        al.Add(bigItem);

        return Render(al);

    }

    

    public void Page_Load(object obj, System.EventArgs e)

    {

          if(!IsPostBack)

          {

            selVal.Value = "";

          }

    

    }

    

    public void cmdOK_Click(object obj, System.EventArgs e)

    {

           lblInfo.Text = selVal.Value;

    }

Open in new window

0
Comment
Question by:Figle
  • 4
5 Comments
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22842745
in your getList() function there is a look up to 10 for adding 10 items to the array list
instead of that for loop you need to loop for all the lines in the text file and add the line text in item[1] and if you know what image needs to be shown you need to update that in item[0].

the solution can be provided better if we know what kind of format the text file has from which you need to populate the arraylist
0
 

Author Comment

by:Figle
ID: 22843027
aw sorry its a .txt text file from notepad
0
 

Author Comment

by:Figle
ID: 22843240
would i need to use a for each to beable to reference each line of text from the .txt file to my ping and display it like i did in the below code?
protected void Page_Load(object sender, EventArgs e)

    {

        string[] servers = File.ReadAllLines(Server.MapPath("TestFile.txt"));

        foreach (string svr in servers)

        {

            PingSvr(svr);  

        }

    }
 

    public void PingSvr(string hostName)

    {

            Ping pingSender = new Ping(); PingOptions options = new PingOptions();

            // Use the default Ttl value which is 128,
 

            // but change the fragmentation behavior. 

            options.DontFragment = true;
 

            // Create a buffer of 32 bytes of data to be transmitted. 

            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
 

            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);

            int timeout = 120;

            //hostName = svr;

            PingReply reply = pingSender.Send(hostName, timeout, buffer, options);
 

            if (reply.Status == IPStatus.Success)

            {  

                ListBox1.Items.Add(hostName + " is Online");

               

                serverstatus2.ImageUrl = "~/ServerUp.jpg";

            }
 

            else

            {

                ListBox1.Items.Add(hostName + " is Down");

                

                serverstatus2.ImageUrl = "~/ServerDown.jpg";

            }

      

    }

Open in new window

0
 

Author Comment

by:Figle
ID: 22844581
I figured it out,i really got to stop over anazling stuff, thanks for ya help!
0
 

Author Closing Comment

by:Figle
ID: 31511541
Could used more detailed insructions and maybe some code examples but thanks for the help thou!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now