how to find control of static control without for loop - asp.net

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

Related

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

Add second header row to GridView BELOW already existing header row

I'm trying to dynamically insert a 2nd header row at a GridView's OnRowCreated event. However, I can't seem to get the row to be inserted anywhere besides the first spot in the gridview's row index. The code below fails on the last line, where the header is actually added to the grid. Index out of bounds exception. How can I add this header row below the already existing header row? Help is much appreciated, thanks!
protected void gvwProd_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridView gvw = (GridView)sender;
GridViewRow HeaderRow = new GridViewRow(1, 1, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell.Text = "Prod Comments - TS/LID";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 4;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Materials Comments - TS/LD";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 8;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
HeaderCell = new TableCell();
HeaderCell.Text = "Plant Comments - TS/LID";
HeaderCell.Style["font-weight"] = "bold";
HeaderCell.ColumnSpan = 11;
HeaderCell.Wrap = false;
HeaderRow.Cells.Add(HeaderCell);
gvw.Controls[0].Controls.AddAt(1, HeaderRow);
}
}
Had a similar problem. You could try casting the GridView to a Table and access the .Rows method:
((Table)gvw.Controls[0]).Rows.AddAt(1, HeaderRow);
source

Creating a Textbox dynamically in for loop

I was trying to create a table dynamically and put textboxes in it. Here in the following code, i was trying to create a textbox with a distinct name for each k. But only the last value of k is getting displayed in the textbox. I was wondering how i could give a name to Textbox so all are shown.
for (int k = 0; k < tblCols; k++)
{
TableCell tc = new TableCell();
TextBox txtCompanyName = new TextBox();
txtCompanyName.Text = dsmissing.Tables[0].Rows[tblCols- 1]["NewCompanyName"].ToString();
tc.Controls.Add(txtCompanyName);
}
Replace this line
dsmissing.Tables[0].Rows[tblCols- 1]["NewCompanyName"].ToString();
With
dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
I assume you are talking about ID like below.
for (int k = 0; k < tblCols; k++)
{
TableCell tc = new TableCell();
TextBox txtCompanyName = new TextBox();
txtCompanyName.Text = dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
txtCompanyName.ID = Guid.NewGuid().ToString("N");
tc.Controls.Add(txtCompanyName);
}
I think you may have meant to do this:
for (int k = 0; k < tblCols; k++)
{
TableCell tc = new TableCell();
TextBox txtCompanyName = new TextBox();
//txtCompanyName.Text = dsmissing.Tables[0].Rows[tblCols-1 ["NewCompanyName"].ToString();
txtCompanyName.Text = dsmissing.Tables[0].Rows[k]["NewCompanyName"].ToString();
tc.Controls.Add(txtCompanyName);
}
Surely this will work and it is easy to understand
protected void Page_Load(object sender, EventArgs e)
{
button1();
}
protected void Page_Init(object sender, EventArgs e)
{
try
{
Label lbl = new Label();
lbl.ID = "lbl_label";
lbl.Text = "Enter the values";
form1.Controls.Add(lbl);
TextBox tb = new TextBox();
tb.ID = "tbx_textbox";
form1.Controls.Add(tb);
Button bt = new Button();
bt.ID = "bt_button";
bt.Text = "click";
form1.Controls.Add(bt);
}
catch (Exception ex) { }
}
public void button1()
{
Table table = new Table();
TableRow row = null;
TableCell cell = null;
TextBox tbx1 = this.Page.FindControl("tbx_textbox") as TextBox;
try
{
int a = int.Parse(tbx1.Text);
for (int i = 0; i < a; i++)
{
row = new TableRow();
cell = new TableCell();
TextBox tx = new TextBox();
tx.ID = "box" + i.ToString();
cell.Controls.Add(tx);
row.Cells.Add(cell);
table.Rows.Add(row);
form1.Controls.Add(table);
}
}
catch (Exception ex) { }
finally
{
table = null;
}
}

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

Get Id of dynamically added control

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.

Resources