I have added a hyperlink to my existing Table cells but I get the filmname twice. Also i want to add other table cells as hyperlinks like Directors, Actors and Filmyear. how do i do that?
My Table Cells below:
tc = GetTableCell(film.FilmName);
tr.Cells.Add(tc);
tc = GetTableCell(film.Directors[0].PersonName);
tr.Cells.Add(tc);
tc = GetTableCell(film.Actors[0].PersonName);
tr.Cells.Add(tc);
tc = GetTableCell(film.FilmYear);
tr.Cells.Add(tc);
My HyperLink code below:
HyperLink link = new HyperLink();
link.NavigateUrl = "https://www.imdb.com/title/tt4154756/";
link.Text = film.FilmName;
tc.Controls.Add(link);
tr.Cells.Add(tc);
Try this.
private void CreateTable()
{
Table table = new Table();
TableRow tr = new TableRow();
//film name
tr.Cells.Add(GetTableCell("Film Name"));
//director
tr.Cells.Add(GetTableCell("Director Name"));
//actor
tr.Cells.Add(GetTableCell("Actor Name"));
//film year
tr.Cells.Add(GetTableCell("Film Year"));
//film link
HyperLink link = new HyperLink();
link.NavigateUrl = "https://www.imdb.com/title/tt4154756/";
link.Text = "Film Name";
tr.Cells.Add(GetTableCell(link));
table.Rows.Add(tr);
}
//returns new table cell with text content
private TableCell GetTableCell(string text)
{
TableCell tc = new TableCell();
tc.Text = text;
return tc;
}
//returns new table cell with hyperlink
private TableCell GetTableCell(HyperLink link)
{
TableCell tc = new TableCell();
tc.Controls.Add(link);
return tc;
}
Related
I have this code in my page in my ASP.New WebForms Project:
private void populateTable(MySqlDataReader mySqlDataReader)
{
foreach (DbDataRecord rowData in mySqlDataReader)
{
TableRow tr = new TableRow();
TableCell cell2 = new TableCell();
cell2.Text = rowData.GetInt32(rowData.GetOrdinal("truck_id")).ToString();
tr.Cells.Add(cell2);
TableCell cell3 = new TableCell();
cell3.Text = rowData.GetString(rowData.GetOrdinal("registration_no")).ToString();
tr.Cells.Add(cell3);
TableCell cell4 = new TableCell();
cell4.Text = rowData.GetString(rowData.GetOrdinal("make")).ToString();
tr.Cells.Add(cell4);
TableCell cell5 = new TableCell();
cell5.Text = rowData.GetString(rowData.GetOrdinal("model")).ToString();
tr.Cells.Add(cell5);
TableCell cell1 = new TableCell();
cell1.Text = rowData.GetString(rowData.GetOrdinal("engine_no")).ToString();
tr.Cells.Add(cell1);
TableCell cell6 = new TableCell();
cell6.Text = rowData.GetString(rowData.GetOrdinal("chassis_no")).ToString();
tr.Cells.Add(cell6);
TableCell cell7 = new TableCell();
cell7.Text = rowData.GetString(rowData.GetOrdinal("driver_name")).ToString();
tr.Cells.Add(cell7);
TableCell cell8 = new TableCell();
cell8.Text = rowData.GetString(rowData.GetOrdinal("driver_contact_no")).ToString();
tr.Cells.Add(cell8);
TableCell cell9 = new TableCell();
cell9.Text = rowData.GetString(rowData.GetOrdinal("status")).ToString();
tr.Cells.Add(cell9);
tbl_TruckData.Rows.Add(tr);
}
}
How can O intergrate it to Vertical Table?
Truck No 1
Registration No AA0-1234
The current out is an Horizontal Table like this:
Truck No Registration No Make
1 AA0-1234 MAN
Other question is it possible to embbed a <asp:Label> inside a <td> element?
For example:
<td> Make: </td>
<td> <asp:Label id = "TMake" runat="server"/></td>(this)
I've tried it but there's no out put in (this)
How can i intergrate it to Vertical Table.
You can start a new row each time:
TableRow tr = new TableRow();
TableCell cell2 = new TableCell();
cell2.Text = rowData.GetInt32(rowData.GetOrdinal("truck_id")).ToString();
tr.Cells.Add(cell2);
tbl_TruckData.Rows.Add(tr);
tr = new TableRow();
TableCell cell3 = new TableCell();
cell3.Text = rowData.GetString(rowData.GetOrdinal("registration_no")).ToString();
tr.Cells.Add(cell3);
tbl_TruckData.Rows.Add(tr);
or look into more sophisticated binding controls like GridView
Is it possible to embbed a <asp:Label> inside a <td> element?
Sure - you may not be setting the Text property - either in binding or in server-side code.
this.TMake.Text = make;
Im creating a table and have added a few controls to it dynamically but the ID im assigning to these controls doesn't seem to be the same when i inspect them with firebug they seem to get a prefix so trying to do FindControls("controlname")when posting the page im not having much joy. example of the controls id: ctl00_ContentPlaceHolder1_Monday_Normal_Small but then adding that prefix to the control name not having much joy either.
Any suggestions would be much appreciated, thanks in advance.
protected void Page_Load(object sender, EventArgs e)
{
CreateMenu();
}
public void CreateMenu()
{
Table table = new Table();
table.Attributes.Add("class", "table table-bordered");
Label lbl = new Label();
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TableHeaderCell thc = new TableHeaderCell();
lbl.Text = "Day";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
thc = new TableHeaderCell();
lbl = new Label();
lbl.Text = "Meal";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
thc = new TableHeaderCell();
lbl = new Label();
lbl.Text = "Normal";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
thc = new TableHeaderCell();
lbl = new Label();
lbl.Text = "No Carb";
thc.Controls.Add(lbl);
tr.Cells.Add(thc);
table.Rows.Add(tr);
for(int i=0;i<5;i++)
{
tr = new TableRow();
tc = new TableCell();
lbl = new Label();
lbl.Text = "<h1 style='text-align:right;'>" + GetDay(i) + "</h1>";
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
tc = new TableCell();
lbl = new Label();
lbl.Text = GetMeal(i);
tc.Controls.Add(lbl);
tr.Cells.Add(tc);
for (int j = 0; j <= 1; j++)
{
tc = new TableCell();
Table dropdowntable = new Table();
TableRow tr2 = new TableRow();
TableCell tc2 = new TableCell();
for (int k = 0; k <= 2; k++)
{
DropDownList ddl = new DropDownList();
ddl.ID = GetDay(i) + "_" + GetType(j) + "_" + GetSize(k);
ddl.DataSource = numbers;
ddl.DataBind();
tc2.Controls.Add(ddl);
}
tr2.Cells.Add(tc2);
dropdowntable.Rows.Add(tr2);
tc.Controls.Add(dropdowntable);
tr.Cells.Add(tc);
}
table.Rows.Add(tr);
}
tableplaceholder.Controls.Add(table);
}
Ive changed the ClientIDMode of the ddl controls and no prefix but this doesnt seem to solve my problem, so i have a submit button at the bottom of my page and calling its click method trying this
DropDownList ddl = (DropDownList)FindControl(controlName);
try
{
int num = Convert.ToInt32(ddl.SelectedValue);
}
catch(Exception ex)
{
return 0;
}
return 0;
But no luck any ideas?
To have same Id while inspecting in Firebug you should use one of the from the below.
Set Clientmode of dropdown to Static (so that it will have same ID in client side.
ddl.ClientIDMode = ClientIDMode.Static;
2.Use ClientId property in javascript to get the exact ID of the control.
var ddlValue = document.getElementById("<% ddl.ClientID %>");
Try setting the ClientIDMode = Static, for the control your are adding as explained in MSDN
Hope this helps!
I realized the issue i was having was when trying to FindControl() i was passing the control's ID and not the controls name, this resolved my issue.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to dynamically create a table with data and buttons acting as download links. On button click, I want a method to be called with arguments relevant to that individual button object.
foreach(var item in list_files) {
TableRow tRow = new TableRow();
file_table.Rows.Add(tRow);
TableCell namecell = new TableCell();
namecell.Text = item.name;
tRow.Cells.Add(namecell);
TableCell datecell = new TableCell();
datecell.Text = item.uTC;
tRow.Cells.Add(datecell);
TableCell sizecell = new TableCell();
sizecell.Text = item.size;
tRow.Cells.Add(sizecell);
Button downloadButton = new Button();
downloadButton.Text = "Download";
// code here to call a method named GetFile with two arguments from "item"
// e.g b.onclick ( GetFile(item.a, item.b)) ;
TableCell downloadlink = new TableCell();
downloadlink.Controls.Add(downloadButton);
tRow.Cells.Add(downloadlink);
}
foreach (var item in list_files)
{
TableRow tRow = new TableRow();
file_table.Rows.Add(tRow);
TableCell namecell = new TableCell(); namecell.Text = item.name; tRow.Cells.Add(namecell);
TableCell datecell = new TableCell(); datecell.Text = item.uTC; tRow.Cells.Add(datecell);
TableCell sizecell = new TableCell(); sizecell.Text = item.size; tRow.Cells.Add(sizecell);
Button downloadButton = new Button();
downloadButton.Text = "Download";
downloadButton.Click += (sender, evnt) =>
{
//do stuff here
GetFile(item.a, item.b)
};
// code here to call a method named GetFile with two arguments from "item"
// e.g b.onclick ( GetFile(item.a, item.b)) ;
TableCell downloadlink = new TableCell(); downloadlink.Controls.Add(downloadButton); tRow.Cells.Add(downloadlink);
}
I have images displayed from a folder and with the help of the function below i am able to do just that.
private void LoadImages()
{
foreach (string strfile in Directory.GetFiles(Server.MapPath("~/Data")))
{
ImageButton imageButton = new ImageButton();
FileInfo fi = new FileInfo(strfile);
imageButton.ImageUrl = "~/Data/" + fi.Name;
imageButton.Height = Unit.Pixel(100);
imageButton.Style.Add("padding", "5px");
imageButton.Width = Unit.Pixel(100);
imageButton.Click += new ImageClickEventHandler(imageButton_Click);
Panel1.Controls.Add(imageButton);
}
}
Guys! i need to display the name of the image below the image. Help Please!
Wrap the ImageButton into a parent DIV element with the text label. A small table with two rows and 1 cell would work well also.
Example:
Table table = new Table();
TableRow row = new TableRow();
TableCell cell = new TableCell();
table.Controls.Add(row);
row.Controls.Add(cell);
cell.Controls.Add(imageButton);
row = new TableRow();
cell = new TableCell();
table.Controls.Add(row);
row.Controls.Add(cell);
cell.Text = "Image Text";
Can anyone tell me how to dynamically create thead tbody tags in my c# code?
private void MakeTable()
{
Table tb = new Table();
TableRow tr = new TableRow();
TableCell td = new TableCell();
td.Text="hello world";
tr.Cells.Add(td);
tb.Rows.Add(tr);
}
Thanks
Here a sample code that creates a THead, TBody and TFooter.
You can basically always use the TableRow object just reset the TableSection property.
Table table = new System.Web.UI.WebControls.Table();
TableRow tableRow;
TableCell tableCell;
tableRow = new TableRow();
tableRow.TableSection = TableRowSection.TableHeader;
tableCell = new TableCell();
tableCell.Text = "HEADER";
tableRow.Cells.Add(tableCell);
table.Rows.Add(tableRow);
tableRow = new TableRow();
tableRow.TableSection = TableRowSection.TableBody;
tableCell = new TableCell();
tableCell.Text = "BODY";
tableRow.Cells.Add(tableCell);
table.Rows.Add(tableRow);
tableRow = new TableRow();
tableRow.TableSection = TableRowSection.TableFooter;
tableCell = new TableCell();
tableCell.Text = "FOOTER";
tableRow.Cells.Add(tableCell);
table.Rows.Add(tableRow);
plhTest.Controls.Add(table);
Although I would suggest building the table in direct html and appending to page.
TableRow is basically tbody.
To make a thead section, use the TableHeaderRow class instead of a TableRow class.
(There is also, btw, TableFooterRow if you want to implement tfoot.
var row = new TableHeaderRow() { TableSection = TableRowSection.TableHeader };
table.Rows.Add(row);
should do the trick