Getting the server-adjusted ID of a repeated user control


A

Alec MacLean

Hi,

Outline of problem:

I've built a set of user controls that are used to output questions for a
survey and gather the responses using simple radio buttons.

I'm adding an optional textbox to the question control to allow individual
typed responses per question. I've got a maximum length to enforce which
matches the underlying data table design.

When asp.Net outputs a textbox set to multiline mode, the server actually
outputs a textarea tag. Fine, but I've found that the MaxLength property
seems to get ignored. I have previously got around this by providing a
javascript function to update a display of the characters remaining and
adding an Attribute to the specific textbox when the page loads that fires
the javascript function on the "onkeyup" event.

The javascript is straight-forward enough and the function allows a pair of
object names to be passed in, which correspond to the text entry textbox and
the chars remaining display textbox.

The issue I am having a problem with is that due to the question control
being repeated multiple times down the page, the server also adds a custom
prefix to each element ID that extends the original ID, so when I need to
add the attribute, I also need to know the adjusted ID of the specific
instance of the textbox.

Example output steps:

1. Out put header label values
2. Start outer loop to output question block header
3. Step 2 makes call to a second loop that outputs questions for the block

For example, the original ID of "txtResText" might become
"ctl109_txtResText", with the "ctl109_" bit varying according to how many
instances of the textbox there are. This is complicated a little as the
question control is not the only control being added to the page. There are
a set number of "static" controls that are output from the database. This
is important as it makes the starting number of the prefix higher than a
simple zero-indexed count of the question control on its own.

Say I have three "normal" labels populated before the repeated controls are
reached. These controls ID's remain unaffected, but they make the prefix of
the repeated control ID's start at "ctl104_".


My proposed solution:

I could use this knowledge to create a counter (with an offset to accomodate
the non-looped controls) that increments along with the output loops of the
various user controls, but this seems like a "hammer to crack a nut"
approach. Also, If I ever wanted to add further output prior to the loops
starting, I would have to remember to adjust the offset of the counter.


The real question:

Is there a more elegant way of achieving this? For example, is there a way
of getting the VS2005 IDE to tell me what the ID prefix would be?


Thanks for any help.

Al
 
Ad

Advertisements

B

bruce barker \(sqlwork.com\)

skip the id altogether.

ctl.AddAttribute('onkeyup',string.Format('textareamax(this,{0});',maxlength));

in client

function textareamax(e,m) {
if (e.value.length > m) e.value = e.value.substr(0,m);
}

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top