Extended control issue

F

Fabien Henriet

Hello,

I got a problem when I try to extend a textbox.
So I overwrite the Render method. For the stuff I try to do, I would like
to add a custom attribute to my customized textbox.
So it renders something like this:
<input ... myattribute="value" />

I modify the value of "myattribute" using javascript, but when I submit my
form, I can't get the value of "myattribute".

Do I have to create create a class extended the attribute class? How do I
have to proceed?

Thank you in advance,
Fabien.
 
H

Hal Rosser

Fabien Henriet said:
Hello,

I got a problem when I try to extend a textbox.
So I overwrite the Render method. For the stuff I try to do, I would like
to add a custom attribute to my customized textbox.
So it renders something like this:
<input ... myattribute="value" />

I modify the value of "myattribute" using javascript, but when I submit my
form, I can't get the value of "myattribute".

Do I have to create create a class extended the attribute class? How do I
have to proceed?

Thank you in advance,
Fabien.

When you submit the form, each form element with a "name" attribute and a
"value" attribute will be sent as a 'pair' or 'set' of name=value pairs. For
example <input type="hidden" name="color" value="blue" /> this element will
provide color=blue.
HTH
 
F

Fabien Henriet

Thank you for your quick answer!
I've already thought to use an hiddenfield. But I don't know how to get the
value of this field in my extended control.

I explain: when I submit my form, I call a method of my extended textbox and
within this method, I would like to test the value of my hiddenfield. I
prefer not to make that test in code behind of my page.

Thanks in advance for your help.

Fabien.
 
G

Guest

Hi,

it's simple, find the HiddenField in page (you can get the current page
instance by anyControl.Page), every control has a FindControl method (but it
searches only his childs not childs of his childs.....) so if the
hiddenField is somewhere deep, you will have use recursion to find the
control. Then you will get it's value by yourHiddenField.Value

Regards,

Lukas Holota
 
F

Fabien Henriet

Hello,

Thank you but it doesn't work. I try in debug mode. In fact, I can find
the value of my hiddenfield by using
(this.Page.Request.Form.GetValues(base.ID + "_hidden"))[0], but the value is
not the good one. The value is the orignal one (before any modification in
javascript). I can't get the value modified by the javascript.
I cannot use FindControl because at this time, the object is not yet
"re-created" (after submit but before render).

Regards and thank you again,
Fabien.
 
P

PJ on Development

Hi, Fabien,

You're almost there with the solution, but instead of using base.ID
use this.ClientID.

This way you'll make sure that the property is retrieved no matter
where the control is placed.

It's always dificult to create custom properties on the client side...

One of the most terrifying aspects of it is the lack of
__defineGetter__ and __defineSetter__ in the Internet Explorer, which
I circunvent using behaviors (and __defineGetter__ and
__defineSetter__ for Gecko) it's ugly but it works.

So if you want your property to mork like, say, "value" you'll need to
type some more code. ;)

Let me know if you have questions

Paulo Santos
http://pjondevelopment.50webs.com

Hello,

Thank you but it doesn't work.  I try in debug mode.  In fact, I can find
the value of my hiddenfield by using
(this.Page.Request.Form.GetValues(base.ID + "_hidden"))[0], but the value is
not the good one.  The value is the orignal one (before any modification in
javascript).  I can't get the value modified by the javascript.
I cannot use FindControl because at this time, the object is not yet
"re-created" (after submit but before render).

Regards and thank you again,
Fabien.

<[email protected]> a écrit dans le message de (e-mail address removed)...


   it's simple, find the HiddenField in page (you can get the current page
instance by anyControl.Page), every control has a FindControl method (but
it searches only his childs not childs of his childs.....) so if the
hiddenField is somewhere deep, you will have use recursion to find the
control. Then you will get it's value by yourHiddenField.Value

Lukas Holota

- Show quoted text -
 
F

Fabien Henriet

Hello,

Thank you for your answer, but I'm not sure to understand the difference
between base.Id and this.ClientID... Both of those strings will return the
same value... I can get back the id or name of my hiddenfield, I can't get
back the value of this item assigned client-side.

Am I wrong?

Regards,
Fabien.


"PJ on Development" <[email protected]> a écrit dans le message de
news: (e-mail address removed)...
Hi, Fabien,

You're almost there with the solution, but instead of using base.ID
use this.ClientID.

This way you'll make sure that the property is retrieved no matter
where the control is placed.

It's always dificult to create custom properties on the client side...

One of the most terrifying aspects of it is the lack of
__defineGetter__ and __defineSetter__ in the Internet Explorer, which
I circunvent using behaviors (and __defineGetter__ and
__defineSetter__ for Gecko) it's ugly but it works.

So if you want your property to mork like, say, "value" you'll need to
type some more code. ;)

Let me know if you have questions

Paulo Santos
http://pjondevelopment.50webs.com

Hello,

Thank you but it doesn't work. I try in debug mode. In fact, I can find
the value of my hiddenfield by using
(this.Page.Request.Form.GetValues(base.ID + "_hidden"))[0], but the value
is
not the good one. The value is the orignal one (before any modification in
javascript). I can't get the value modified by the javascript.
I cannot use FindControl because at this time, the object is not yet
"re-created" (after submit but before render).

Regards and thank you again,
Fabien.

<[email protected]> a écrit dans le message de (e-mail address removed)...


it's simple, find the HiddenField in page (you can get the current page
instance by anyControl.Page), every control has a FindControl method
(but
it searches only his childs not childs of his childs.....) so if the
hiddenField is somewhere deep, you will have use recursion to find the
control. Then you will get it's value by yourHiddenField.Value

Lukas Holota

- Show quoted text -
 
F

Fabien Henriet

Hi Mark,

Yes and No. I explain. I render the hiddenfield this way:
HiddenField hiddenField = new HiddenField();

hiddenField.ID = base.ID + "_hidden";

hiddenField.EnableViewState = true;

hiddenField.Value = false.ToString();

hiddenField.RenderControl(writer);

This is in my "protected override void Render(System.Web.UI.HtmlTextWriter
writer)" method of my extended textbox. How to specify "runat=server"?

During postback my value is the one I set in hiddenField.Value, not the one
javascript set.

Any idea?

Thank in advance,
Fabien.
 
F

Fabien Henriet

Hello Mark,

I try to create our own custom controls and I would like to make it the most
generic you can have. We create a lot of controls to use in web pages.
It's a kind of central project used in all our websites.
Anyway, we thought a lot over that and the best way for us was to extend
TextBox class and so on... So we took this way.

I can't imagine you cannot add some attributes to an object. If I modify
Text attribute, I can get its value, why can't I do that with another
property? I heard about custom attributes. I defined my own attribute and
try to use it in my class. I can change the value within javascript. But
do you know how to get the value server-side?

Can you give me a good tutorial with custom attributes and confirm me I can
use them to solve my problem?

Thank you in advance,
Fabien.
 
M

Mark Rae [MVP]

Anyway, we thought a lot over that and the best way for us was to extend
TextBox class and so on...
Why?

I can't imagine you cannot add some attributes to an object.

You can...
If I modify Text attribute, I can get its value, why can't I do that with
another property?

The Text attribute already exists for the TextBox control. I think the
problem is that you are modifying the TextBox control far too late in the
page lifecycle. Generally speaking, custom controls need to be created no
later than Page_Init...
Can you give me a good tutorial with custom attributes and confirm me I
can use them to solve my problem?

Create a UserControl which contains a TextBox and a HiddenField, and your
problems will disappear...
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top