My question i asked for "Randomly" adding controls in spefic
Table Cells. e.g.
if i call funtion CreateTable() , it should create tables
with vary number of rows and columns, and then for example if i
want a text box in Cell(0,1), a dropdown in cell(2,1) then again a text
area control in Cell(5,1) etc etc.( u getting my point, i am not
putting one type of control), how can i code that.The control state should be saved in the table.And whenever i open particylar webform it show shos all the control which i have created before
private void CreateTable(short noOfRows)
{
PlaceHolder p1 = new PlaceHolder();
Table table1 = new Table();
table1.BorderWidth = 1;
table1.BorderStyle = BorderStyle.Solid;
TableRow[] rows = new TableRow[noOfRows];
for (int i = 0; i < rows.Length; i++)
{
rows[i] = new TableRow();
}
table1.Rows.AddRange(rows);
TextBox t1 = new TextBox();
t1.Text = "Hello";
t1.EnableViewState = true;
TextBox t2 = new TextBox();
t2.Text = "World";
t2.EnableViewState = true;
Button btnOk = new Button();
btnOk.EnableViewState = true;
btnOk.Text = "OK";
Button btnCancel = new Button();
btnCancel.EnableViewState = true;
btnCancel.Text = "Cancel";
TableCell cell1=new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
cell1.Controls.Add(t1);
cell2.Controls.Add(t2);
cell3.Controls.Add(btnOk);
cell4.Controls.Add(btnCancel);
table1.Rows[0].Cells.AddAt(0, cell1);
table1.Rows[0].Cells.AddAt(1, cell2);
table1.Rows[1].Cells.AddAt(0, cell3);
table1.Rows[1].Cells.AddAt(1, cell4);
p1.Controls.Add(table1);
form1.Controls.Add(p1);
}
Related
This code works fine like adding a row and deleting a row dynamically .But when i delete some of rows from table and then click the add button to add some rows into the panel then the flickering starts and increases while adding more rows.
Public Global Variables
public TableLayoutPanel table;
public int tablecol, tablerow;
Function for creating a table in Main Window.
This function creates a table with some header labels and Button named "ADD" .
public void createtable()
{
Label title = new Label(); title.Text = "Table Name";
this.myw.right_panel.Controls.Add(title);
TextBox tname = new TextBox(); tname.Width = 300;
this.myw.right_panel.Controls.Add(tname);
Button addrowbtn = new Button(); addrowbtn.Text = "ADD ROW";
addrowbtn.Click += addrowbtn_Click;
this.myw.right_panel.Controls.Add(addrowbtn);
table = new TableLayoutPanel();
table.Width = this.myw.right_panel.Width;
table.Height = 400;
table.AutoScroll = true;
table.ColumnCount = 9;
int col = 0;
Label[] titlelbl = new Label[9];
foreach(Label l in titlelbl)
{
titlelbl[col++] = new Label();
}
titlelbl[0].Text = "NAME";
titlelbl[0].Width = 200;
titlelbl[1].Text = "TYPE";
titlelbl[2].Text = "NOT NULL";
titlelbl[3].Text = "PK";
titlelbl[4].Text = "AI";
titlelbl[5].Text = "U";
titlelbl[6].Text = "DEFAULT";
titlelbl[7].Text = "CHECK";
titlelbl[8].Text = "DELETE";
col = 0;
FlowLayoutPanel headerpanel = new FlowLayoutPanel();
headerpanel.Width = this.myw.right_panel.Width;
headerpanel.AutoSize = true;
foreach(Label l in titlelbl)
{
l.BackColor = Color.LightGray;
l.TextAlign = ContentAlignment.MiddleCenter;
headerpanel.Controls.Add(l);
}
this.myw.right_panel.Controls.Add(headerpanel);
this.myw.right_panel.Controls.Add(table);
}
This Function is used to create a dynamic row in the table .Each row contains a button named "DEL"
void addrowbtn_Click(object sender, EventArgs e)
{
TextBox name = new TextBox(); name.Width = 200;
ComboBox type = new ComboBox();
type.Items.AddRange(new string[] {"INTEGER","TEXT","BLOB","NUMERIC","REAL"});
CheckBox notnull = new CheckBox();
CheckBox pk = new CheckBox();
CheckBox ai = new CheckBox();
CheckBox u = new CheckBox();
TextBox def = new TextBox(); def.Width = 100;
TextBox check = new TextBox(); check.Width = 100;
Button delbtn = new Button(); delbtn.Text = "DEL";delbtn.AutoSize = true;
delbtn.Click += Delbtn_Click;
table.Controls.Add(name, 0, tablerow);
table.Controls.Add(type, 1, tablerow);
table.Controls.Add(notnull, 2, tablerow);
table.Controls.Add(pk, 3, tablerow);
table.Controls.Add(ai,4, tablerow);
table.Controls.Add(u, 5, tablerow);
table.Controls.Add(def, 6, tablerow);
table.Controls.Add(check,7, tablerow);
table.Controls.Add(delbtn, 8, tablerow);
tablerow++;
}
Function for Deleting a Row,
This function will be called whenever a delete button on the table row clicks
private void Delbtn_Click(object sender, EventArgs e)
{
TableLayoutPanelCellPosition tlpcp = new TableLayoutPanelCellPosition();
tlpcp = table.GetPositionFromControl((Control)sender);
for (int i = 0; i < table.ColumnCount; i++)
{
var control = table.GetControlFromPosition(i, tlpcp.Row);
table.Controls.Remove(control);
}
}
Any code improvement suggestions are welcome.
I have a gridview. I have created two columns dynamically in the Row_DataBound event of the gridview.
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCell cell1 = new TableCell();
cell1.Width = 100;
e.Row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
cell2.Width = 100;
e.Row.Cells.Add(cell2);
}
else
{
TableCell cell1 = new TableCell();
cell1.Width = 100;
cell1.Text = "<span style='font-weight:bold'>Total Punches";
e.Row.Cells.Add(cell1);
TableCell cell2 = new TableCell();
cell2.Width = 110;
cell2.Text = "<span style='font-weight:bold'>Hours Worked";
e.Row.Cells.Add(cell2);
}
I want to export the whole grid to Excel sheet. When I click the Export button, the last two columns which I created on Row_DataBound disappear in the excel sheet.
Can anyone help me solving this.
Thanks in Advance.
Try to move the logic to RowCreated event.
I am adding a table header row dynamically as shown below. It is rendered correctly.
So now I have 2 headers.
protected void gvCustomers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";
TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";
TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";
TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";
newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
}
If my gridview is having 3 rows, and loop through gridview it will read only 2 row.
It is skiping the last row.
Any thoughts
I solved this. write this code on RowCreated event.
I wrote this code on RowDataBound so created header row was not consistent.
protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";
TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";
TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";
TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";
newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
I am adding a table header row dynamically as shown below. It is rendered correctly. Now I need to read the first cell value of the newly added header row in a button click event. How can we read the header value?
I cannot use gvCustomers.Rows since it will not take header row.
I cannot use gvCustomers.HeaderRow.Cells[0].Text; also since there are two header rows
CODE
protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell cell1 = new TableHeaderCell();
cell1.ColumnSpan = 1; //e.Row.Cells.Count;
cell1.Text = "Expected";
TableCell cell2 = new TableCell();
cell2.ColumnSpan = 2;
cell2.Text = "One";
TableCell cell3 = new TableCell();
cell3.ColumnSpan = 2;
cell3.Text = "Two";
TableCell cell4 = new TableCell();
cell4.ColumnSpan = 2;
cell4.Text = "Three";
newHeaderRow.Cells.Add(cell1);
newHeaderRow.Cells.Add(cell2);
newHeaderRow.Cells.Add(cell3);
newHeaderRow.Cells.Add(cell4);
((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
}
}
protected void Btn_Click(object sender, EventArgs args)
{
}
I am using the following approach. Any improvement suggestions?
int current = 0;
int headerCount = grdTransactions.HeaderRow.Cells.Count;
for (current = 0; current < headerCount; current++)
{
TableHeaderCell cell = new TableHeaderCell();
cell.Text = grdTransactions.HeaderRow.Cells[current].Text;
originalHeaderRow.Cells.Add(cell);
}
I'm trying to add another eventHandler to RadioButton. This is the sample code (which is working):
ASP.NET:
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />
C#:
protected void Page_Load(object sender, EventArgs e)
{
RadioButton RB1 = new RadioButton();
RB1.ID = "1";
RB1.GroupName = "bla";
RB1.CheckedChanged += new EventHandler(CheckedChanged);
RadioButton RB2 = new RadioButton();
RB2.ID = "2";
RB2.GroupName = "bla";
RB2.CheckedChanged += new EventHandler(CheckedChanged);
PlaceHolder1.Controls.Add(RB1);
PlaceHolder1.Controls.Add(RB2);
}
protected void CheckedChanged(object sender, EventArgs e)
{
Label1.Text = ((RadioButton)sender).ID;
}
In my project I have dynamic creating of RadioButtons (the number of rows I get from database). The same adding eventHandler does not work, but if I write
MyRadioButton.Load += new EventHandler(Another_method);
The Another_method will start, but in
MyRadioButton.CheckedChanged += new EventHandler(Main_method);
the Main_method will not start if I choose one of the RadioButtons.
What is wrong?
#KevinP
This is my code:
Table tb1 = new Table();
PlaceHolder1.Controls.Add(tb1);
TableRow tr = new TableRow();
TableCell tc = new TableCell();
//adding the first row with title"
tr.Cells.Add(tc);
for (int i = 0; i < ((ArrayList)(result[0])).Count; i++)
{
tc = new TableCell();
Label example = new Label();
example.Text = ((columnsResultFromSqlClients)(i)).ToString();
tc.Controls.Add(example);
tr.Cells.Add(tc);
}
tb1.Rows.Add(tr);
//filling the table
for (int i = 0; i < result.Count; i++)
{
tr = new TableRow();
tc = new TableCell();
//adding radio button
RadioButton RB = new RadioButton();
RB.Attributes.Add("value", ((ArrayList)(result[i]))[0].ToString());
RB.GroupName = "for_selecting";
RB.ID = ((ArrayList)(result[i]))[0].ToString();
RB.CheckedChanged += new EventHandler(RB_CheckedChanged2);
//RB.AutoPostBack = true;
RB.Attributes.Add("AutoPostBack", "True");
tc.Controls.Add(RB);
tr.Cells.Add(tc);
//adding content
for (int j = 0; j < ((ArrayList)(result[i])).Count; j++)
{
tc = new TableCell();
Label example = new Label();
example.Text = ((ArrayList)(result[i]))[j].ToString();
tc.Controls.Add(example);
tr.Cells.Add(tc);
}
tb1.Rows.Add(tr);
}
If I use RB.AutoPostBack = true;, I have no time to press the button to submit my choice, cause the page will reload when i click the one of the Radio Buttons.
Also the RB_CheckedChanged2 code:
protected void RB_CheckedChanged2(object sender, EventArgs e)
{
RadioButton tempRB = (RadioButton)sender;
if (tempRB.Checked)
{
selected_id = tempRB.ID;
}
}
The select_id is a static int varible with standart value = "-1".
If I am not mistaken, with dynamic controls in ASP.Net, you need to "rewire" their events on a postback. Remember that on a postback, dynamic controls are no longer there. You have to recreate them.