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;
}
}
Related
Here is the part of a code which builds DB from SQL query, but I need to put extra cell for LinkButton.
for (int j = 0; j < numrows; j++)
{
TableRow row = new TableRow();
for (int i = 0; i < numcols; i++)
{
TableCell cell = new TableCell();
string textContent;
textContent = subjects.Rows[j][i].ToString();
cell.Text = textContent;
row.Cells.Add(cell);
}
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
row.Cells.Add(cell1);
table.Rows.Add(row);
}
The question is that I don't know what do to with this part of a code where I put button into cell, I couldn't find a way to do it. Is it possible to do it without GridView, because I can't use it?
TableCell cell1 = new TableCell();
LinkButton button = new LinkButton();
button.Text = "look";
button.Click += new EventHandler(submit_Click2);
cell1. = button;
Here you go, a simple example. But remember to not add the table to the page within IsPostBack
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//do not add dynamic controls like the linkbutton in the table in "IsPostBack"
}
//to generate some random data
var random = new Random();
//number of rows and cells
var rowCount = random.Next(5, 25);
var cellCount = random.Next(3, 10);
//create a new table
var table = new Table()
{
CssClass = "table table-striped"
};
//add the rows
for (int i = 0; i < rowCount; i++)
{
var row = new TableRow();
//add the cells
for (int j = 0; j < cellCount; j++)
{
//create a new cell
var cell = new TableCell();
//if the cell is not the last add content, otherwise add the linkbutton
if (j < cellCount - 1)
{
cell.Text = "CellValue " + random.Next(1000, 100000).ToString();
}
else
{
//create a new linkbutton
var lb = new LinkButton()
{
Text = "LinkButton"
};
//add the click event handler
lb.Click += new EventHandler(LinkButton_Click);
//add the linkbutton to the cell
cell.Controls.Add(lb);
}
//add the cell to the row
row.Cells.Add(cell);
}
//add the row to the table
table.Rows.Add(row);
}
//add the table to the page
PlaceHolder1.Controls.Add(table);
}
I have created a dynamic table using Asp.net with c#. Now I want to store this dynamically created table in the session, How can i do that?
I am new to Asp.Net and Programming aswell.
Thanks.
public partial class WebForm1 : System.Web.UI.Page
{
}
protected void Page_Load(object sender, EventArgs e)
{
}
public void CreateRuntime_Table()
{
int tblRows = int.Parse(txtrow.Text);
int tblCols = int.Parse(txtcol.Text);
Table tbl = new Table();
tbl.BorderWidth = 3;
tbl.BorderStyle = BorderStyle.Solid;
tbl.ID = "myTable";
for (int i = 1; i <= tblRows; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= tblCols; j++)
{
TableCell tc = new TableCell();
TextBox txtbox = new TextBox();
txtbox.Text = "Test Row:" + i + "Test Col:" + " " + j;
//Add the control to the table cell
tc.Controls.Add(txtbox);
tr.Controls.Add(tc);
}
tbl.Rows.Add(tr);
}
form1.Controls.Add(tbl);
}
protected void Unnamed_Click(object sender, EventArgs e)
{
CreateRuntime_Table();
}
To store the table in a session do:
Table tbl = new Table();
...
Session["myTable"] = tbl;
The to get the table from the session do:
Table getTableFromSession = (Table)Session["myTable"];
this is how i was generating buttons and on click event for the buttons.but every time on button click its taking the max value rather than the button clicked
int noofquestions = 100;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
myTable.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
Button link = new Button();
//LinkButton link = new LinkButton();
link.ID = "link" + k;
link.Text = k.ToString();
tCell.Controls.Add(link);
ViewState["qno"] = k;
link.Click += new EventHandler(link_Click);
tCell.Controls.Add(link);
//link.Click += new EventHandler(this.btn_click);
k = k + 1;
}
}
void link_Click(object sender, EventArgs e)
{
}
It is up to how you read value.
I think my meaning max value you are questioning that when you read ViewState["qno"] it is max value because it is what it set to that.
In stead of that just try to read from some other property.
void link_Click(object sender, EventArgs e)
{
//If you do ViewState["qno"] it will be max value of K instead do following thing.
}
// Do following update
int noofquestions = 100;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
myTable.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
tRow.Cells.Add(tCell);
Button link = new Button();
//LinkButton link = new LinkButton();
link.ID = "link" + k;
link.Text = k.ToString();
tCell.Controls.Add(link);
ViewState[link.ID] = k;
link.Click += new EventHandler(link_Click);
tCell.Controls.Add(link);
//link.Click += new EventHandler(this.btn_click);
k = k + 1;
}
}
void link_Click(object sender, EventArgs e)
{
Button b = (sender)Button;
string value = ViewState[b.ID].ToString();
}
two part question:
1:
I am using a session state refresh to hold count of the page reloads but when using a series of if statements the validation isn't working. Code at the bottom. What am I doing wrong?
2:
Using session state against postback doesn't appear to be the most efficient method of doing this, if I added another table with the same functionality, the count would affect both tables.
For example if I only wanted people to be able to add a maximum of three rows to a table, on two tables, they would only be able to add four rows in total.
What would be a better method of storing a count for separate refreshes?
Update
I have found the issue with the code not adding rows to the existing but I'm unsure of how to fix it.
Basically each iteration is deleting the existing row then adding a single row again. So there will never be more than one row.
Any ideas?
namespace FormTest
{
public partial class About : Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
tbl.BorderStyle = BorderStyle.Inset;
tbl.BorderWidth = Unit.Pixel(1);
if (!Page.IsPostBack)
{
Session["count"] = 0;
}
else
{
int count = (int)Session["count"];
count++;
Session["count"] = count;
}
}
protected void cmdCreate_Click(object sender, System.EventArgs e)
{
tbl.Controls.Clear();
int rows = 1;
int cols = 4;
if ((int)Session["count"] == 0)
{
for (int i = 0; i < rows; i++)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
for (int j = 0; j < cols; j++)
{
TableCell cellNew = new TableCell();
Label lblNew = new Label();
lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";
TextBox tbNew = new TextBox();
cellNew.Controls.Add(lblNew);
cellNew.Controls.Add(tbNew);
rowNew.Controls.Add(cellNew);
}
}
}
else
{
if ((int)Session["count"] == 1)
{
for (int i = 0; i < rows; i++)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
for (int j = 0; j < cols; j++)
{
TableCell cellNew = new TableCell();
Label lblNew = new Label();
lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";
TextBox tbNew = new TextBox();
cellNew.Controls.Add(lblNew);
cellNew.Controls.Add(tbNew);
rowNew.Controls.Add(cellNew);
}
}
}
else
{
if ((int)Session["count"] == 2)
{
for (int i = 0; i < rows; i++)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
for (int j = 0; j < cols; j++)
{
TableCell cellNew = new TableCell();
Label lblNew = new Label();
lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";
TextBox tbNew = new TextBox();
cellNew.Controls.Add(lblNew);
cellNew.Controls.Add(tbNew);
rowNew.Controls.Add(cellNew);
}
}
}
else
{
if ((int)Session["count"] == 3)
{
for (int i = 0; i < rows; i++)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
for (int j = 0; j < cols; j++)
{
TableCell cellNew = new TableCell();
Label lblNew = new Label();
lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";
TextBox tbNew = new TextBox();
cellNew.Controls.Add(lblNew);
cellNew.Controls.Add(tbNew);
rowNew.Controls.Add(cellNew);
}
}
}
else
{
Response.Redirect("http://www.google.co.uk");
}
}
}
}
}//end cmdCreate_Click
}
}
You don't need a list of if conditions, you just need to ensure the number of rows is less than your desired limit. You also should store the limit per table to avoid your count clashing if/when more tables are added.
This is how I would do it:
(note: This won't work "as-is" - I've assumed you've removed the code to assign tbl within cmdCreate_Click()and have left it as is for clarity)
namespace FormTest
{
public partial class About : Page
{
private int myFirstTableMaxRows = 3;
protected void Page_Load(object sender, System.EventArgs e)
{
tbl.BorderStyle = BorderStyle.Inset;
tbl.BorderWidth = Unit.Pixel(1);
if (!Page.IsPostBack)
{
Session["myFirstTable_count"] = "0";
}
else
{
int count = (int)Session["myFirstTable_count"];
Session["myFirstTable_count"] = ++count;
}
}
protected void cmdCreate_Click(object sender, System.EventArgs e)
{
tbl.Controls.Clear();
int cols = 4;
int currentRowCount = (int)Session["myFirstTable_count"];
if(currentRowCount <= myFirstTableMaxRows)
{
TableRow rowNew = new TableRow();
tbl.Controls.Add(rowNew);
for (int j = 0; j < cols; j++)
{
TableCell cellNew = new TableCell();
Label lblNew = new Label();
lblNew.Text = "(" + i.ToString() + "," + j.ToString() + ")<br />";
TextBox tbNew = new TextBox();
cellNew.Controls.Add(lblNew);
cellNew.Controls.Add(tbNew);
rowNew.Controls.Add(cellNew);
}
}
else
{
Response.Redirect("http://www.google.co.uk");
}
}//end cmdCreate_Click
}
Update after question edit
The existing rows will go due to this line in the code:
tbl.Controls.Clear();
The table is being cleared, then a single row is added per the logic in the cmdCreate_Click() method.
I just tried over it but i got the HTML tables in the manner of unique,i just need the tables to created one within another.
sample output: http://i.stack.imgur.com/drdXx.jpg][1]sample
In Design Page
<table id="tableContent" border="1" runat="server"></table>
protected void Page_Load(object sender, EventArgs e)
{
var s = 0;
Addtable(s);
}
public void Addtable(int j)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.InnerText = "col 1";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "col2";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
tableContent.Border =1;
if( j < 5)
{
j++;
Addtable(j);
}
}
You need to change your recursion function as below
public void Addtable(HtmlTable baseTable, int j)
{
HtmlTable innerTable = new HtmlTable();
// change to stylesheet instead. Just added as an example to get the output
baseTable.Style.Add("margin-left", "25px");
baseTable.Style.Add("margin-right", "25px");
baseTable.Style.Add("margin-bottom", "25px");
baseTable.Style.Add("text-align", "center");
baseTable.Border = 1;
//Create a container cell for inner table
HtmlTableRow container = new HtmlTableRow();
HtmlTableCell containerCell = new HtmlTableCell();
Literal l = new Literal();
l.Text = "Table " + j;
containerCell.Controls.Add(l);
containerCell.Controls.Add(innerTable);
containerCell.ColSpan = 2;
container.Cells.Add(containerCell);
baseTable.Rows.Add(container);
if (j < 5)
{
j++;
Addtable(innerTable, j);
}
}
Then call the function
var s = 0;
Addtable(tableContent,s);