Dynamically Created Form from Controls and Submit Click - c#

Why btnSave_Click doesn't fire? It does postback when I click it but not jump in to handler.
if (Session["mytable"] != null)
pnlContent.Controls.Add((Table)Session["mytable"]);
else
{
Table table = new Table();
foreach (var item in Items)
{
TableRow row = new TableRow();
table.Rows.Add(row);
if (item.ColumnTypeName == "String")
{
TableCell cell = new TableCell();
row.Cells.Add(cell);
cell.Controls.Add(new LiteralControl(item.ColumnName));
TextBox textbox = new TextBox();
textbox.ID = item.ColumnName;
TableCell cell2 = new TableCell();
row.Cells.Add(cell2);
cell2.Controls.Add(textbox);
}
}
LinkButton btnSave = new LinkButton();
btnSave.ID = "btnSave";
btnSave.Click += new EventHandler(btnSave_Click);
btnSave.Text = "Submit";
TableRow row1 = new TableRow();
table.Rows.Add(row1);
TableCell cell3 = new TableCell();
row1.Cells.Add(cell3);
cell3.Controls.Add(btnSave);
pnlContent.Controls.Add(table);
Session["mytable"] = table;
}
protected void btnSave_Click(object sender, EventArgs e)
{
//TADAA
}

Related

ASP C# Add Row to Table and Survive Postback

