?
Solved

Display values in an array on the webpage

Posted on 2005-03-09
18
Medium Priority
?
247 Views
Last Modified: 2012-05-05
I have a list of values in the array and I want to display them on the webpage (asp.net) with a check box next to it. The values will be keep changing, so I want to do this programatically. The tricky part is I want to display the values and the check box only on the right hand side of my webpage. How can I do this?
0
Comment
Question by:praveenuni
[X]
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
  • 10
  • 8
18 Comments
 
LVL 4

Expert Comment

by:jaynus
ID: 13499402
1.  Create a foreach loop to loop through your array and create new table rows, cells, and checkbox's for each value. It would look something like this (Excuse the dirtyness of the code, its something I have laying around on a site of mine):

-------------------------------------
int BoxCount;
HtmlTableRow newRow;
HtmlTableCell TitleCell, FieldCell;
CheckBox[] tempCheckBox;
System.Web.UI.HtmlControls.HtmlTable ContainerTable = new HtmlTable();

BoxCount = 0;
tempCheckBox = new CheckBox[array.Length];

foreach(string item in array){
        newRow = new HtmlTableRow();
        TitleCell = new HtmlTableCell();
        FieldCell = new HtmlFieldCell();

        ......
        fill your values however you want or need to, along with row/cell styles etc
        ........

        tempCheckBox[BoxCount].ID = BoxCount;
        FieldCell.Controls.Add(tempCheckBox[BoxCount]);

        newRow.Controls.Add(TitleCell);
        newRow.Controls.Add(FieldCell);
        ContainerTable.Controls.Add(newRow);
        BoxCount++;
}

-------------------------------------

the above code will add new rows and checkbox's etc. to your ContainerTable. You now have an array of checkbox's, ID'd by their count position, and they can referenced them programatically via this array of checkbox's. You can do the same thing with the cells and rows if you want to.  Or you can just loop through the Control Collections within each Control(gets slow after a bit).

1a. Your other option is to create a DataGrid with CheckBox values within them, and put your array into a DataSet to do this. I have never really used DataGrid's in asp.net, so sorry I cant give that answer, it may be easier though, or it may not be. I dont know.


2. To put it on the right hand side, you either break your page into tables with 100% width, and make 3 Cells and break it up appropriately.  Either that or create a div with an absolute position. e.g. <div align="right" style="position:absolute;"> And that will align it to the right side of the page no matter what.


Hope that all made sense.


-jaynus
0
 

Author Comment

by:praveenuni
ID: 13499616
Thanks for the reply ..

here are the problems ....

When I used the above code, its giving me lot of errors (for eg., array.length statement). I think asp.net/C# will not allow for the array.length statement. Moreover I'm novice in C#, could you please elaborate the code. For me its quite confusing.

Regards
Praveen
0
 
LVL 4

Accepted Solution

by:
jaynus earned 2000 total points
ID: 13499852
Ahh, I was just giving a generic example of how it would be done, obviously none of the names of anything really apply to anything you've written, so copy+paste wont work very well hehe. Let me write up a full Page_Load() that does this, and the related .aspx page. I simplified it a bit, and customized it a bit more from my code I had to what your looking to do. Comments Galor!

------------------------

// I just put this here so it wouldnt error. You should change any instances of this array to yours you
// want the data read from.
protected string[] YourArrayOfValues = {"Test Value 1", "Test Value 2", "Test Value 3"};
// The Array of checkboxs
protected CheckBox[] checkBoxArray;
// Where we are putting our control
protected System.Web.UI.WebControls.PlaceHolder placeHolder;

