how to get cell selected from dynamically generated html table with links - c#

This is how im creating an table an adding link button for every cell. Im stuck in getting the selected cell value from the table
int noofquestions = 60;
int k = 1;
for (int i = 1; i <= noofquestions / 5; i++)
{
HtmlTableRow tRow = new HtmlTableRow();
//TableRow tRow = new TableRow();
tblNavigation.Rows.Add(tRow);
for (int j = 1; j <= 5; j++)
{
HtmlTableCell tCell = new HtmlTableCell();
//TableCell tCell = new TableCell();
//tCell.Text = "Row " + i + ", Cell " + j;
tRow.Cells.Add(tCell);
//tCell.InnerText = k.ToString();
LinkButton link = new LinkButton();
//HyperLink link = new HyperLink();
link.Text = k.ToString();
tCell.Controls.Add(link);
k = k + 1;
}

Related

How do I add table header dynamically in c#?

I have successfully created generating new records into table rows but want to add table header, so how to do that?
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
You do it like that:
TableHeaderRow header = new TableHeaderRow(); // Creating a header row
table.Rows.Add(header); // Add the header row to table tbl
For adding cells in the header row here is an example:
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
And this is where is goes in your code:
private void GenerateTable(int rowsCount)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
const int colsCount = 3;
TableHeaderRow header = new TableHeaderRow();
table.Rows.Add(header);
//These two lines in iteir own loop
TableHeaderCell headerTableCell1 = new TableHeaderCell();
header.Cells.Add(headerTableCell1);
for (int i = 0; i < rowsCount; i++)
{
TableRow row = new TableRow();
for (int j = 0; j < colsCount; j++)
{
TableCell cell = new TableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
Building on the suggested solution by #israel-altar, I struggled with adding the header into a <thead> tag and not into a <tbody>.
To achieve this, you have to use the TableSelection property of your TableHeaderRow:
private void GenerateTable(List<string> fieldsList)
{
Table table = new Table();
table.ID = "Table1";
Page.Form.Controls.Add(table);
TableHeaderRow header = new TableHeaderRow();
foreach (string field in fieldsList)
{
TableHeaderCell headerTableCell1 = new TableHeaderCell();
headerTableCell1.Controls.Add(new Literal() {
Text = field
});
header.Cells.Add(headerTableCell1);
}
// Set TableSection to TableHeader so that the TableHeaderRow is added within <thead> and not within <tbody>
header.TableSection = TableRowSection.TableHeader;
table.Rows.Add(header);
// loop rows ...
}

how to create onclick event for dynamically generated buttons

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

Dynamic rows on table c#

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.

GridView cell width is always 0

I have a GridView, in which the width of each column is dynamic and changes depending on the DataSet. I need to get the width of each column, but the column/cells width is always '0', presumably because I haven't set the width property.
Is there a way around this?
here is the code:
gridResult.DataSource = dtResult;
gridResult.DataBind();
for (int i = 0; i < gridResult.Rows.Count; i++)
{
for (int j = 0; j < gridResult.Rows[i].Cells.Count; j++)
{
string encoded = gridResult.Rows[i].Cells[j].Text;
gridResult.Rows[i].Cells[j].Text = Context.Server.HtmlDecode(encoded);
}
}
GridView2.ShowHeaderWhenEmpty = true;
int tblCols = gridResult.HeaderRow.Cells.Count;
Table tbl = new Table();
TableRow tr = new TableRow();
double check = gridResult.Columns[0].ItemStyle.Width.Value;
double check2 = gridResult.Rows[0].Cells[0].Width.Value;
// TableRow tr = new TableRow();
for (int j = 0; j < tblCols; j++)
{
// Add the table to the placeholder control
TableCell td = new TableCell();
td.Text = gridResult.HeaderRow.Cells[j].Text;
td.Attributes.Add("width", gridResult.HeaderRow.Cells[j].Width + "px");
td.HorizontalAlign = HorizontalAlign.Center;
tr.Cells.Add(td);
td.BorderWidth = 1;
td.BorderColor = System.Drawing.Color.Black;
}
tbl.Rows.Add(tr);
PlaceHolder1.Controls.Add(tbl);

How to get a Value from Table Iteration in C#

I am new to programming, working in asp.net.
I made a table with iteration, and here is my code:
int row2 = Convert.ToInt32(Request.QueryString["coll"]);
Session.Add("sumColl", row2);
TableRow tr2;
TableCell tc2;
TextBox tb2;
RequiredFieldValidator rfv;
for (int j = 0; j < row2; j++)
{
tr2 = new TableRow();
tc2 = new TableCell();
tc2.Controls.Add(new LiteralControl((j + 1).ToString()));
tr2.Cells.Add(tc2);
tc2 = new TableCell();
tb2 = new TextBox();
tb2.ID = "name" + (j + 1).ToString();
btn = new Button();
btn.ID = "search" + (j + 1).ToString();
btn.Text = "Search";
btn.OnClientClick = "javascript:openChild('popupA.aspx?dest=" + tb2.ID + "','win2')";
btn.CausesValidation = false;
rfv = new RequiredFieldValidator();
rfv.ID = "rfvR" + (j + 1).ToString();
rfv.ControlToValidate = tb2.ID;
rfv.Display = ValidatorDisplay.Dynamic;
rfv.ErrorMessage = "**";
tc2.Controls.Add(tb2);
tc2.Controls.Add(btn);
tc2.Controls.Add(rfv);
tr2.Cells.Add(tc2);
tblA.Rows.Add(tr2);
}
I want to get values from the database, where the input came from the textbox in the table above.
After I get the values I want to sum them.
The problem is I don't know how to get the input from iteration above.
I did something like below code.
int sumsColl = Convert.ToInt32(Session["sumColl"]);
double sum_coll = 0;
for (int j = 0; j < sumsColl; j++)
{
TextBox tb2 = new TextBox();
tb2 = this.Page.FindControl("name" + (j + 1).ToString()) as TextBox;
if (tb2 != null)
{
string sqlCol2 = "select p from somewhere where nameColl = '" + tb2.Text + "'";
DataTable dtcol2 = db.setDataTableSQL(sqlCol2);
for (int i = 0; i < j; i++)
{
Session.Add("P" + (j + 1).ToString(), dtcol2.Rows[0]["p"]);
}
sum_coll = sum_coll + Convert.ToDouble(Session["P"+(j + 1).ToString()]);
}
}
if (sum_coll < value)
{
Msg.Text = "Error . " + Msg.Text;
valid = false;
}
I know this is a mess, please help me and let me know if you need another clue.
I don't know what you are trying to do. Try DataGrid or DataGridView, ListView controls to display data. Your code seems very complicated. There are controls out there that specifically displays tabular data.

Categories