Why doesn't it add a new TableHeaderCell? - c#

So I would like to add an a new TableHeaderCell. Because I don't have the time to learn vb, I only used a converter, so I don't know if the fault happened there. This is my code so far. It does execute this section but it won't add the header to the ASP.NET GridView. I'm using asp:BoundField.
if (grdvProductChurn != null)
{
GridViewRow row = new GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell left = new TableHeaderCell();
left.ColumnSpan = 2;
row.Cells.Add(left);
TableCell totals = new TableHeaderCell();
totals.ColumnSpan = grdvProductChurn.Columns.Count - 2;
totals.Text = "Anzahl";
row.Cells.Add(totals);
}

Related

How do i add hyperlinks to my existing table cells

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

Asp.net Web Forms Vertical Table

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;

retrieve values from dynamically created dropdownlist

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.

Adding Image Caption

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";

ASP.NET and C# fill a table with data from database

I've created a table with some C# and asp.net for be filled by data from a database, but when I add data to the table, the data is going to the same row. I want to put data in multiple rows.
Some code I have used is:
int tmp = 0;
TableRow tabelaLinhas = new TableRow();
while (tmp < dados.Rows.Count)
{
TableCell celula = new TableCell();
celula.Controls.Add(new LiteralControl(dados.Rows[tmp]["nome"].ToString()));
tabelaLinhas.Cells.Add(celula);
tabela.Rows.Add(tabelaLinhas);
CheckBox caixa = new CheckBox();
caixa.ID = dados.Rows[tmp]["nome"].ToString().Replace(" ", "").ToLower() + "CheckBox";
celula = new TableCell();
celula.Controls.Add((Control)caixa);
tabelaLinhas.Cells.Add(celula);
tabela.Rows.Add(tabelaLinhas);
tmp++;
}
Move
TableRow tabelaLinhas = new TableRow();
into the while loop; you are only creating one row object, even though you are adding multiple times. New instances need created in every while iteration.
HTH.
write
tabelaLinhas = new TableRow();
under
tabela.Rows.Add(tabelaLinhas);

Categories