Solved

ASP.NET Dynamic Table Question

Posted on 2011-09-20
4
198 Views
Last Modified: 2012-06-27
Hi,

I have an ASP.NET application in which I create a table dynamically.  Each row in the table has columns for 5 days with a dropdown and a text box for each day - the idea is to record the number of hours worked by employees each day.

The drop down list has values for 'Attended', 'Holiday', 'Sick' etc and has an event handler associated with it to trap client side when the selection is changed.

My problem is that the table is within a parent tab control and as such the names get decorated.  For example,

 
TableCell statusCell = new TableCell();
			ddlStatus = new DropDownList();
			ddlStatus.ID = table.ID + "_row_" + table.Rows.Count.ToString() + "_" + day + "_Status_" + lblWeekNumber.Text;
			ddlStatus.EnableViewState = false;
			ddlStatus.AutoPostBack = true;
			AddDropDownStatusItems(ddlStatus);
			ddlStatus.SelectedIndex = (timesheet == null) ? 0 : (int)timesheet.Status;
			statusCell.Controls.Add(ddlStatus);

Open in new window


This adds a new cell to the table and adds a drop down to the cell.  I then create the event handler currently as :-

 
ddlStatus.Attributes.Add("onchange", "EnableHours(ctl00$ContentPlaceHolder1$WebTab1$tmpl0$" + ddlStatus.ID
										+ ", ctl00$ContentPlaceHolder1$WebTab1$tmpl0$" + tbHours.ID
										+ ");");

Open in new window


Note that I hard-wire the 'prefix' for the drop down as if I simply add its ID this will not match up at runtime.  I don't like this however as it relies on the hardcoded bit not changing - which it has in the past.  

The event itself is declared quite simply as :-

 
function EnableHours(ddl, tbHours) 
{
var statusID = ddl.options[ddl.selectedIndex].value;

Open in new window


I had hoped to be able to use the '<%=ddl.ClientID %>' type approach but have not been able to get this to work.  Any suggestions to get around hard-wiring the prefix?
0
Comment
Question by:ChrisMDrew
  • 2
4 Comments
 
LVL 51

Expert Comment

by:Ted Bouskill
ID: 36571372
OK, why aren't you using a DataGrid?  It creates tables automatically and provides binding options to do this automatically.
0
 

Author Comment

by:ChrisMDrew
ID: 36572781
Mainly down to styling.  I found I could style the table easier when I created it myself as certain columns have check boxes, others are edit fields and I wanted some columns shaded etc.  Could have used a DataGrid but I just found it easier not to at the time and don't want to have to go back and re-engineer something that does seem to work!
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 36573881
Hi,

Try to use ClientID property instead of ID property, this will give you the complete decorated ID, so instead of

ddlStatus.Attributes.Add("onchange", "EnableHours(ctl00$ContentPlaceHolder1$WebTab1$tmpl0$" + ddlStatus.ID            
+ ", ctl00$ContentPlaceHolder1$WebTab1$tmpl0$" + tbHours.ID            
+ ");");


use

 ddlStatus.Attributes.Add("onchange", "EnableHours(" + ddlStatus.ClientID                  + ", " + tbHours.ClientID + ");");
 

Hope that helps
0
 

Author Closing Comment

by:ChrisMDrew
ID: 36576868
thanks - simple really!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question