Solved

ASP.NET Dynamic Table Question

Posted on 2011-09-20
4
195 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:tedbilly
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks - simple really!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now