Prevent controls from inheirting ID of parent

A

Alex

I am building a grid which I am adding my own templated columns to. One of
the columns has a checkbox and I am assiging the checkbox an ID based on the
row of the griid. When the grid gets built, the checkbox ID is changed so
that it is a combination of the parent control IDs and its id. Is there any
way to prevent this behaviour?

Thanks,

Alex Gadea
 
T

Teemu Keiski

Hi,

not really unless you play with only client-side controls (e.g have
client.side HTML which doesn't use server controls).

Certainly you could get over it by assigning the ID (per row, whuch you get
from db) to DataGrid's DataKeyField and get it from there when you get
reference to any particular CheckBox in the grid (the current DataGridItem
is Parent's Parent to the CheckBox in control hierarchy amd with
DataGridItem you get the info on which row you are on the grid).

Idea is exactly opposite to your custom naming that you don't acuyally need
to name them yourself, you can use other means. One other way would be using
just HTML checkboxes (<input type="checkbox" runat="server" />) for which
you could according to the HTML model assign also value to the Value
attribute/property. That's another place where you could place the ID
special to the row/checkbox
 
A

Alex

Actually, the big reason I wanted to override this is that we use Guids for
our IDs and we wind up having a name and id property that approaches 100
characters for each checkbox. And then you add 30 checkboxes to a page,
you've suddenly got 1k worth of Ids for no particular reason.

Alex
 
R

Robert Koritnik

If you don't need GUIDs on the client side, I suggest you to take a
different approach. On the server side you had to rebind the data to get the
checkboxes, so you already have access to GUIDs. But if you need GUIDs, you
could add them to some custom checkbox attribute:

checkbox1.Attributes.Add("rowGUID", yourguid.ToString());

Using this technique, attribute would be ignored by the client HTML parser
and you could easily access guid (without regular expression changes to
string) with Javascript or on the server.

This way you could read the guids on the client and the server side.
Checkbox IDs would stay as they are. Framework does the naming this way to
prevent different controls to share the same ID, which would result in an
error.
 
A

Alex

Actually, I just wound up rebuilding the checkbox controls using literals and
doing my own management of the postback. A little more coding, but it
eliminated my main problem which was the huge Ids that were being passed
around.

Thanks for the suggestion.

Alex
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top