Get Id of dynamically added control - asp.net

i have one html table "table "and added controls it at run time as :
HtmlTableRow tabelrow = new HtmlTableRow();
HtmlTableCell tablecell = new HtmlTableCell();
Label lbl = new Label();
tablecell.Controls.Add(lbl);
then i want to get each controls by using foreach
such as (foreach control c in table.controls) or (foreach control c in tablecell.controls)
but its not working.

Tried the following. I can get the controls with the ID but you need to assign an ID to it first.
HtmlTable table = new HtmlTable();
for (int i = 0; i < 10; i++)
{
HtmlTableRow tablerow = new HtmlTableRow();
tablerow.ID = Guid.NewGuid().ToString();
HtmlTableCell tablecell = new HtmlTableCell();
tablecell.ID = Guid.NewGuid().ToString();
Label lbl = new Label();
lbl.ID = Guid.NewGuid().ToString();
tablecell.Controls.Add(lbl);
tablerow.Controls.Add(tablecell);
table.Controls.Add(tablerow);
}
List<string>RowID = new List<string>();
List<string>CellID = new List<string>();
List<string>LabelID = new List<string>();
foreach (Control Row in table.Controls)
{
//Each control will be a tablerow.
RowID.Add(Row.ID);
CellID.Add(Row.Controls[0].ID);
LabelID.Add(Row.Controls[0].Controls[0].ID);
}
What exactly are you trying to do? Please elaborate.

Related

how to find control of static control without for loop

private void searchgrid()
{
GridViewRow HeaderGridRow = new GridViewRow(0, 1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox abc = new TextBox();
HeaderCell.Controls.Add(abc);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "";
TextBox search = new TextBox();
search.ID = "search1";
HeaderCell.Controls.Add(search);
HeaderCell.ColumnSpan = 3;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
Button btn_chk = new Button();
btn_chk.Text = "go";
HeaderCell.Controls.Add(btn_chk);
HeaderCell.ColumnSpan = 1;
btn_chk.Click += new EventHandler(btnClick);
HeaderGridRow.Cells.Add(HeaderCell);
//tbl1.Controls.Add(HeaderGridRow);
//btn_chk.CommandName = "btn_chk";
GridView2.Controls[0].Controls.AddAt(0, HeaderGridRow);
//GridView2.Controls[0].Controls.AddAt(0, tbl1);
}
i have a gridview,inside this there is another dynamically created gridview which hAS a textbox.how to find control of this textbox.by looping i get the values but i don get the desired output..so i need just one textbox value..please help
You can do it without a loop like this:
GridView gv = GridView1.Rows[0].FindControl("GridView2") as GridView;
TextBox tb = gv.Rows[0].FindControl("TextBox1") as TextBox;
string tbValue = tb.Text;
You find the nested GridView first and cast it, then you can find the TextBox in the nested GridView.
And you could still always use a loop:
foreach (GridViewRow parentRow in GridView1.Rows)
{
if (parentRow.RowType == DataControlRowType.DataRow)
{
GridView gv = (GridView)parentRow.FindControl("GridView2");
foreach (GridViewRow childRow in gv.Rows)
{
if (childRow.RowType == DataControlRowType.DataRow)
{
TextBox tb = (TextBox)childRow.FindControl("TextBox1");
string tbValue = tb.Text;
}
}
}
}

Populate Table with Table Rows and Cells on LinkButton click event while getting info from XML

Basically I create Rows then Cells then i populate the Cells with LinkButton which is getting Text value from XML nodes .
Code :
private void linkBTN_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
string text = btn.Text.ToString();
XmlDocument clickDoc = new XmlDocument();
clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
int count = btnNode.Count;
foreach (XmlNode node in btnNode)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
row.Cells.Add(cell);
MainTable.Rows.Add(row);
}
}
My problem is that i want to create 4 cells for each row and my code creates 1 cell for each row untill the Nodes end
You need another loop to add more columns to each row
foreach (XmlNode node in btnNode)
{
TableRow row = new TableRow();
for (int i = 0; i < 4; i++){
TableCell cell = new TableCell();
cell.CssClass = "Cell";
LinkButton linkbtn = new LinkButton();
linkbtn.Text = node.InnerText;
linkbtn.Attributes.Add("runat", "server");
Image img = new Image();
cell.Controls.Add(linkbtn);
cell.Controls.Add(new LiteralControl("<br/>"));
cell.Controls.Add(img);
row.Cells.Add(cell);
}
MainTable.Rows.Add(row);
}

Error with the event handlers of dynamic linkbutton

