Solved

ASP.NET Dynamic Table Question

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
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…

696 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