for (int i = 0; i < dtCategories.Rows.Count; i++)
{
objCell = new HtmlTableCell(); // Category Name
objCell.InnerHtml = string.Empty;
objRow.Cells.Add(objCell);
objCell = new HtmlTableCell(); //SubCategory Name
objCell.InnerHtml = dtCategories.Rows[i]["SubCategoryName"].ToString();
objRow.Cells.Add(objCell);
HtmlAnchor objAnchor = new HtmlAnchor();
objAnchor.ID = "a" + catId;
objAnchor.InnerHtml = "Delete";
objAnchor.Attributes.Add("onclick","delCategory(a+"+catId+")"); // Is it correct, if not how can i call a javascript method with parameter
ssjGanesh
Participant
1928 Points
1352 Posts
issue in creating HtmlAnchor dynamically in c#
Mar 16, 2012 08:36 AM|LINK
HtmlTable objTable = new HtmlTable();
objTable.Border = 1;
objTable.Width = "100%";
objTable.Attributes.Add("class", "tblCategory");
HtmlTableRow objRow;
HtmlTableCell objCell;
objRow = new HtmlTableRow();
//Creating Column Heading
objCell = new HtmlTableCell();
objCell.BgColor = "DarkGray";
objCell.Attributes.Add("class", "tdHeading");
objCell.InnerHtml = "CategoryName";
objRow.Cells.Add(objCell);
objCell = new HtmlTableCell();
objCell.BgColor = "DarkGray";
objCell.Attributes.Add("class", "tdHeading");
objCell.InnerHtml = "SubCategoryName";
objRow.Cells.Add(objCell);
objCell = new HtmlTableCell();
objCell.BgColor = "DarkGray";
objCell.Attributes.Add("class", "tdHeading");
objCell.InnerHtml = "";
objRow.Cells.Add(objCell);
objTable.Rows.Add(objRow);
for (int i = 0; i < dtCategories.Rows.Count; i++)
{
objCell = new HtmlTableCell(); // Category Name
objCell.InnerHtml = string.Empty;
objRow.Cells.Add(objCell);
objCell = new HtmlTableCell(); //SubCategory Name
objCell.InnerHtml = dtCategories.Rows[i]["SubCategoryName"].ToString();
objRow.Cells.Add(objCell);
HtmlAnchor objAnchor = new HtmlAnchor();
objAnchor.ID = "a" + catId;
objAnchor.InnerHtml = "Delete";
objAnchor.Attributes.Add("onclick","delCategory(a+"+catId+")"); // Is it correct, if not how can i call a javascript method with parameter
and how to bind the HTMLanchor into a cell
Mark as answer,if it helped U!
jigarbjpatel
Member
454 Points
88 Posts
Re: issue in creating HtmlAnchor dynamically in c#
Mar 16, 2012 09:55 AM|LINK
First create a new HTMLTableCell object inside the for loop for the 3rd column. Then add the HtmlAnchor object in it (u can use Controls property)
Secondly, you also need to create HtmlTableRow object in the begining of for loop and add all the HtmlTableCell objects in it.
And finally, the HtmlTable object needs to be added in the form object (or some parent object within the form)
ramiramilu
All-Star
95503 Points
14106 Posts
Re: issue in creating HtmlAnchor dynamically in c#
Mar 17, 2012 01:29 PM|LINK
something like this sould work -
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server" language="C#"> protected void Page_Load(object sender, EventArgs e) { // Create a new HtmlTable object. HtmlTable table1 = new HtmlTable(); // Set the table's formatting-related properties. table1.Border = 1; table1.CellPadding = 3; table1.CellSpacing = 3; table1.BorderColor = "red"; // Start adding content to the table. HtmlTableRow row; HtmlTableCell cell; for (int i = 1; i <= 5; i++) { // Create a new row and set its background color. row = new HtmlTableRow(); row.BgColor = (i % 2 == 0 ? "lightyellow" : "lightcyan"); for (int j = 1; j <= 4; j++) { // Create a cell and set its text. cell = new HtmlTableCell(); HtmlAnchor a = new HtmlAnchor(); a.InnerHtml = "Google/Bing"; a.ID = j.ToString(); a.Attributes.Add("onclick", "delCategory(+" + j.ToString() + ")"); cell.Controls.Add(a); row.Cells.Add(cell); } // Add the row to the table. table1.Rows.Add(row); } Page.Form.Controls.Add(table1); } </script> <script type="text/javascript"> function delCategory(val) { alert(val); } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Substitution Class Example</title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>JumpStart