Dynamically Adjust Table Cell Width - c#

I'm dynamically adding the rows and columns to a Table, but i am unable to adjust the first column's width.. i tried all possible combinations of "Unit" (new Unit(300, Unit Type.Point) ) - but no joy. It always shows the column-width to length of the data in that column, but i wanted it to be fixed. what's wrong here please.
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc = new TableCell();
tc.ID = "tcResource";
tc.Text = "Resource_Name";
Unit uWidth = new Unit(300, UnitType.Point);
tc.Width = uWidth;
tr.Cells.Add(tc);
for (i = 1; i <= 365; i++)
{
tc = new TableCell();
tc.ID = "tc" + i.ToString();
tc.Text = dtStart.AddDays(i).ToString("dd/MM") ;
dtRange[i - 1] = dtStart.AddDays(i );
tr.Cells.Add(tc);
}
tHoliday.Rows.Add(tr);

Try this
Unit width = new Unit(30, UnitType.Pixel);
TableCell cell = new TableCell();
cell.Width = width;

You should be adjusting the columns width with a CSS style, not by doing it server side. Put this in a CSS file:
td.tcResouce{ width:300px }
and add a link in your ASP.Net page:
<link rel="stylesheet" type="text/css" href="/path/to/file.css">

Related

AJAX toolkit Accordion doesn't work when published

I'm using an AJAX accordion from the AJAX control toolkit to display a dynamic list of items, which are pulled from a database. I generate the entire accordion from code behind. This works without problems when testing it locally, but when i publish it online, the accordion shows the first item but doesn't do anything else when clicked, basically not responding to anything. There are also links to other pages inside the accordionpane contents, and these do work.
For aspx i'm using a masterpage with inside it a toolkitscriptmanager:
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server EnablePartialRendering="true">
</asp:ToolkitScriptManager>
and in the content i also added a scriptmanagerproxy in the hopes that that would solve the problem, but it didn't.
the empty accordion is like this:
<asp:accordion ID="Accordion1" runat="server"
HeaderCssClass="Header" ContentCssClass="Contents"
Font-Names="Verdana" Font-Size="10"
BorderColor="#000000" BorderStyle="Solid" BorderWidth="1"
FramesPerSecond="100" FadeTransitions="true"
TransitionDuration="500">
</asp:accordion>
This is my current code to generate the accordion:
public void getRequests()
{
DataTable requests = getRequests();
if (requests.Rows.Count == 0)
{
return;
}
for (int i = 0; i < requests.Rows.Count; i++)
{
DataTable makers = getInformationByRequest();
AjaxControlToolkit.AccordionPane pane1 = new AjaxControlToolkit.AccordionPane();
pane1.ID = "pane" + i;
Table table = new Table();
table.Width = Unit.Percentage(100);
TableRow row = new TableRow();
row.CssClass = "Header";
for (int j = 0; j < 4; j++)
{
Create panel head with information
}
table.Rows.Add(row);
pane1.HeaderContainer.Controls.Add(table);
Table table1 = new Table();
table1.Width = Unit.Percentage(100);
TableRow rowhead = new TableRow();
rowhead.CssClass = "Contents";
TableCell cellName = new TableCell();
cellName.Text = "Bedrijf naam";
TableCell cellStatus = new TableCell();
cellStatus.Text = "Request status";
TableCell cellAction = new TableCell();
cellAction.Text = "Actie";
rowhead.Cells.Add(cellName);
rowhead.Cells.Add(cellStatus);
rowhead.Cells.Add(cellAction);
table1.Rows.Add(rowhead);
for (int j = 0; j < makers.Rows.Count; j++)
{
TableRow row1 = new TableRow();
if (makers.Rows.Count != 0)
{
//method to create table1
}
pane1.ContentContainer.Controls.Add(table1);
}
Accordion1.Panes.Add(pane1);
}
This works without any problems when debugging, as i've said.
Any help is greatly appreciated.
EDIT:
I solved it by using this:
http://geekswithblogs.net/lorint/archive/2007/03/28/110161.aspx
Thanks to henk mollema for putting me on the right track.

Get values from TextBox

I have two functions. In my first i have created the TextBox:
private TableRow GetGebuchteDienstleistungRow(Dienstleistungsreservierung dr, int rowIndex)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
if (dr.Dienstleistung.Mengeneinheit == Mengeneinheit.Basket)
{
foreach (Basketdienstleistung basketDl in dr.Dienstleistung.Basket)
{
cell = new TableCell();
cell.Text = "<tr />";
row.Cells.Add(cell);
TableRenderer.AddTableCell(row, dr.Von.ToString(form.DatumsFormat), 1);
TableRenderer.AddTableCell(row, dr.Von.ToString(form.ZeitFormat), 1);
TableRenderer.AddTableCell(row, dr.Bis.ToString(form.DatumsFormat), 1);
TableRenderer.AddTableCell(row, dr.Bis.ToString(form.ZeitFormat), 1);
cell = new TableCell();
var txt = new TextBox();
txt.Text = string.Format("{0:0.00}", dr.Bestellmenge * basketDl.Anzahl);
txt.Width = 42;
txt.MaxLength = 5;
txt.CssClass = "nummer";
cell.Controls.Add(txt);
row.Cells.Add(cell);
................
}
}
}
.................
How can i get the value of the created TextBox in my second function? Specifically I want to pass the value in a database.
You can use FindControl method
var textBox = (TextBox)cell.FindControl("YourID");
You can enumerate through all controls in specific cell and find your textbox by name
cell.Controls.OfType<TextBox>();

