Solved

ASP.NET Dynamic Table Question

Posted on 2011-09-20
4
197 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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