I am retrieving data from database depending upon the texboxes input and storing in a datatable , after then from datatable im sending data into dynamic table and displaying table in a panel,in the table all the data of the first column are of linkbuttons, i wrote event handler for dynamic link buttons , but the event handler is not triggering, and i want to store the linkbutton text in a string in the event handler, but the event handler is not triggering.
Code:
protected void Button1_Click(object sender, EventArgs e)
{
// GridView1.
DataTable dt = new DataTable();
OleDbConnection con = new OleDbConnection(str);
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = "select ID,title,desc from [SO] where ID='" + TextBox1.Text.ToString() + "'or title='" + TextBox2.Text.ToString() + "'";
OleDbDataAdapter db = new OleDbDataAdapter(cmd);
db.Fill(dt);
Table tb = new Table();
tb.BorderColor = Color.Black;
tb.BorderWidth = 2;
DataRow dr;
for (int i = 0; i < dt.Rows.Count; i++)
{
TableRow tr = new TableRow();
tr.BorderColor = Color.Black;
tr.BorderWidth = 2;
tr.ID = "tr" + i;
TableCell tc = new TableCell();
tc.BorderColor = Color.Black;
tc.BorderWidth = 2;
tc.ID = "tc" + i;
TableCell tc1 = new TableCell();
tc1.BorderColor = Color.Black;
tc1.BorderWidth = 2;
tc1.ID = "tc1" + i;
TableCell tc2 = new TableCell();
tc2.BorderColor = Color.Black;
tc2.BorderWidth = 2;
tc2.ID = "tc2" + i;
LinkButton t = new LinkButton();
t.BorderColor = Color.Black;
t.BorderWidth = 2;
t.ID = "t" + i;
t.Click += new EventHandler(t_edit);
TextBox t1 = new TextBox();
t1.BorderColor = Color.Black;
t1.BorderWidth = 2;
t1.ID = "t1" + i;
TextBox t2 = new TextBox();
t2.BorderColor = Color.Black;
t2.BorderWidth = 2;
t2.ID = "t2" + i;
dr = dt.Rows[i];
t.Text = Convert.ToString(dr["ID"]);
t1.Text = Convert.ToString(dr["title"]);
t2.Text = Convert.ToString(dr["desc"]);
tc.Controls.Add(t);
tc1.Controls.Add(t1);
tc2.Controls.Add(t2);
tr.Cells.Add(tc);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tb.Rows.Add(tr);
}
Panel1.Controls.Add(tb);
}
protected void t_edit(object sender, EventArgs e)
{
}
k but by using the sessions concept im retrieving the total table so that the linkbuttons are also retrieving , and i want to add the linkbttons on a button click , here the problem is the eventhandler is not assiging to the linkbutton, and im adding linkbuttons on button click,not on page load.
You must have to use Page_Init or Page_Load event handler to write code that create controls dynamically. Please read MSDN pages on How to add controls dynamically and ASP.NET Page life cycle articles.
You can add event handlers to the Page_Load event but the important think to remember is that they must be added on every page load. It is common to do setup type tasks such as this in a !Page.IsPostBack clause. When wiring up event handlers that's not the case otherwise they will seem to disappear
if(!Page.PostBack)
{
control.EventRaised += new EventHandler(EventResponse)
}
is wrong and will result in the handler disappearing on postback

Generate multiple DropDownLists

I have to generate multiple dropdownlists on clientclick.
That is new dropdownlists on every client click.
I placed a button and wrote code on click event.
protected void addReq1_Click(object sender, ImageClickEventArgs e)
{
DropDownList oDdl = new DropDownList();
oDdl.ID = "ddlReq" + (++i).ToString();
oDdl.DataSourceID = "DSUsers";
oDdl.DataTextField = "UName";
oDdl.DataValueField = "UName";
HtmlTableCell tc0 = new HtmlTableCell();
HtmlTableCell tc1 = new HtmlTableCell();
HtmlTableCell tc2 = new HtmlTableCell();
HtmlTableCell tc3 = new HtmlTableCell();
tc2.Controls.Add(oDdl);
HtmlTableRow tr = new HtmlTableRow();
tr.Cells.Add(tc0);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
search2.Rows.Add(tr);
}
Here "DSUsers" is SqlDataSource.
"i" is static variable.
"serarch2" is html table with runat server tag
The problem is only one control is getting rendred,
after that on every additional click same DropDownList is getting replaced.
No new DropDownList is added to the page.
Thank You.
When you dynamically add any controls to your page, they are lost on the next postback. In order for this to work, you will need to test i in your PreInit or Init event and create the correct number of rows there.
for (int x = 0 to i) {
DropDownList oDdl = new DropDownList();
oDdl.ID = "ddlReq" + (++x).ToString();
oDdl.DataSourceID = "DSUsers";
oDdl.DataTextField = "UName";
oDdl.DataValueField = "UName";
HtmlTableCell tc0 = new HtmlTableCell();
HtmlTableCell tc1 = new HtmlTableCell();
HtmlTableCell tc2 = new HtmlTableCell();
HtmlTableCell tc3 = new HtmlTableCell();
tc2.Controls.Add(oDdl);
HtmlTableRow tr = new HtmlTableRow();
tr.Cells.Add(tc0);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
search2.Rows.Add(tr);
}
Obligatory link to the ASP.NET page lifecycle.

ASP.NET Dynamically creating HTMLTableColumns (headers)

is it possible to dynamically create html table columns (< th>< /th>) using namespae System.Web.UI.HtmlControls ?
try this:
HtmlTable table1 = new HtmlTable();
// Set the table's formatting-related properties.
table1.Border = 1;
table1.CellPadding = 3;
table1.CellSpacing = 3;
table1.BorderColor = "red";
// Start adding content to the table.
HtmlTableRow row;
HtmlTableCell cell;
for (int i=1; 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=1; j<=4; j++)
{
// Create a cell and set its text.
cell = new HtmlTableCell();
cell.InnerHtml = "Row: " + i.ToString()+ "<br />Cell: " + j.ToString();
// 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.
this.Controls.Add(table1);
Yes in that name space you will want to look at:
HtmlTable
HtmlTableRow
HtmlTableCell
Use is like so:
HtmlTable htmlTable = new HtmlTable(); //creating table object
HtmlTableRow htmlRow = new HtmlTableRow(); //creating row object
HtmlTableCell htmlTableCell = new HtmlTableCell(); //create cell
htmlTableCell.InnerHtml = "<b>Test Text Bolded</b>";//setting cell content
HtmlTableCell textTableCell = new HtmlTableCell(); //create cell
textTableCell.InnerText = "Test plain text"; //setting cell content
htmlRow.Cells.Add(htmlTableCell); //add cell to row
htmlRow.Cells.Add(textTableCell); //add cell to row
htmlTable.Rows.Add(htmlRow); // add row to table
HtmlTableCell = new HtmlTableCell("th");

Resources