C# ASP.net Variable falls out of scope in if statment

I am having a schedule print from a database and using a loop. I have done this in asp but I am changing to c# asp.net and having troubles.
First I print the schedule headers
time|court|court|court
based on the number of courts then it prints the games.
Next ff the current records date is different the last date it will print the date over the entire table row.
Then it checks to see if the time is of the current record is the same as the last if it is not it prints the time and then the game record if it is it just prints the game record.
My problem is I am declaring the TableRow in the time if statment so when I try to use it in another statment it is out of scope. If I take the tablerow outside of the if statement it doesn't print right.
Here is what I have.
for (int i = 0; i < GameCount; i++)
{
DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
string ndate = currentdatetime.ToString("MM/dd/yyy");
string ntime = currentdatetime.ToString("HH:mm");
string nextdate = currentdatetime.ToString("MM/dd/yyy");
if (i + 1 != GameCount)
{
DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
nextdate = nextdatetime.ToString("MM/dd/yyy");
}
string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
//check to see if date is current
if (LastDate != ndate)
{
TableRow daterow = new TableRow();
TableCell datecell = new TableCell();
datecell.ColumnSpan = 7;
datecell.Controls.Add(new LiteralControl(ndate));
daterow.Cells.Add(datecell);
ScheduleTable.Rows.Add(daterow);
LastDate = ndate;
}
//print the games
if (currentdatetime != LastDateTime)
{
TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
}//check to see if next game is part of the current row
else
{
TableCell gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
}
I can also post what I currently have in asp if that would help... you can go to www.swgc.ca/volleyball/2011/schedules.asp to see what I am trying to accomplish.
Thanks
Change your last bit to:
TableRow gamerow = new TableRow();
if (currentdatetime != LastDateTime)
{
TableCell timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
}//check to see if next game is part of the current row
else
{
TableCell gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
And I'd strongly recommend looking at gridviews and repeater/list controls, as this is what they are for.
The easiest solution would be to pull your instantiation outside of the for loop. Try this (untested code):
TableRow gamerow = new TableRow();
TableCell timecell = new TableCell();
TableCell gamecell = new TableCell();
TableRow daterow = new TableRow();
TableCell datecell = new TableCell();
for (int i = 0; i < GameCount; i++)
{
DateTime currentdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i]["datetime"];
string ndate = currentdatetime.ToString("MM/dd/yyy");
string ntime = currentdatetime.ToString("HH:mm");
string nextdate = currentdatetime.ToString("MM/dd/yyy");
if (i + 1 != GameCount)
{
DateTime nextdatetime = (DateTime)Schedules.Tables["Schedule"].Rows[i + 1]["datetime"];
nextdate = nextdatetime.ToString("MM/dd/yyy");
}
string TeamA = Schedules.Tables["Schedule"].Rows[i]["teamA"].ToString();
string TeamB = Schedules.Tables["Schedule"].Rows[i]["teamB"].ToString();
//check to see if date is current
if (LastDate != ndate)
{
daterow = new TableRow();
datecell = new TableCell();
datecell.ColumnSpan = 7;
datecell.Controls.Add(new LiteralControl(ndate));
daterow.Cells.Add(datecell);
ScheduleTable.Rows.Add(daterow);
LastDate = ndate;
}
//print the games
if (currentdatetime != LastDateTime)
{
gamerow = new TableRow();
timecell = new TableCell();
timecell.Controls.Add(new LiteralControl(ntime));
gamerow.Cells.Add(timecell);
if (i + 1 != GameCount & ndate != nextdate)
{
ScheduleTable.Rows.Add(gamerow);
}
}//check to see if next game is part of the current row
else
{
gamecell = new TableCell();
gamecell.Controls.Add(new LiteralControl(TeamA + ".vs." + TeamB));
gamerow.Cells.Add(gamecell);
}
This is a non-optimized answer for your question. I feel like there is probably a better OO way to achieve your goal, but didn't want to answer a question you didn't ask.

Table generation from Code Behind

I know what my fault is, but not sure how to resolve. I am trying to generate an asp:table from Code Behind.
The table should be 3 cells wide... I'll work on the row limit later.
Here's my code:
GallaryImage g = new GallaryImage();
var images = g.GetAll();
photos.Style.Add("width","100%");
photos.Style.Add("border-style","none");
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tr.Cells.Add(tc);
tr.Cells.Add(tc);
tr.Cells.Add(tc);
int cntr = 0;
TableRow row = new TableRow();
foreach (var image in images)
{
cntr++;
TableCell cell = new TableCell();
Image i = new Image();
i.ImageUrl = image.fullThumbPath;
cell.Controls.Add(i);
row.Cells.Add(cell);
if(cntr%3==0)
{
photos.Rows.Add(row);
row.Cells.Clear();
}
}
if(row.Cells.Count > 0)
photos.Rows.Add(row);
}
My problem is that I need to create a new row in the Foreach, only when I need the new row... i.e, when we have added 3 cells.
I thought I could add the row to the table, and then clear the row to start a new row - but that's not working, as I just keep clearing the same row object... and therefore, never add multiple rows.
Can someone assist with my logic here?
GallaryImage g = new GallaryImage();
var images = g.GetAll();
photos.Style.Add("width","100%");
photos.Style.Add("border-style","none");
int cntr = 0;
TableRow row = new TableRow();
foreach (var image in images)
{
cntr++;
TableCell cell = new TableCell();
Image i = new Image();
i.ImageUrl = image.fullThumbPath;
cell.Controls.Add(i);
row.Cells.Add(cell);
if(cntr%3==0)
{
photos.Rows.Add(row);
row = new TableRow();
}
}
if(row.Cells.Count > 0)
photos.Rows.Add(row);
}

