Visual Studios ASPX. Using Panels to create Controls dynamically in a column - asp.net

I have a list filled with objects and for each object, I would like to create a new label at run-time. I would like the labels to be stacked on top of each other and not side by side.
for (int i = 0; i < list_product.Count; i++)
{
int labelCount = 1;
Label newLabel = new Label();
newLabel.ID = "newLabel" + labelCount;
newLabel.Text = list_product[i].ProductName;
labelCount++;
panel_test.Controls.Add(newLabel);
}
I've been looking everywhere and I can't find a solution. Thanks for your help!

Related

How to create a table from CodeBehind using ASP.NET Table Control methods (using, Table,TableRow,TableColumn,TableHeader etc.)

I need to create a table, which will have one header and Like This multiple checkboxes. Can someone please give some reference so that I can understand how ASP.NET table control works. I already went thorough MSDN blog. So but I was unable understand that. I will be very happy if someone can explain in details.
Can't see your image because work blocks imgur.com, so hopefully this helps. We use something like this to create custom ListView Controls. But since you can't use a GridView, I would suggest putting a PlaceHolder control on the page. Once the table is created then add it to the PlaceHolder.
Table t = new Table();
TableHeaderRow th = new TableHeaderRow();
//add the header row to table t
t.Rows.Add(th);
//add three columns to the header row with a label for text
for (int i = 0; i < 3; i++)
{
TableCell td = new TableCell();
Label lb = new Label();
lb.Text = "Header Label: " + i.ToString();
td.Controls.Add(lb);
th.Cells.Add(td);
}
//add the rows, say you need five
for (int i = 0; i < 5; i++)
{
TableRow tr = new TableRow();
//add three columns to the table row with a checkbox
for (int i = 0; i < 3; i++)
{
//add the cells with a checkbox
TableCell td1 = new TableCell();
CheckBox cb = new CheckBox();
cb.Text = "CheckBox: " + i.ToString() + "_" + j.ToString();
td.Controls.Add(cb);
tr.Cells.Add(td1);
}
t.Rows.Add(tr);
}
placeHolder.Controls.Add(t);

How to add rows and columns to a table in asp.net dynamically in button

I intend to create a dynamic table with the proviso that every time I click on the button I can add a row to that table.
I'm using the following code but it does not work.
For the first time I click on the button runs, but for the second time that must be added the second row it does not do so, and only one row is added to the previous.
In page
DropDownList[] dlsathetasilat = new DropDownList[50];
Label[] lblradif = new Label[50];
TextBox[] txtreshtetahsili = new TextBox[50];
TextBox[] txtmoasese = new TextBox[50];
TextBox[] txtcity = new TextBox[50];
TextBox []txtdateakhz = new TextBox[50];
CheckBox[] chmadrakmoadel = new CheckBox[50];
TableCell []tc = new TableCell[7];
static int i=0;
In button :
TableRow tr = new TableRow();
lblradif[i] = new Label();
lblradif[i].Text = (i + 1).ToString();
lblradif[i].CssClass = "lbl";
dlsathetasilat[i] = new DropDownList();
dlsathetasilat[i].Items.Add("دیپلم");
dlsathetasilat[i].CssClass = "dl";
txtreshtetahsili[i] = new TextBox();
txtreshtetahsili[i].CssClass = "dl";
txtmoasese[i] = new TextBox();
txtmoasese[i].CssClass = "dl";
txtdateakhz[i] = new TextBox();
txtdateakhz[i].CssClass = "dl";
txtcity[i] = new TextBox();
txtcity[i].CssClass = "dl";
chmadrakmoadel[i] = new CheckBox();
chmadrakmoadel[i].CssClass = "d2";
for (int k = 0; k < 7; k++)
tc[k] = new TableCell();
tc[0].Controls.Add(lblradif[i]);
tc[1].Controls.Add(dlsathetasilat[i]);
tc[2].Controls.Add(txtreshtetahsili[i]);
tc[3].Controls.Add(txtmoasese[i]);
tc[4].Controls.Add(txtcity[i]);
tc[5].Controls.Add(txtdateakhz[i]);
tc[6].Controls.Add(chmadrakmoadel[i]);
for (int j = 0; j < 7; j++)
tr.Controls.Add(tc[j]);
Table1.Rows.Add(tr);
i++;
This is the issue of PostBack and ViewState. ASP.NET does not save the ViewState for Table control. Which means if you want to keep the table data during the post back, you need to bind the table on every post back.
What you can do is save the table values in a ViewState object. Then write a function which creates the table by using the ViewState object. In the on click event of the button, add additional rows to the view state object and then call the function again to recreate the table. This looks quite simple, but the main problem is that if you have additional controls in the table, like TextBox, DropDownList etc. then they do not retain the values after postback, becuase we are recreating the table every time.
By looking at all these problems, I would consider using DataBound controls like Repeater with which you can build the table based on the number of rows that you want to create. Also you can deal with the dynamic controls if you want.

how to add picture box control and label control dynamically in list view control

