raising events in a user control ???

  • Thread starter Thread starter Christian Cambier
  • Start date Start date
C

Christian Cambier

Hi,

I have a textbox in a web user control.
I then add the usercontrol to a web form. I also add a label in the
web form.

Now, when I press a key in the textbox, i want to display some text in
the label.

I tried by raising events as used in Winforms but it is not as easy as
that apparently.

Any ideas?

thank you
Chris
 
HI,

Here is what you will do...

first you got to set your textbox's autopostback property to true.

next you will add textchange event hanlder of textbox...

add a public demo event in user control... (example : public event
eventhanlder textchangeonusercontrol;)

then in textchange event hanlder fire the textchangeonusercontrol
event like

if(textchangeonusercontrol!=null)
{
textchangeonusercontrol("stringvalue",e);
}

in page... subscribe the event of the user control...

and when the event is raise... change the value of the lable from
sender argument

example ( string value = sender as string; lable.text = value;


Best of luck

-------
Munna

www.munna.shatkotha.com/blog
www.munna.shatkotha.com
www.shatkotha.com
 
Hi

you might also check out this blog post

http://codebetter.com/blogs/brendan.tompkins/archive/2004/10/06/Easil...

Best of luck

Thank you for your reply !

That works fine.

An additional question I have: is there a way to implement it without
the Autopostback of the textbox set to true?
In other words I'd like to handle it on the client, thus with
javascript as well.
so intercept the 'onkeypress' event (that I can do) but then fire the
event to the parent form (which i think has to be done with ASP.NET.)

Any idea?

thank you
Chris
 
Christian said:
Hi,

I have a textbox in a web user control.
I then add the usercontrol to a web form. I also add a label in the
web form.

Now, when I press a key in the textbox, i want to display some text in
the label.

I tried by raising events as used in Winforms but it is not as easy as
that apparently.

Any ideas?

thank you
Chris

If you want to raise a server event for every key press, the page will
be reloaded for every key that you press, which is not very practical.
For something simple like displaying some text on the page, you should
use a client event instead, i.e. Javascript.

Example:

onkeydown="document.getElementById('ElementForShowingMessage').innerHTML='message';"
 
If you want to raise a server event for every key press, the page will
be reloaded for every key that you press, which is not very practical.
For something simple like displaying some text on the page, you should
use a client event instead, i.e. Javascript.

Example:

onkeydown="document.getElementById('ElementForShowingMessage').innerHTML='message';"

Hello,

that is to set the text of a control Ok, but my situation is a little
more complicated than that ... allow me to explain what I really try
to implement.

I am creating a simple calculator to add 2 values.
For the user to enter a value, I have created a web user control
consisting of a textbox and some additional logic.

I add 2 of those webuser control on an asp.net form, one for each
value.
added as well ON THE FORM (and not in the user control) are a button
(Add) and a label to display the result
Now, i run it ... enter 2 values, press Add and the result is
displayed in the label ... easy.

But what i want now is that when I change one of the values in one of
the textboxes in the user control (during 'onkeypress' in javascript)
is that the text in the label control is cleared.
for this to happen, I can not just implement it in the onkeypress of
the textbox, since the label control is not part of the user control
you see?

so what I need, I think, is to raise some event in onkeypress (in the
webuser control) and implement the event handler in the host form but
whithout a postback to the server !

any ideas?

Chris
 
this is a poor design in a web application. raising an event on the
server requires the browser posting all form data to the server, the
server processing the page logic, and sending a new html page to be
displayed at the browser, then the browser rerendering the whole page.
you could use an ajax library to reduce the amount of rerendering the
browser does, but it is still too much overhead to process on a keystoke
basis.

this should all be done in client script. the bookstore is full of
javascript books, any of which should teach you enough to code this
simple of a task.

-- bruce (sqlwork.com)
 
Christian said:
that is to set the text of a control Ok, but my situation is a little
more complicated than that ... allow me to explain what I really try
to implement.

Why doesn't anybody ever start there, instead of asking for something
that they think that they should use? ;)
I am creating a simple calculator to add 2 values.
For the user to enter a value, I have created a web user control
consisting of a textbox and some additional logic.

I add 2 of those webuser control on an asp.net form, one for each
value.
added as well ON THE FORM (and not in the user control) are a button
(Add) and a label to display the result
Now, i run it ... enter 2 values, press Add and the result is
displayed in the label ... easy.

But what i want now is that when I change one of the values in one of
the textboxes in the user control (during 'onkeypress' in javascript)
is that the text in the label control is cleared.
for this to happen, I can not just implement it in the onkeypress of
the textbox, since the label control is not part of the user control
you see?

so what I need, I think, is to raise some event in onkeypress (in the
webuser control) and implement the event handler in the host form but
whithout a postback to the server !

any ideas?

Chris

The browser doesn't know anything at all about user controls. When the
code arrives at the browser, it's just a plain web page. So there is
nothing keeping you from accessing the element rendered by the Label
control from a client event in an element rendered by a control in the
user control.
 
Back
Top