raising events in a user control ???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Cambier

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

    #2
    Re: raising events in a user control ???

    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 textchangeonuse rcontrol;)

    then in textchange event hanlder fire the textchangeonuse rcontrol
    event like

    if(textchangeon usercontrol!=nu ll)
    {
    textchangeonuse rcontrol("strin gvalue",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




    Comment

    • Munna

      #3
      Re: raising events in a user control ???

      Hi

      you might also check out this blog post



      Best of luck

      -------
      Munna




      Comment

      • Christian Cambier

        #4
        Re: raising events in a user control ???

        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
        >
        https://www.munna.shatkotha.com/blog....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

        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: raising events in a user control ???

          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="docu ment.getElement ById('ElementFo rShowingMessage ').innerHTML='m essage';"

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          • Christian Cambier

            #6
            Re: raising events in a user control ???

            On Jul 21, 11:25 am, Göran Andersson <gu...@guffa.co mwrote:
            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="docu ment.getElement ById('ElementFo rShowingMessage ').innerHTML='m essage';"
            >
            --
            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

            Comment

            • bruce barker

              #7
              Re: raising events in a user control ???

              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.co mwrote:
              >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="doc ument.getElemen tById('ElementF orShowingMessag e').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

              Comment

              • =?ISO-8859-1?Q?G=F6ran_Andersson?=

                #8
                Re: raising events in a user control ???

                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
                _____
                Göran Anderssons privata hemsida.

                Comment

                Working...