Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Help with my custom listbox code please!

Posted on 2008-10-29
5
651 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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

809 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