private void Page_Load(object sender, System.EventArgs e) { // The Page_Load Method, executed on load.
      int BoxCount = 0;
      HtmlTableRow newRow;
      HtmlTableCell FieldCell;
      
      System.Web.UI.HtmlControls.HtmlTable ContainerTable = new HtmlTable();

      checkBoxArray = new CheckBox[YourArrayOfValues.Length];  // Create our Array of CheckBox's based on the array of values
      foreach(string item in YourArrayOfValues) {  // foreach() loop on the Array of Values.
            newRow = new HtmlTableRow();     // Create a new row
            FieldCell = new HtmlTableCell(); // Create a new cell (column)
            checkBoxArray[BoxCount] = new CheckBox(); // Create a new instance of CheckBox object

            checkBoxArray[BoxCount].ID = Convert.ToString(BoxCount); // Set the CheckBox's ID to the count. Change this if you want.
            checkBoxArray[BoxCount].Text = item;      // This puts the Values in your Array next to the checkbox, change this if you want.
            FieldCell.Controls.Add(checkBoxArray[BoxCount]);  // Add our checkbox to the cell (column)

            newRow.Controls.Add(FieldCell); // Add the cell (column) to this row.
            ContainerTable.Controls.Add(newRow); // Add this row to our table.
            BoxCount++; // Iterate the count
      }

      this.placeHolder.Controls.Add(ContainerTable);  // Now that we are done, add this Table to the page.
}

private void ReadTheCheckBoxs() { // This reads all the checkbox's in our array
      foreach(CheckBox currentBox in checkBoxArray) { // Loop through or checkbox's
            // Do what you need to here, this is just for testing.
            Response.Write(currentBox.Checked);
      }
}

------------

On your .aspx page, put this control wherever you want this table to be:

<asp:PlaceHolder ID="placeHolder" Runat="server" />

Play around with it of course until you get what you want, but thats the very very very basic way to do this. You can add styles etc to the cells and rows and table, as well as move values around or anything else youd like.

Quick note, You cannot edit the .InnerHtml or .InnerText values of the column, because a control is programmatically added to it.

Hope thats better heh. *shrug*

-jaynus


0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:praveenuni
ID: 13500669
Hi Jaynus,

Thank you very much for the reply. I was really amazed looking at your reply.

the code works perfect .. but my array (name: LayerNameList) will get the values dynamically. How can I assign my dynamically created array LayerNameList instead of protected string[] YourArrayOfValues = {"Test Value 1", "Test Value 2", "Test Value 3"}; statement. and one more thing is how can I control the fonts?

