?
Solved

ASP.NET Dynamic Table Question

Posted on 2011-09-20
4
Medium Priority
?
203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 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