Creating controls within a Loop

I have have some code which adds new cells to a table and fills them with text boxes.
The way I've coded it so far works fine:
TableCell tCell1 = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
TableCell tCell4 = new TableCell();
TableCell tCell5 = new TableCell();
TableCell tCell6 = new TableCell();
TableCell tCell7 = new TableCell();
TextBox txt1 = new TextBox();
TextBox txt2 = new TextBox();
TextBox txt3 = new TextBox();
TextBox txt4 = new TextBox();
TextBox txt5 = new TextBox();
TextBox txt6 = new TextBox();
TextBox txt7 = new TextBox();
tCell1.Controls.Add(txt1);
tCell2.Controls.Add(txt2);
tCell3.Controls.Add(txt3);
tCell4.Controls.Add(txt4);
tCell5.Controls.Add(txt5);
tCell6.Controls.Add(txt6);
tCell7.Controls.Add(txt7);
tRow.Cells.Add(tCell1);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
tRow.Cells.Add(tCell4);
tRow.Cells.Add(tCell5);
tRow.Cells.Add(tCell6);
tRow.Cells.Add(tCell7);
As you can see there's basically 4 instructions getting repeated 7 times. I'm sure there has to be a way to accomplish this with just 4 lines of code within a FOR loop and having all the names dynamically assigned but I just can't seem to find anything that would point me in the direction of how to do it.
Something like the following is what I'm after:
for (int i = 0; i < 6; i++)
{
TableCell tCell[i] = new TableCell();
TextBox txt[i] = new TextBox();
tCell[i].Controls.Add(txt[i]);
tRow.Cells.Add(tCell[i]);
}
Any help would be much appreciated.
I think this should do it:
for (int i = 0; i < 7; i++)
{
TableCell tCell = new TableCell();
TextBox txt = new TextBox();
tCell.Controls.Add(txt);
tRow.Cells.Add(tCell);
}
Make sure that 6 is changed to a 7.
This should work fine?
for (int i = 0; i < 6; i++)
{
TableCell tCell = new TableCell();
TextBox txt = new TextBox();
tCell.Controls.Add(txt);
tRow.Cells.Add(tCell);
}
I don't really get what you need the names for though.
Do you plan on using the "txt5" name as a reference to that specific textbox?
Why not just use tRow.Cells[4].Controls[0] As TextBox ?
What you wrote actually looks pretty close to me. There a re a few points to keep in mind though.
I don't believe you need the array index. As long as tRow is initialized outside the loop it will add the new elements each time. You also may want to set the ID property of each textbox so you can access any specific on you may be looking for down the road.
Thanks for all the helpful answers. To those asking questions about what I was doing with the arrays, I wasn't! That was just an example of what I was trying to achieve.
Ian and Lars got the right idea in the fact that I will need to refer to these textboxes later so I just need to use Eugene and Lubos' solution and make sure I'm adding a line that will give them sequential ID's (txt1, txt2 etc) so that I can do this.
Thanks again for all the wonderful (and quick!) input, I'm now in love with this site!

Categories