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 :) - protected void Panel17_Init(object sender, EventArgs e)
-
{
-
-
// Create a new HtmlTable object.
-
HtmlTable table1 = new HtmlTable();
-
-
// Set the table's formatting-related properties.
-
table1.Border = 1;
-
table1.CellPadding = 6;
-
table1.CellSpacing = 3;
-
table1.BorderColor = "red";
-
table1.Width = "900";
-
-
// Start adding content to the table.
-
HtmlTableRow row;
-
HtmlTableCell cell;
-
DropDownList9.SelectedIndex = 1;
-
DropDownList9.DataBind();
-
ListBox6.DataBind();
-
ListBox7.DataBind();
-
for (int i = 0; i <ListBox6.Items.Count; i++)
-
{
-
// Create a new row and set its background color.
-
row = new HtmlTableRow();
-
row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
-
-
for (int j = 0; j < ListBox6.Items.Count; j++)
-
{
-
CheckBox box = new CheckBox();
-
box.ID = "TextBoxRow_" + i ;
-
box.Text = ListBox7.Items[i].ToString() + ListBox6.Items[i].ToString();
-
-
-
Image img = new Image();
-
img.ImageUrl = "~/images/img01.gif";
-
img.ID = "Image" + i ;
-
img.Width = 100;
-
img.Height = 110;
-
-
Literal lt = new Literal();
-
lt.Text = "<br/>";
-
lt.ID = "lit" + i ;
-
-
-
// Create a cell and set its Content.
-
cell = new HtmlTableCell();
-
cell.Align = "center";
-
-
-
-
-
cell.Controls.Add(img);
-
cell.Controls.Add(lt);
-
cell.Controls.Add(box);
-
-
// Add the cell to the current row.
-
row.Cells.Add(cell);
-
-
-
}
-
-
// Add the row to the table.
-
table1.Rows.Add(row);
-
}
-
-
// Add the table to the page.
-
Panel17.Controls.Add(table1);
-
-
-
-
-
-
}
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: -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
<asp:Repeater ID="itemsListing" runat="server">
-
<HeaderTemplate>
-
<div style="border: 1px solid #C00000; width: 120px;">
-
</HeaderTemplate>
-
<ItemTemplate>
-
<div style="border: 1px solid #C00000; text-align: center; float: left; width: 55px; margin:1px;
-
overflow: hidden;">
-
<asp:Image ID="itemImage" runat="server" ImageUrl='<%# Eval("imagePath") %>' Style="height: 50px;
-
width: 50px;" />
-
<br />
-
<asp:CheckBox ID="itemSelected" runat="server" Checked='<%# Eval("isSelected") %>'
-
Text='<%#Eval("name") %>' />
-
</div>
-
</ItemTemplate>
-
<FooterTemplate>
-
<div style="clear: both;height:0px;" /> </div>
-
</FooterTemplate>
-
</asp:Repeater>
-
</div>
-
</form>
-
</body>
-
</html>
Where my C# code-behind is: -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class _Default : System.Web.UI.Page
-
{
-
System.Data.DataTable tableSource;
-
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (Session["tableSource"] != null)
-
{
-
tableSource = (System.Data.DataTable)Session["tableSource"];
-
}
-
else
-
{
-
tableSource = new System.Data.DataTable();
-
tableSource.Columns.Add(new System.Data.DataColumn("imagePath",typeof(string)));
-
tableSource.Columns.Add(new System.Data.DataColumn("name", typeof(string)));
-
tableSource.Columns.Add(new System.Data.DataColumn("isSelected", typeof(Boolean)));
-
-
string[] imageNames = { "Chrysanthemum.jpg", "Desert.jpg", "Hydrangeas.jpg", "Penguins.jpg", "Tulips.jpg" };
-
foreach (string imgName in imageNames)
-
{
-
System.Data.DataRow imageRow = tableSource.NewRow();
-
imageRow["imagePath"] = String.Format("{0}{1}", "Images/", imgName);
-
imageRow["name"] = imgName;
-
imageRow["isSelected"] = true;
-
tableSource.Rows.Add(imageRow);
-
}
-
}
-
-
itemsListing.DataSource = tableSource;
-
itemsListing.DataBind();
-
}
-
}
-
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
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. - protected void Panel17_Init(object sender, EventArgs e)
-
{
-
-
-
// Create a new HtmlTable object.
-
HtmlTable table1 = new HtmlTable();
-
-
// Set the table's formatting-related properties.
-
table1.Border = 1;
-
table1.CellPadding = 6;
-
table1.CellSpacing = 3;
-
table1.BorderColor = "red";
-
table1.Width = "50";
-
-
-
-
// Start adding content to the table.
-
HtmlTableRow row;
-
HtmlTableCell cell;
-
DropDownList9.SelectedIndex = 1;
-
DropDownList9.DataBind();
-
ListBox6.DataBind();
-
ListBox7.DataBind();
-
int a = 0;
-
for (int i = 0; i <5; i++)
-
{
-
// Create a new row and set its background color.
-
row = new HtmlTableRow();
-
row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan");
-
-
-
-
for (int j = a; j < ListBox6.Items.Count;j++ )
-
{
-
a = a+1;
-
CheckBox box = new CheckBox();
-
box.ID = "TextBoxRow_" + i;
-
box.Text = ListBox7.Items[j].ToString() + ListBox6.Items[j].ToString();
-
-
-
Image img = new Image();
-
img.ImageUrl = "~/images/img01.gif";
-
img.ID = "Image" + i;
-
img.Width = 100;
-
img.Height = 110;
-
-
Literal lt = new Literal();
-
lt.Text = "<br/>";
-
lt.ID = "lit" + i;
-
-
-
// Create a cell and set its Content.
-
cell = new HtmlTableCell();
-
cell.Align = "center";
-
-
-
-
-
cell.Controls.Add(img);
-
cell.Controls.Add(lt);
-
cell.Controls.Add(box);
-
-
// Add the cell to the current row.
-
-
row.Cells.Add(cell);
-
if (j == 4)
-
{
-
j= ListBox6.Items.Count;
-
}
-
if (j == 9)
-
{
-
j = ListBox6.Items.Count;
-
}
-
if (j == 14)
-
{
-
j = ListBox6.Items.Count;
-
}
-
-
}
-
-
// Add the row to the table.
-
table1.Rows.Add(row);
-
}
-
-
// Add the table to the page.
-
Panel17.Controls.Add(table1);
-
-
-
}
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: -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml">
-
<head runat="server">
-
<title></title>
-
</head>
-
<body>
-
<form id="form1" runat="server">
-
<div>
-
<asp:Repeater ID="itemsListing" runat="server">
-
<HeaderTemplate>
-
<div style="border: 1px solid #C00000; width: 120px;">
-
</HeaderTemplate>
-
<ItemTemplate>
-
<div style="border: 1px solid #C00000; text-align: center; float: left; width: 55px; margin:1px;
-
overflow: hidden;">
-
<asp:Image ID="itemImage" runat="server" ImageUrl='<%# Eval("imagePath") %>' Style="height: 50px;
-
width: 50px;" />
-
<br />
-
<asp:CheckBox ID="itemSelected" runat="server" Checked='<%# Eval("isSelected") %>'
-
Text='<%#Eval("name") %>' />
-
</div>
-
</ItemTemplate>
-
<FooterTemplate>
-
<div style="clear: both;height:0px;" /> </div>
-
</FooterTemplate>
-
</asp:Repeater>
-
</div>
-
</form>
-
</body>
-
</html>
Where my C# code-behind is: -
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
-
public partial class _Default : System.Web.UI.Page
-
{
-
System.Data.DataTable tableSource;
-
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
if (Session["tableSource"] != null)
-
{
-
tableSource = (System.Data.DataTable)Session["tableSource"];
-
}
-
else
-
{
-
tableSource = new System.Data.DataTable();
-
tableSource.Columns.Add(new System.Data.DataColumn("imagePath",typeof(string)));
-
tableSource.Columns.Add(new System.Data.DataColumn("name", typeof(string)));
-
tableSource.Columns.Add(new System.Data.DataColumn("isSelected", typeof(Boolean)));
-
-
string[] imageNames = { "Chrysanthemum.jpg", "Desert.jpg", "Hydrangeas.jpg", "Penguins.jpg", "Tulips.jpg" };
-
foreach (string imgName in imageNames)
-
{
-
System.Data.DataRow imageRow = tableSource.NewRow();
-
imageRow["imagePath"] = String.Format("{0}{1}", "Images/", imgName);
-
imageRow["name"] = imgName;
-
imageRow["isSelected"] = true;
-
tableSource.Rows.Add(imageRow);
-
}
-
}
-
-
itemsListing.DataSource = tableSource;
-
itemsListing.DataBind();
-
}
-
}
-
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
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
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
Thank you very much!!! :DD I made it :D a 1.000.000 thank you ;)
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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. ...
|
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...
|
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:
|
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,...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |