467,894 Members | 1,432 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

raising events in a user control ???

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
Jul 20 '08 #1
  • viewed: 2601
Share:
7 Replies
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
Jul 21 '08 #2
Hi

you might also check out this blog post

http://codebetter.com/blogs/brendan....-Controls.aspx

Best of luck

-------
Munna

www.munna.shatkotha.com/blog
www.munna.shatkotha.com
www.shatkotha.com
Jul 21 '08 #3
On Jul 21, 8:10 am, Munna <munna...@gmail.comwrote:
Hi

you might also check out this blog post

http://codebetter.com/blogs/brendan....04/10/06/Easil...

Best of luck

-------
Munna

http://www.munna.shatkotha.com/blogw....shatkotha.com
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
Jul 21 '08 #4
Christian Cambier wrote:
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('ElementForShow ingMessage').innerHTML='message';"

--
Göran Andersson
_____
http://www.guffa.com
Jul 21 '08 #5
On Jul 21, 11:25 am, Göran Andersson <gu...@guffa.comwrote:
Christian Cambier wrote:
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('ElementForShow ingMessage').innerHTML='message';"

--
Göran Andersson
_____http://www.guffa.com
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
Jul 22 '08 #6
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 Cambier wrote:
On Jul 21, 11:25 am, Göran Andersson <gu...@guffa.comwrote:
>Christian Cambier wrote:
>>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('ElementForSho wingMessage').innerHTML='message';"

--
Göran Andersson
_____http://www.guffa.com

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
Jul 22 '08 #7
Christian Cambier wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jul 22 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by rawCoder | last post: by
7 posts views Thread by Colin Young | last post: by
reply views Thread by Joe Campbell | last post: by
4 posts views Thread by Dave A | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.