473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Add listbox items to the dynamic checkbox text in a dynamic table

lightwalker19
I need to dynamically create multiple checkbox, the data source is a listbox... that listbox receives a sqldatasource with a select statement. So basically what i want is to show users with their own names on a dynamic table. is there any other way? please help :)


Expand|Select|Wrap|Line Numbers
  1.  protected void Panel17_Init(object sender, EventArgs e)
  2.         {
  3.  
  4.             // Create a new HtmlTable object.
  5.             HtmlTable table1 = new HtmlTable();
  6.  
  7.             // Set the table's formatting-related properties.
  8.             table1.Border = 1;
  9.             table1.CellPadding = 6;
  10.             table1.CellSpacing = 3;
  11.             table1.BorderColor = "red";
  12.             table1.Width = "900";
  13.  
  14.             // Start adding content to the table.
  15.             HtmlTableRow row;
  16.             HtmlTableCell cell;
  17.             DropDownList9.SelectedIndex = 1;
  18.             DropDownList9.DataBind();
  19.             ListBox6.DataBind();
  20.             ListBox7.DataBind();
  21.             for (int i = 0; i <ListBox6.Items.Count; i++)
  22.             {
  23.                 // Create a new row and set its background color.
  24.                 row = new HtmlTableRow();
  25.                 row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
  26.  
  27.                for (int j = 0; j < ListBox6.Items.Count; j++)
  28.                 {
  29.                     CheckBox box = new CheckBox();
  30.                     box.ID = "TextBoxRow_" + i ;
  31.                     box.Text = ListBox7.Items[i].ToString() + ListBox6.Items[i].ToString();
  32.  
  33.  
  34.                     Image img = new Image();
  35.                     img.ImageUrl = "~/images/img01.gif";
  36.                     img.ID = "Image" + i ;
  37.                     img.Width = 100;
  38.                     img.Height = 110;
  39.  
  40.                     Literal lt = new Literal();
  41.                     lt.Text = "<br/>";
  42.                     lt.ID = "lit" + i ;
  43.  
  44.  
  45.                     // Create a cell and set its Content.
  46.                     cell = new HtmlTableCell();
  47.                     cell.Align = "center";
  48.  
  49.  
  50.  
  51.  
  52.                     cell.Controls.Add(img);
  53.                     cell.Controls.Add(lt);
  54.                     cell.Controls.Add(box);
  55.  
  56.                     //  Add the cell to the current row.
  57.                     row.Cells.Add(cell);
  58.  
  59.  
  60.                 }
  61.  
  62.                 // Add the row to the table.
  63.                 table1.Rows.Add(row);
  64.             }
  65.  
  66.             // Add the table to the page.
  67.             Panel17.Controls.Add(table1);
  68.  
  69.  
  70.  
  71.  
  72.  
  73.         }
Attached Images
File Type: jpg table.jpg (59.3 KB, 435 views)
Apr 9 '12 #1

✓ answered by Frinavale

I still think you would be better off using an ASP.NET control.
Even if you used a Repeater...it would dynamically generate the table for you, lets you bind to things, and lets you handle events.

For example:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <form id="form1" runat="server">
  8.     <div>
  9.         <asp:Repeater ID="itemsListing" runat="server">
  10.             <HeaderTemplate>
  11.                 <div style="border: 1px solid #C00000; width: 120px;">
  12.             </HeaderTemplate>
  13.             <ItemTemplate>
  14.                 <div style="border: 1px solid #C00000; text-align: center; float: left; width: 55px; margin:1px;
  15.                     overflow: hidden;">
  16.                     <asp:Image ID="itemImage" runat="server" ImageUrl='<%# Eval("imagePath") %>' Style="height: 50px;
  17.                         width: 50px;" />
  18.                     <br />
  19.                     <asp:CheckBox ID="itemSelected" runat="server" Checked='<%# Eval("isSelected") %>'
  20.                         Text='<%#Eval("name") %>' />
  21.                 </div>
  22.             </ItemTemplate>
  23.             <FooterTemplate>
  24.                 <div style="clear: both;height:0px;" />&nbsp;</div>
  25.             </FooterTemplate>
  26.         </asp:Repeater>
  27.     </div>
  28.     </form>
  29. </body>
  30. </html>
Where my C# code-behind is:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10.     System.Data.DataTable tableSource;
  11.  
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (Session["tableSource"] != null)
  15.         {
  16.             tableSource = (System.Data.DataTable)Session["tableSource"];
  17.         }
  18.         else
  19.         {
  20.             tableSource = new System.Data.DataTable();
  21.             tableSource.Columns.Add(new System.Data.DataColumn("imagePath",typeof(string)));
  22.             tableSource.Columns.Add(new System.Data.DataColumn("name", typeof(string)));
  23.             tableSource.Columns.Add(new System.Data.DataColumn("isSelected", typeof(Boolean)));
  24.  
  25.             string[] imageNames = { "Chrysanthemum.jpg", "Desert.jpg", "Hydrangeas.jpg", "Penguins.jpg", "Tulips.jpg" };
  26.             foreach (string imgName in imageNames)
  27.             {
  28.                 System.Data.DataRow imageRow = tableSource.NewRow();
  29.                 imageRow["imagePath"] = String.Format("{0}{1}", "Images/", imgName);
  30.                 imageRow["name"] = imgName;
  31.                 imageRow["isSelected"] = true;
  32.                 tableSource.Rows.Add(imageRow);
  33.             }
  34.         }
  35.  
  36.         itemsListing.DataSource = tableSource;
  37.         itemsListing.DataBind();
  38.     }
  39. }
  40.  
