Reputation: 153
I am new to asp.net c# so please anyone can help me with below code:
var td1 = new HtmlGenericControl("td");
How can I make this td colspan to anyvalue?
Even how can I add id or class value?
Upvotes: 2
Views: 4331
Reputation: 3562
To add a td
control to the page, have markup(.aspx) that could look like this
<table><tr id="row" runat="server"></tr></table>
Then to add td1
to the page have this in your code behind:
row.Controls.add(td1);
Then to set the colspan
attribute either use the method by dknaack:
td1.Attributes.Add("colspan", 1);
or you could do it in markup as:
<td colspan="<%= ValueOfColSpanProperty %>">TableCell</td>
To set the class
td1.Attributes.Add("class", "tablecell");
To set the id set the ID
property:
td1.ID = "id1";
To control how the ID gets rendered on the client use the ClientIDMode
property if avaliable:
td1.ClientIDMode = System.Web.UI.ClientIDMode.AutoID;
Upvotes: 1
Reputation: 3224
HtmlGenericControl is generally using for div. You should use HtmlTableCell.
HtmlTableCell td = new HtmlTableCell();
td.ColSpan = 2;
Upvotes: -2
Reputation: 60486
You need to add it to the HtmlGenericControl.Attributes
collection
HtmlControl.Attributes Property Gets a collection of all attribute name and value pairs expressed on a server control tag within the ASP.NET page.
var td1 = new HtmlGenericControl("td");
td1.Attributes.Add("colspan", "1"); // replace "1" with the correct value.
Upvotes: 5