WebControl.Attributes.Add and custom attributes

P

P4trykx

Hello
I'm want to add some custom attributes to WebControls using
WebControl.Attributes.Add("abc","234");
So the html output will look like this,
<input type="hidden" abc="123" /> etc.

I know that I need to modify .dtd file and tell the browser that abc i
legal attribute.

But I have another problem, I can't change the abc attribute using
javascript, the change is invisible in code behind :-(. (in js it's
visible)

C#
someTextBox.Attrbites.Add("abc","1");

JS
document.GetElementById("someTextBox").abc = "0" ;

here goes the psotabck on server

C#
someTextBox.Attrbites["abc"] == "1"


Patryk
 
E

Eliyahu Goldin

You don't have to modify .dtd file, browser will accept the new attribute
just fine.

Asp.net doesn't take care of custom attributes automatically. You need to
pass their values by some other means, for example in hidden input elements.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
B

bruce barker

you are confusing attributes with javascript properties. some html
attributes are used to set properties. take the following html

<input Name="c1" id="c1" type="hidden" Value="value1" myattribute="foo">

when the browser parses the input node, it set properties for name, id,
type and value attributes which have the same name (but in lowercase).

this you can reference the value by:

document.getElementById('c1').value

notice you have to use "value", even though it was specified "Value". in
javascript you can also add a dynamic property:

document.getElementById('c1').Value = "myValue";

now the input has a "Value" and "value" property.

because the input is a dom object, it also has additional dom
properties. attributes are accessed with dom methods and are not
properties. to access an attribute:

document.getElementById('c1').getAttribute('myattribute');

to create one:

document.getElementById('c1').getAttribute('Value')='attvalue';

now the input has a 'Value' attribute distinct from the 'Value' property.


note: the browser only post back name/value pairs for form elements, so
any attribute changes are not seen by the server.

-- 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

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top