I want to display picture box control and label into list view for particular record. And all the records comes from the database. I tried it but i am only able to display only single image but i want to display all images with label
the code I try is
DataSet ds3 = load.LoadNewlyAddedBook();
DataTable dt3 = ds3.Tables[0];
lstViewNewAdd.Items.Clear();
int count = dt3.Rows.Count;
for (int a = 0; a < count; a++)
{
DataRow dtRow = dt3.Rows[a];
if (dtRow.RowState != DataRowState.Deleted)
{
ListViewItem lvi3 = new ListViewItem(dtRow["BookName"].ToString());
PictureBox p1 = new PictureBox();
p1.Size = new Size(80, 100);
Byte[] bytes = (Byte[])(dtRow["BookImage"]);
MemoryStream ms = new MemoryStream(bytes);
p1.Image = Image.FromStream(ms);
p1.SizeMode = PictureBoxSizeMode.StretchImage;
Label lbl = new Label();
lbl.Text = dtRow["BookName"].ToString();
lstViewNewAdd.Controls.Add(p1);
//lstViewNewAdd.Controls.Add(lbl);
//lvi3.SubItems.Add(p1);
//lstViewNewAdd.Items.Add(lvi3);
}
}
Please suggest me any solution.
Thanks in advance.
Did you google around?
There are lot of links that can help you
http://geekswithblogs.net/dotNETvinz/archive/2009/04/24/faq-displaying-image-from-database-to-gridview-control.aspx
http://weblogs.asp.net/aghausman/archive/2009/05/26/show-images-on-grid-view-from-file-stream.aspx
http://www.codeproject.com/Articles/20782/Displaying-Images-from-a-Database-in-a-GridView
http://www.codeproject.com/Articles/20971/Thumbnail-Images-in-GridView-using-C

Loop through the labels of a page to assign text

I have some 20 labels on my aspx page for which the IDs are lbl1,lbl2....lbl20 and text is driven by SqlServer table. Is there any easy way to loop through all the labels on the page and assing the text from reader.
I did some thing like but it doesn't work.
SqlDataReader Reader = new SqlDataReader();
int i = 0;
while(Reader.read())
{
label lbl = new label();
lbl.ID = "label" + i;
lbl.text = Reader["ColumnName"].ToString();
}
Is there any other method through which I can loop through all the labels and assign text for it?
If you have them in a container, i think you can do something like this below:
For Each lbl As Control In Grid1.Children
If TypeOf lbl Is Label Then
'your logic
End If
Next
I have only tried this in silverlight however, so i'm not sure it works, or if putting them all in a container is practical in your case.
One way to do this is use the findcontrol method. This would work well because all your labels are named with a "lbl0", "lbl1"... convention.
**start looping:
int index = 0;
string currentLabel = "lbl" + index.ToString();
index++;
Control myControl1 = FindControl(currentLabel);
// cast control to type: (label)
// apply text from reader**
Give that a shot. Hope it works out
I have used this in the past and I just tested it out.
You can do this because every page has a form
HtmlForm form1 = (HtmlForm)Page.FindControl("ContentPlaceHolder1");
for (int i = 1; i <= 3; i++) {
((TextBox)form1.FindControl("label" + i)).Text = "This is label number " + i;
}
If you have a master page change the first line to this
ContentPlaceHolder ph = (ContentPlaceHolder)Page.FindControl("ContentPlaceHolder1");

Making Variable Sized form tables in ASP.Net (there has to be a better way...)

Currently I'm writing code to make variable sized tables with asp:controls in them that look something like this:
TableRow tableHeader = new TableRow();
tableHeader.ID = "tableHeader";
userTable.Rows.Add(tableHeader);
TableCell tableHeaderCell_1 = new TableCell();
tableHeader.Cells.Add(tableHeaderCell_1);
TableCell tableHeaderCell_2 = new TableCell();
Label lblThc2 = new Label();
lblThc2.Text = "<b>Name:</b>";
tableHeaderCell_2.Controls.Add(lblThc2);
tableHeader.Cells.Add(tableHeaderCell_2);
TableCell tableHeaderCell_3 = new TableCell();
Label lblThc3 = new Label();
lblThc3.Text = "<b>User</b>";
tableHeaderCell_3.Controls.Add(lblThc3);
tableHeader.Cells.Add(tableHeaderCell_3);
...
and bodies that look like:
for(int i = 0; i < dynamicTableLength; i++)
{
TableRow tableBodyTemp = new TableRow();
tableBodyTemp.ID = "tableBody" + i;
userTable.Rows.Add(tableBodyTemp);
TableCell tableBodyCell_1 = new TableCell();
Label lblThc2 = new Label();
lblThc2.Text = "<b>"+ i +"</b>";
tableBodyCell_1.Controls.Add(lblThc2);
tableHeader.Cells.Add(tableHeaderCell_1);
TableCell tableBodyCell_2 = new TableCell();
TextBox text = new TextBox();
text.ID = "name" + i;
text.GroupName = "myGroup";
tableHeader.Cells.Add(tableHeaderCell_2);
...
}
There has to be a better way to create tables/forms when you don't know how many rows you need.... or in general!
Can someone help?
Use a repeater control. This lets you keep the markup within the aspx page. It will also allow you to change the markup very easily. You won't be restricted to a table - you can easily change it to divs or lists, if necessary at a later date.
Just create some helper methods to make your code more readable. You've got several copy/paste sections that can be refactored, and that should improve your situation a lot.

Resources