I'm looking for a simple way to create a running table of data input by a user in a submit form. The table really just needs to add a row with data that was just input, but more importantly, survive postback.
The table should have a header, then a row per 'set' of data entered the last submit.
For example:
<asp:Table ID="lastRecordTable" runat="server" ViewStateMode="Enabled"
AutoPostBack="true">
<asp:TableHeaderRow>
<asp:TableCell>A</asp:TableCell>
<asp:TableCell>B</asp:TableCell>
<asp:TableCell>C</asp:TableCell>
<asp:TableCell>D</asp:TableCell>
</asp:TableHeaderRow>
<asp:TableRow>
<asp:TableCell>1</asp:TableCell>
<asp:TableCell>2</asp:TableCell>
<asp:TableCell>3</asp:TableCell>
<asp:TableCell>4</asp:TableCell>
</asp:TableRow>
</asp:Table>
Code Behind:
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
cell1.Text = 1.ToString();
cell1.Text = 2.ToString();
cell1.Text = 3.ToString();
cell1.Text = 4.ToString();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
row.Cells.Add(cell4);
lastRecordTable.Rows.Add(row);
Any advice is appreciated.
Because you add the rows dynamically, you need to (re)create them on every page load, and that includes a PostBack. So move the adding of the row outside the IsPostBack check.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//not here
}
//but here
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableCell cell3 = new TableCell();
TableCell cell4 = new TableCell();
cell1.Text = 1.ToString();
cell1.Text = 2.ToString();
cell1.Text = 3.ToString();
cell1.Text = 4.ToString();
row.Cells.Add(cell1);
row.Cells.Add(cell2);
row.Cells.Add(cell3);
row.Cells.Add(cell4);
lastRecordTable.Rows.Add(row);
}
You don't need ViewStateMode="Enabled" and AutoPostBack="true" for this to work.
This is what I got to work:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
}
lastRecordTable();
}
Table Method:
public void lastRecordTable()
{
if (Session["lastRecord"] == null)
{
DataTable dt = new DataTable();
dt.Columns.Add("1");
dt.Columns.Add("2");
dt.Columns.Add("3");
dt.Columns.Add("4");
Session["lastRecord"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
else
{
DataTable dt = (DataTable)Session["lastRecord"];
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Modify Table:
protected void buttonSubmit_Click(object sender, EventArgs e)
{
//do stuff here
DataTable dt = Session["lastRecord"] as DataTable;
DataRow row = dt.NewRow();
row["1"] = "A";
row["2"] = "B";
row["3"] = "C";
row["4"] = "D";
dt.Rows.Add(row);
Session["lastRecord"] = dt;
lastRecordTable();
}
ASP:
<asp:GridView ID="GridView1" runat="server" ViewStateMode="Enabled"
AutoPostBack="true"></asp:GridView>
I'm very new to asp c#, so not 100% sure this is 'correct', but it works!

How to get value from a cell in table C#

I have table and each row contains a button. If I click the button, I want to retrieve the value from the first column in that particular row. This is my code, how can I do that. Thank you
foreach (var c in list)
{
TableRow row = new TableRow();
TableCell c0 = new TableCell();
c0.Text = string.Format("<img src='"+c.Movie_poster+ "'/>");
TableCell c1 = new TableCell();
c1.Text = c.Name;
TableCell c2 = new TableCell();
c2.Text = c.Date.ToStirng();
TableCell c3 = new TableCell();
c3.Text = c.desc;
TableCell c4 = new TableCell();
c4.Text = c.genre;
TableCell c5 = new TableCell();
//Add each string to cell in row
row.Cells.Add(c0);
row.Cells.Add(c1);
row.Cells.Add(c2);
row.Cells.Add(c3);
row.Cells.Add(c4);
row.Cells.Add(c5);
//Add the row to the table
Table.Rows.Add(row);
Button getname = new Button();
getname.Text = "Reserve";
getname.Click+= new EventHandler(getname_Click);
c5.Controls.Add(getname);
}
}
protected void reserve_Click(object sender,EventArgs e)
{
Button btn = (Button)sender;
TableRow row = (TableRow)btn.
string name = row.Cells[1].Text;
lbl.Text = name;
}
Before create button you should write a new attribute with Row number.
//Add the row to the table
Table.Rows.Add(row);
Button getname = new Button();
getname.Attributes.Add("idRow", idrow);
idrow += 1;
getname.Text = "Reserve";
getname.Click += new EventHandler(getname_Click);
Then in you reserve_Click you should get the row with the ID stored in button attributes
Button btn = (Button)sender;
int idRow = int.Parse(btn.Attributes["id"]);
TableRow row = t.Rows[idRow];
You can bind the value of the Column to the Button as a CommandArgument.
Button getname = new Button();
getname.Text = "Reserve";
getname.CommandName = "Reserve";
getname.CommandArgument = "Column Value";
getname.Command += new CommandEventHandler(Button1_Command);
And then in the Button Command
protected void Button1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Reserve")
{
string columnValue = e.CommandArgument.ToString();
}
}

How to set the width of a Table?

Table table = new Table ();
TableRow row1 = new TableRow();
TableRow row2 = new TableRow();
TableCell cell1_1 = new TableCell { RowSpan = 2, };
TableCell cell1_2 = new TableCell
{
Text = string.Format("<h4>{0}</h4><br />{1}<br/>",
product.ProductName, "Product No:" + product.Id),
HorizontalAlign = HorizontalAlign.Left,
};
TableCell cell1_3 = new TableCell { Text = "Unit Price<hr/>"};
TableCell cell1_4 = new TableCell { Text = "Quantity<hr/>" };
TableCell cell1_5 = new TableCell { Text = "Product Total<hr/>"};
TableCell cell1_6 = new TableCell();
TableCell cell2_1 = new TableCell();
TableCell cell2_2 = new TableCell { Text = "Rs " + product.Price};
TableCell cell2_3 = new TableCell();
TableCell cell2_4 = new TableCell { Text = "Rs " + (order.Quantity * product.Price) };
TableCell cell2_5 = new TableCell();
How can i set the width of this table? When i am trying table.Witdh its not working
Table table = new Table();
table.Width=new Unit("100%");
or
table.Width = new Unit("70px");
Where Unit belongs to System.Web.UI.WebControls
table.Width = new Unit("25%")
Works for me.

How to put controls in the footer of the gridview(not belongs to specific column)

Q:
How to put a table from two columns and one row in the footer of the gridview. the footer doesn't belong to any column .
You could generate the footer manually from codebehind in RowCreated of the GridView.
For example:
protected void GridView1_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer) {
var grid = (GridView)sender;
Label lbl1 = new Label();
Label lbl2 = new Label();
TableCell footerCell = new TableCell();
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
TableRow footerRow = new TableRow();
Table footerTable = new Table();
e.Row.Cells.Clear();
footerCell.Controls.Add(footerTable);
footerTable.Rows.Add(footerRow);
lbl1.ID = "lbl1";
lbl2.ID = "lbl2";
cell1.Controls.Add(lbl1);
cell2.Controls.Add(lbl2);
footerRow.Cells.Add(cell1);
footerRow.Cells.Add(cell2);
e.Row.Cells.Add(footerCell);
}
}
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer) {
DataRow row = getFooterSource().Rows[0];
var lbl1 = (Label)e.Row.FindControl("lbl1");
var lbl2 = (Label)e.Row.FindControl("lbl2");
lbl1.Text = row["Col1"];
lbl2.Text = row["Col2"];
e.Row.Cells[0].ColumnSpan = ((DataTable)((GridView)sender).DataSource).Columns.Count;
}
}
//get some mock-data
private DataTable getFooterSource()
{
DataTable tblFooter = new DataTable("Footer");
tblFooter.Columns.Add(new DataColumn("Col1", typeof(string)));
tblFooter.Columns.Add(new DataColumn("Col2", typeof(string)));
var footerRow = tblFooter.NewRow();
footerRow(0) = "first column's value";
footerRow(1) = "second column's value";
tblFooter.Rows.Add(footerRow);
return tblFooter;
}

Inserting DropDownList in a TableCell

while (reader.Read())
{
TableRow r = new TableRow();
TableCell c = new TableCell();
c.Controls.Add(new LiteralControl(reader["Name"].ToString()));
r.Cells.Add(c);
Table1.Rows.Add(r);
TableCell c1 = new TableCell();
c1.Controls.Add(new LiteralControl(reader["RollID"].ToString()));
r.Cells.Add(c1);
Table1.Rows.Add(r);
}
i want to add another cell with a dropdownlist for every row.Could any one sort me out with this issue?
You can do like...
DropDownList ddl = new DropDownList();
ddl.ID = "ddl";
ddl.Items.Add(new ListItem("Text", "Value")); // add list items
TableCell c2 = new TableCell();
c2.Controls.Add(ddl);
r.Cells.Add(c2);
Table1.Rows.Add(r);

Categories

Resources