Regards
-- Praveen
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13500756
To fix it for your array:
1. Delete that declaration line (protected string[] YourArrayOfValues = {"Test Value 1", "Test Value 2", "Test Value 3"};
2. Do a search and replace on the file, Search for YourArrayOfValues and replace it with the name of your array, whatever that may be.

2. To control fonts, its fairly simply as long as your semi-familiar with CSS styles. You add to the Style member of the TableCell(The TableCell being the variable FieldCell). This is done withe call FieldCell.Styles.Add("Stylename", "StyleValue");

Heres an example that would set the font to Arial, the size to 'xx-small', and the color to red for the cell named FieldCell.

FieldCell.Styles.Add("font-family", "Arial");
FieldCell.Styles.Add("font-size", "xx-small");
FieldCell.Styles.Add("text-color", "red");

Alternatively, you can just create a CSS class on your page and add it to the cell as an attribute.
FieldCell.Attributes.Add("class", "MyClassName");

-jaynus
0
 

Author Comment

by:praveenuni
ID: 13500807
Can you please explain how to get the values from the array LayerNameList  and how to declare the statement. I'm novice in C# and trying to learn this.

Regards
-- Praveen
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13500843
If you want the fastest easiest way without having to modify the code, just add at the top of the Page_Load function..

YourArrayOfValues = LayerNameList;

Thats the easiest way to do it without modifying.
I just created that array (YourArrayOfValues) to test it and make sure it worked for you.
0
 

Author Comment

by:praveenuni
ID: 13500920
This is how I declared ..

string[] YourArrayOfValues = {"Test Value 1", "Test Value 2", "Test Value 3"};
LayerNameList1 = LayerNameList;

The error is "Cannot convert array list to string"

Is that way you asked me to declare?

0
 

Author Comment

by:praveenuni
ID: 13500930
I tried this too ... string[] YourArrayOfValues  = LayerNameList; but still the same error !

Any suggestions!
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13500932
Can you show the full chunk of code you have right now for that section?
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13500948
Oh. I see it now.


----------------------------------------------------
Change this section of code:
-------------------------------------------
// I just put this here so it wouldnt error. You should change any instances of this array to yours you
// want the data read from.
protected string[] YourArrayOfValues = {"Test Value 1", "Test Value 2", "Test Value 3"};
// The Array of checkboxs
protected CheckBox[] checkBoxArray;
// Where we are putting our control
protected System.Web.UI.WebControls.PlaceHolder placeHolder;

private void Page_Load(object sender, System.EventArgs e) { // The Page_Load Method, executed on load.
     int BoxCount = 0;
     HtmlTableRow newRow;
     HtmlTableCell FieldCell;

----------------------------------------------------
To this..
----------------------------------------------------


// I just put this here so it wouldnt error. You should change any instances of this array to yours you
// want the data read from.
protected string[] YourArrayOfValues;
// The Array of checkboxs
protected CheckBox[] checkBoxArray;
// Where we are putting our control
protected System.Web.UI.WebControls.PlaceHolder placeHolder;

private void Page_Load(object sender, System.EventArgs e) { // The Page_Load Method, executed on load.
     int BoxCount = 0;
     HtmlTableRow newRow;
     HtmlTableCell FieldCell;
     YourArrayOfValues = LayerNameList;


----------------------------------------------------
0
 

Author Comment

by:praveenuni
ID: 13500954
oh I'm sorry it actuall worked! This is the correct statement .. ArrayList YourArrayOfValues = LayerNameList;

Thanks for your time and help

Regards
-- Praveen
0
 

Author Comment

by:praveenuni
ID: 13511003
Hello Jaynus,

There is  a small problem in the above code .. After I check each checkbox I want some C# code to be executed .. Where can I place my C# code ?

Regards
Praveen
0
 
LVL 4

Expert Comment

by:jaynus
ID: 13511064
Anywhere you want really :) As long as the page has loaded, the objects will be readable. If they are checked, then the array will be populated in your class. You could just call ReadTheCheckBoxs() from Page_Load() and replace my little loop with whatever you want, and have some event call taht method.

Its really whatever you want :)
0
 

Author Comment

by:praveenuni
ID: 13511077
Could you please explain that with a small example or code?

0
 
LVL 4

Expert Comment

by:jaynus
ID: 13511147
Referencing my above code examples:

Change this chunk of code:
------------
    this.placeHolder.Controls.Add(ContainerTable);  // Now that we are done, add this Table to the page.
}

private void ReadTheCheckBoxs() { // This reads all the checkbox's in our array
     foreach(CheckBox currentBox in checkBoxArray) { // Loop through or checkbox's
          // Do what you need to here, this is just for testing.
          Response.Write(currentBox.Checked);
     }
}
--------------------
To this:
    this.placeHolder.Controls.Add(ContainerTable);  // Now that we are done, add this Table to the page.
    ReadTheCheckBoxs();
}

private void ReadTheCheckBoxs() {
     // Do whatever you want here to process them!!
}
0
 

Author Comment

by:praveenuni
ID: 13511271
Thank you for the prompt and descriptive reply ..

Depending on the values of array .. we are populating the checkboxes .. so on the webpage, checkbox and the value are displayed side by side .. if I check any of the check boxes .. depending on the value next to it ... the C# code will be executed ..  and if I uncheck the same box some other C# code should be exceuted ...  Please help me on this case ..

regards
Praveen
0
 

Author Comment

by:praveenuni
ID: 13511894
If you need any further info or code .. please let me know

- Praveen
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

777 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