In this example I manipulate how many items are shown on a line by modifying the width of the containing <div> which is defined in the header template for the repeater. I know how big each element is (55px) and so I set the width of the containing <div> to 55*3 for 3/row, 55*4 for 4/row....

-Frinny

6 4699
Frinavale
9,735 Expert Mod 8TB
Why don't you use the CheckBoxList control?

That way you could set the list's DataSource property, indicate the column that contains the Text for the CheckBox, and bind to the data.


-Frinny
Apr 9 '12 #2
i've managed to get things done... but i encounter another 2 problems :(

the first is that my row is not dynamic... cause it has a static value 5.

the second problem is that my solution is also static :(
i stopped the for in 4, 9 and 14. but imagine that there is 30 students in a class... it wont fit because of these problems... help? :)

code above.
Expand|Select|Wrap|Line Numbers
  1. protected void Panel17_Init(object sender, EventArgs e)
  2.         {
  3.  
  4.  
  5.             // Create a new HtmlTable object.
  6.             HtmlTable table1 = new HtmlTable();
  7.  
  8.             // Set the table's formatting-related properties.
  9.             table1.Border = 1;
  10.             table1.CellPadding = 6;
  11.             table1.CellSpacing = 3;
  12.             table1.BorderColor = "red";
  13.             table1.Width = "50";
  14.  
  15.  
  16.  
  17.             // Start adding content to the table.
  18.             HtmlTableRow row;
  19.             HtmlTableCell cell;
  20.             DropDownList9.SelectedIndex = 1;
  21.             DropDownList9.DataBind();
  22.             ListBox6.DataBind();
  23.             ListBox7.DataBind();
  24.             int a = 0;
  25.             for (int  i = 0; i <5; i++)
  26.             {
  27.                 // Create a new row and set its background color.
  28.                 row = new HtmlTableRow();
  29.                 row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
  30.  
  31.  
  32.  
  33.                 for (int j = a; j < ListBox6.Items.Count;j++ )
  34.                     {
  35.                         a = a+1;
  36.                         CheckBox box = new CheckBox();
  37.                         box.ID = "TextBoxRow_" + i;
  38.                         box.Text = ListBox7.Items[j].ToString() + ListBox6.Items[j].ToString();
  39.  
  40.  
  41.                         Image img = new Image();
  42.                         img.ImageUrl = "~/images/img01.gif";
  43.                         img.ID = "Image" + i;
  44.                         img.Width = 100;
  45.                         img.Height = 110;
  46.  
  47.                         Literal lt = new Literal();
  48.                         lt.Text = "<br/>";
  49.                         lt.ID = "lit" + i;
  50.  
  51.  
  52.                         // Create a cell and set its Content.
  53.                         cell = new HtmlTableCell();
  54.                         cell.Align = "center";
  55.  
  56.  
  57.  
  58.  
  59.                         cell.Controls.Add(img);
  60.                         cell.Controls.Add(lt);
  61.                         cell.Controls.Add(box);
  62.  
  63.                         //  Add the cell to the current row.
  64.  
  65.                             row.Cells.Add(cell);
  66.                             if (j == 4)
  67.                             {
  68.                                 j=  ListBox6.Items.Count;
  69.                             }
  70.                             if (j == 9)
  71.                             {
  72.                                 j = ListBox6.Items.Count;
  73.                             }
  74.                             if (j == 14)
  75.                             {
  76.                                 j = ListBox6.Items.Count;
  77.                             }
  78.  
  79.                 }
  80.  
  81.                 // Add the row to the table.
  82.                 table1.Rows.Add(row);
  83.             }
  84.  
  85.             // Add the table to the page.
  86.             Panel17.Controls.Add(table1);
  87.  
  88.  
  89.         }
Attached Images
File Type: jpg table2.jpg (86.9 KB, 342 views)
Apr 9 '12 #3
Frinavale
9,735 Expert Mod 8TB
I still think you would be better off using an ASP.NET control.
Even if you used a Repeater...it would dynamically generate the table for you, lets you bind to things, and lets you handle events.

