Find Dynamic columns of a RadGrid - asp.net

I created Dynamic columns (textBox) in RadGrid on a Click Event. How do I find those Controls on another Click Event.
for (int i = 0; i < objISTUserDetailsObjectList.Count; i++)
{
tempCol = new GridTemplateColumn();
tempCol.ItemTemplate = new DynamicTemplateCoulmn("txtQuantity" + i, "numericTextBox");
tempCol.UniqueName = "Users" + i;
tempCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.ItemStyle.HorizontalAlign = HorizontalAlign.Right;
tempCol.FooterStyle.HorizontalAlign = HorizontalAlign.Right;
gvwISTProduct.MasterTableView.Columns.Add(tempCol);
}
I an unable to find the "txtxQuantity" on another Click event although i find it in Grids ItemDataBound event.
Thanks in Advance.

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);

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

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!

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 assign target control to validator control on serverside

I am having N numbers of Text boxes those are generating dynamically. I want to validate each textbox for Formate HH:MM:SS PM/AM so i dynamicaly create the validation control . but as the dynamic textbox has no ID , so what i have to pass to the Validation control for ControlToValidate Property ?
AccordionPane ap1;
for (int i = 0; i <= 2; i++)
{
ap1 = new AccordionPane();
ap1.HeaderContainer.Controls.Add(new LiteralControl("Using Markup"));
Panel pnl = new Panel();
TextBox txtTime = new TextBox();
txtTime.ID = "txtTimeBox" + i;
txtTime.ValidationGroup = "MKE";
RegularExpressionValidator validateDate = new RegularExpressionValidator();
validateDate.ValidationExpression = "(0[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9]|60) (AM|PM|am|pm)";
validateDate.ControlToValidate = txtTime.ID;
validateDate.ErrorMessage = "Invalid Date. Try in HH:MM:SS AM/am|PM/pm format";
pnl.Controls.Add(txtTime);
ap1.ContentContainer.Controls.Add(pnl);
MyAccordion.Panes.Add(ap1);
}
Page.Controls.Add(divContainer);

How to create line breaks between dynamically generated labels in a placeholder?

This is the code below in code behind file's Page_Load event:
LinkButton linkButton = new LinkButton();
linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i;
linkButton.ForeColor = Color.Blue;
linkButton.Font.Bold = true;
linkButton.Font.Size = 14;
linkButton.Font.Underline = false;
linkButton.Text = itemList[i].ItemTitle.InnerText;
linkButton.Click += new EventHandler(LinkButton_Click);
linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText);
PlaceHolder1.Controls.Add(linkButton);
Label label = new Label();
label.ID = "LabelDynamicInPlaceHolder1Id" + i;
label.ForeColor = Color.DarkGray;
label.Text = itemList[i].ItemDescription.InnerText;
PlaceHolder1.Controls.Add(label);
I want a line break between each control generated.
Solution to your Line Break issue is below however, if you're doing this in the Page_Load event, then your event handlers won't work and your going to run into Page Life Cycle issues. Basically, in order for your event handlers to fire on PostBack, you really need to be creating these dynamic controls earlier in the Page Life Cycle. Try moving your code to the OnInit method if you do run into this problem.
LinkButton linkButton = new LinkButton();
linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i;
linkButton.ForeColor = Color.Blue;
linkButton.Font.Bold = true;
linkButton.Font.Size = 14;
linkButton.Font.Underline = false;
linkButton.Text = itemList[i].ItemTitle.InnerText;
linkButton.Click += new EventHandler(LinkButton_Click);
linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText);
PlaceHolder1.Controls.Add(linkButton);
//Add This
PlaceHolder1.Controls.Add(new LiteralControl("<br />"));
Label label = new Label();
label.ID = "LabelDynamicInPlaceHolder1Id" + i;
label.ForeColor = Color.DarkGray;
label.Text = itemList[i].ItemDescription.InnerText;
PlaceHolder1.Controls.Add(label);
Another solution is that you could add each control to a Panel, which will render them each in a <div> resulting in the effect you're looking for.
To me this would be more dynamic because if you hide any of the controls the div will collapse and not leave empty lines.
LinkButton linkButton = new LinkButton();
linkButton.ID = "LinkButtonDynamicInPlaceHolder1Id" + i;
linkButton.ForeColor = Color.Blue;
linkButton.Font.Bold = true;
linkButton.Font.Size = 14;
linkButton.Font.Underline = false;
linkButton.Text = itemList[i].ItemTitle.InnerText;
linkButton.Click += new EventHandler(LinkButton_Click);
linkButton.Attributes.Add("LinkUrl",itemList[i].ItemLink.InnerText);
//Add control to a panel, add panel to placeholder
Panel lbPan = new Panel();
lbPan.Controls.Add(linkButton);
PlaceHolder1.Controls.Add(lbPan);
Label label = new Label();
label.ID = "LabelDynamicInPlaceHolder1Id" + i;
label.ForeColor = Color.DarkGray;
label.Text = itemList[i].ItemDescription.InnerText;
//Add control to a panel, add panel to placeholder
Panel lblPan = new Panel();
lblPan.Controls.Add(label);
PlaceHolder1.Controls.Add(lblPan);
How to: Add Controls to an ASP.NET Web Page Programmatically
In some instances, you might want to
create both static text and controls.
To create static text, you can use either a Literal or a Label Web server
control. You can then add these
controls to the container as you would
any other control. For information
about view state in controls created
at run time, see Dynamic Web Server
Controls and View State.

Resources