For example:
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4.     <title></title>
  5. </head>
  6. <body>
  7.     <form id="form1" runat="server">
  8.     <div>
  9.         <asp:Repeater ID="itemsListing" runat="server">
  10.             <HeaderTemplate>
  11.                 <div style="border: 1px solid #C00000; width: 120px;">
  12.             </HeaderTemplate>
  13.             <ItemTemplate>
  14.                 <div style="border: 1px solid #C00000; text-align: center; float: left; width: 55px; margin:1px;
  15.                     overflow: hidden;">
  16.                     <asp:Image ID="itemImage" runat="server" ImageUrl='<%# Eval("imagePath") %>' Style="height: 50px;
  17.                         width: 50px;" />
  18.                     <br />
  19.                     <asp:CheckBox ID="itemSelected" runat="server" Checked='<%# Eval("isSelected") %>'
  20.                         Text='<%#Eval("name") %>' />
  21.                 </div>
  22.             </ItemTemplate>
  23.             <FooterTemplate>
  24.                 <div style="clear: both;height:0px;" />&nbsp;</div>
  25.             </FooterTemplate>
  26.         </asp:Repeater>
  27.     </div>
  28.     </form>
  29. </body>
  30. </html>
Where my C# code-behind is:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class _Default : System.Web.UI.Page
  9. {
  10.     System.Data.DataTable tableSource;
  11.  
  12.     protected void Page_Load(object sender, EventArgs e)
  13.     {
  14.         if (Session["tableSource"] != null)
  15.         {
  16.             tableSource = (System.Data.DataTable)Session["tableSource"];
  17.         }
  18.         else
  19.         {
  20.             tableSource = new System.Data.DataTable();
  21.             tableSource.Columns.Add(new System.Data.DataColumn("imagePath",typeof(string)));
  22.             tableSource.Columns.Add(new System.Data.DataColumn("name", typeof(string)));
  23.             tableSource.Columns.Add(new System.Data.DataColumn("isSelected", typeof(Boolean)));
  24.  
  25.             string[] imageNames = { "Chrysanthemum.jpg", "Desert.jpg", "Hydrangeas.jpg", "Penguins.jpg", "Tulips.jpg" };
  26.             foreach (string imgName in imageNames)
  27.             {
  28.                 System.Data.DataRow imageRow = tableSource.NewRow();
  29.                 imageRow["imagePath"] = String.Format("{0}{1}", "Images/", imgName);
  30.                 imageRow["name"] = imgName;
  31.                 imageRow["isSelected"] = true;
  32.                 tableSource.Rows.Add(imageRow);
  33.             }
  34.         }
  35.  
  36.         itemsListing.DataSource = tableSource;
  37.         itemsListing.DataBind();
  38.     }
  39. }
  40.  
In this example I manipulate how many items are shown on a line by modifying the width of the containing <div> which is defined in the header template for the repeater. I know how big each element is (55px) and so I set the width of the containing <div> to 55*3 for 3/row, 55*4 for 4/row....

-Frinny
Apr 10 '12 #4
i have a problem with that too.. my goal is to retrieve the user by Profile.UserName and Profile.PicturePath.. and when checkbox is selected, add row item to listbox... so i dont know how to do it in a gridview or repeater... :S
Apr 11 '12 #5
Frinavale
9,735 Expert Mod 8TB
Is your user a custom class that you created?
Is it a Principal/identity object?
Is it located in the HttpContext.Current.User property?
In session??

You can handle each CheckBox's CheckChanged Event in your repeater.

Implement a method that handles the event in your server code. Then specify that the CheckBox should call that event on it's CheckChanged event in the repeater's template (make sure to set the CheckBox's autopostback=true so that the server-side code is executed during this event).

In that server-side method that handles the event you can retrieve the CheckBox whose check has changed by casting the sender parameter into a CheckBox.

From there you can retrieve it's checked value.

If it's checked, do what you need to do to add the item to your Principal, Identity, or ListBox (if your ListBox is bound to a list of things in the user/principal/identity, then all you have to do is add it to the user and refresh the binding on your list box).

-Frinny
Apr 11 '12 #6
Thank you very much!!! :DD I made it :D a 1.000.000 thank you ;)
Apr 12 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mel Draper | last post by:
Hello all, I have tried searching for this. I need a listbox populated by my database table where users can re-order the list of items and submit the changes. I can do the listbox and populate...
4
by: Steph | last post by:
Hello, Can someone tell me the script to use for having a change on the same page when using checkbox function ? For example, i would to check one condition and display dynamically a button...
0
by: eddy | last post by:
I have data in a datagrid, how can I write the text file to the users local PC to any locaion that they want Thanks for the help in advance
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
8
by: tshad | last post by:
I have a string that I read from my database: 1|8|5620|541 These are all values in my ListBox. I want to select each of these items (4 of them - but could be many more). At the moment I am...
1
by: savvy | last post by:
I'm comparing two strings and trying to check a dynamic checkbox when its true on pageload , but i dont know why i'm not able to do that on page load, when i click some other links and come back to...
0
by: Dennis | last post by:
I have been needing a tag property for some of my list box items and I've seen this question in this news group before where one solution was to define a usercontrol inheiriting from the list box. ...
1
by: UJ | last post by:
I have a table that I need to manually generate based on stuff in the database. I build the actual table string and stuff it into an asp:panel. So far it works fine. I'm now trying to add a...
2
by: gnosys | last post by:
In ASP.Net 1.1 using C#, I'm trying to dynamically change the background colors of certain listbox items based on some criteria. For example:
0
by: JamesOo | last post by:
I have the code below, but I need to make it searchable in query table, below code only allowed seach the table which in show mdb only. (i.e. have 3 table, but only can search either one only,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.