Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. usercontrol event

usercontrol event

Scheduled Pinned Locked Moved ASP.NET
tutorialquestion
26 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Dhyanga

    hi, I am trying to use user control event in the aspx page. I have two textboxes txt1 and txt2. txt1 is in user control(ascx) page and txt2 is in aspx page. Now with ascx page, i have one popup calender say calender1 too. Now when i click that calender1, both textboxes should show the selected date. How to do that? I can show the date in the textbox txt1 but how to do it for txt2? thanks suchita

    suchita

    N Offline
    N Offline
    Not Active
    wrote on last edited by
    #2

    If you are using JavaScript then get access to the textbox (document.getElementById(...)) and set the value. If you using a postback then you need to expose an event on the user control that the main page can subscribe to so it can receive a notification to set the text.


    I know the language. I've read a book. - _Madmatt

    D 1 Reply Last reply
    0
    • N Not Active

      If you are using JavaScript then get access to the textbox (document.getElementById(...)) and set the value. If you using a postback then you need to expose an event on the user control that the main page can subscribe to so it can receive a notification to set the text.


      I know the language. I've read a book. - _Madmatt

      D Offline
      D Offline
      Dhyanga
      wrote on last edited by
      #3

      can you give me some examples please ?? ok lets say there is a button btn1 in ascx page and textbox txtbox1 in aspx page. When i click the button btn1, txtbox1 should show "hello". so in this case, how to do it ?

      suchita

      1 Reply Last reply
      0
      • D Dhyanga

        hi, I am trying to use user control event in the aspx page. I have two textboxes txt1 and txt2. txt1 is in user control(ascx) page and txt2 is in aspx page. Now with ascx page, i have one popup calender say calender1 too. Now when i click that calender1, both textboxes should show the selected date. How to do that? I can show the date in the textbox txt1 but how to do it for txt2? thanks suchita

        suchita

        R Offline
        R Offline
        Rutvik Dave
        wrote on last edited by
        #4

        in your user control add a property like... (I assume that your user control has text box called txt1)

            private string displayText
            public string DisplayText
            {
                get { return \_displayText; }
                set { txt1.Text = \_displayText = value; }
            }
        

        now suppose your user control has name U1, then just add U1.DisplayText = "something" along with txt2.Text = "something" in your aspx page. and you are all set. :)

        D 2 Replies Last reply
        0
        • R Rutvik Dave

          in your user control add a property like... (I assume that your user control has text box called txt1)

              private string displayText
              public string DisplayText
              {
                  get { return \_displayText; }
                  set { txt1.Text = \_displayText = value; }
              }
          

          now suppose your user control has name U1, then just add U1.DisplayText = "something" along with txt2.Text = "something" in your aspx page. and you are all set. :)

          D Offline
          D Offline
          Dhyanga
          wrote on last edited by
          #5

          the above code is written in which page ?

          suchita

          1 Reply Last reply
          0
          • R Rutvik Dave

            in your user control add a property like... (I assume that your user control has text box called txt1)

                private string displayText
                public string DisplayText
                {
                    get { return \_displayText; }
                    set { txt1.Text = \_displayText = value; }
                }
            

            now suppose your user control has name U1, then just add U1.DisplayText = "something" along with txt2.Text = "something" in your aspx page. and you are all set. :)

            D Offline
            D Offline
            Dhyanga
            wrote on last edited by
            #6

            but where to keep this code? aspx or ascx.. can you clarify it please ?? also i dont know where to define _displayText ??

            suchita

            R 1 Reply Last reply
            0
            • D Dhyanga

              but where to keep this code? aspx or ascx.. can you clarify it please ?? also i dont know where to define _displayText ??

              suchita

              R Offline
              R Offline
              Rutvik Dave
              wrote on last edited by
              #7

              ok the property (code block) you need to put inside your user control ascx.cs and the setting of the property (that U1.DisplayText = "something") should be inside your aspx.cs, in other words you add a property in the user control to just pass on the value you assign to the text box inside the user control, and then when you want to set the value, you use the user control name . dispalytext to assign the value to the text box inside the user control.

              D 1 Reply Last reply
              0
              • R Rutvik Dave

                ok the property (code block) you need to put inside your user control ascx.cs and the setting of the property (that U1.DisplayText = "something") should be inside your aspx.cs, in other words you add a property in the user control to just pass on the value you assign to the text box inside the user control, and then when you want to set the value, you use the user control name . dispalytext to assign the value to the text box inside the user control.

                D Offline
                D Offline
                Dhyanga
                wrote on last edited by
                #8

                so you mean In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } } and what about in aspx page ?? and where to define _displayText ??

                suchita

                R 1 Reply Last reply
                0
                • D Dhyanga

                  so you mean In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } } and what about in aspx page ?? and where to define _displayText ??

                  suchita

                  R Offline
                  R Offline
                  Rutvik Dave
                  wrote on last edited by
                  #9

                  SayamiSuchi wrote:

                  In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } }

                  yes just after the class declaration and before any method (not inside any method).

                  SayamiSuchi wrote:

                  and what about in aspx page ??

                  here you define the value when you are assigning txt2.Text = "something"; assign your user control variable name i.e. usercontrol1.DisplayText = "something";

                  SayamiSuchi wrote:

                  and where to define _displayText ??

                  that's typing mistake remove the _ (underscore from the code)

                  D 1 Reply Last reply
                  0
                  • R Rutvik Dave

                    SayamiSuchi wrote:

                    In ascx page,you include the following code ? private string displayText ; public string DisplayText { get { return _displayText; } set { txt1.Text = _displayText = value; } }

                    yes just after the class declaration and before any method (not inside any method).

                    SayamiSuchi wrote:

                    and what about in aspx page ??

                    here you define the value when you are assigning txt2.Text = "something"; assign your user control variable name i.e. usercontrol1.DisplayText = "something";

                    SayamiSuchi wrote:

                    and where to define _displayText ??

                    that's typing mistake remove the _ (underscore from the code)

                    D Offline
                    D Offline
                    Dhyanga
                    wrote on last edited by
                    #10

                    its so confusing?? Sorry i didnt get anything... can you clearly write down in steps.. that will be more helpful ?? where to keep which part ??

                    suchita

                    modified on Monday, May 17, 2010 2:26 PM

                    M 1 Reply Last reply
                    0
                    • D Dhyanga

                      its so confusing?? Sorry i didnt get anything... can you clearly write down in steps.. that will be more helpful ?? where to keep which part ??

                      suchita

                      modified on Monday, May 17, 2010 2:26 PM

                      M Offline
                      M Offline
                      michaelschmitt
                      wrote on last edited by
                      #11

                      As i assume that you want textbox1 and textbox2 to get their values during an event in your usercontrol (ascx file), you have to follow Mark Nischalkes instructions. For example: In your ascx file, you can set the Text Property of your textbox1 control (which is inside the ascx) easily by using:

                      TextBox1.Text = "xy";
                      

                      This is pretty simple. In addition, you want to set the text of another TextBox (Textbox2)- which lives inside the aspx Page. This is more complex. You have to define an event/delegate in your .ascx file (usercontrol), which is subscribed by your page class (.aspx.cs) during page load and called when you set the textbox1.text property. This is the server-side solution. Another possibility would be to use javascript. I'm posting some example code now, which is not a 100% answer to your question, but it should help you out. I cannot modify this code more due to time restrictions: Define this delegate (maybe in a seperate file somewhere in your usercontrols namespace):

                      public delegate void OnSelectDelegate(string text);

                      In your Usercontrol:

                      public partial class MyUserControl: ....
                      {
                      public OnSelectDelegate OnSelect;

                       private void SetText(string text)
                       {
                          TextBox1.Text = text;
                          
                          if (OnSelect != null)
                             OnSelect(text);
                      
                       }
                      

                      }

                      In your aspx Page during page-load:

                      this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect);

                      Include this method in your aspx:

                      private void OnSelect(string text)
                      {
                      TextBox2.Text = text;
                      }

                      Hope i didnt include too many typing mistakes and this helps a bit. Good Luck

                      D 1 Reply Last reply
                      0
                      • M michaelschmitt

                        As i assume that you want textbox1 and textbox2 to get their values during an event in your usercontrol (ascx file), you have to follow Mark Nischalkes instructions. For example: In your ascx file, you can set the Text Property of your textbox1 control (which is inside the ascx) easily by using:

                        TextBox1.Text = "xy";
                        

                        This is pretty simple. In addition, you want to set the text of another TextBox (Textbox2)- which lives inside the aspx Page. This is more complex. You have to define an event/delegate in your .ascx file (usercontrol), which is subscribed by your page class (.aspx.cs) during page load and called when you set the textbox1.text property. This is the server-side solution. Another possibility would be to use javascript. I'm posting some example code now, which is not a 100% answer to your question, but it should help you out. I cannot modify this code more due to time restrictions: Define this delegate (maybe in a seperate file somewhere in your usercontrols namespace):

                        public delegate void OnSelectDelegate(string text);

                        In your Usercontrol:

                        public partial class MyUserControl: ....
                        {
                        public OnSelectDelegate OnSelect;

                         private void SetText(string text)
                         {
                            TextBox1.Text = text;
                            
                            if (OnSelect != null)
                               OnSelect(text);
                        
                         }
                        

                        }

                        In your aspx Page during page-load:

                        this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect);

                        Include this method in your aspx:

                        private void OnSelect(string text)
                        {
                        TextBox2.Text = text;
                        }

                        Hope i didnt include too many typing mistakes and this helps a bit. Good Luck

                        D Offline
                        D Offline
                        Dhyanga
                        wrote on last edited by
                        #12

                        hey this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect); hey here MyUserControl1 is the ascx class name or one of the control in ascx file ?

                        suchita

                        N 1 Reply Last reply
                        0
                        • D Dhyanga

                          hey this.MyUserControl1.OnSelect += new SomeNameSpace.OnSelectDelegate(OnSelect); hey here MyUserControl1 is the ascx class name or one of the control in ascx file ?

                          suchita

                          N Offline
                          N Offline
                          Not Active
                          wrote on last edited by
                          #13

                          I think at this point you should give up and hand over the project to someone else. If this confuses you, you are most certainly over your head.


                          I know the language. I've read a book. - _Madmatt

                          D 1 Reply Last reply
                          0
                          • N Not Active

                            I think at this point you should give up and hand over the project to someone else. If this confuses you, you are most certainly over your head.


                            I know the language. I've read a book. - _Madmatt

                            D Offline
                            D Offline
                            Dhyanga
                            wrote on last edited by
                            #14

                            can you give some more examples ??

                            suchita

                            P 1 Reply Last reply
                            0
                            • D Dhyanga

                              can you give some more examples ??

                              suchita

                              P Offline
                              P Offline
                              PunkIsNotDead
                              wrote on last edited by
                              #15

                              mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:

                              //this is Logo.ascx
                              public string txt;
                              private string Text
                              {
                              get { return txt; }
                              set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
                              }

                              and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control

                              //and this is Test.aspx
                              protected void Page_Load(object sender, EventArgs e)
                              {//Once you have understand the property's world ^^ you can
                              Logo1.txt = "my text";//set the property value
                              TextBox2_GetText.Text = Logo1.txt;//or get it's value
                              }

                              ;) good luck

                              N D 3 Replies Last reply
                              0
                              • P PunkIsNotDead

                                mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:

                                //this is Logo.ascx
                                public string txt;
                                private string Text
                                {
                                get { return txt; }
                                set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
                                }

                                and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control

                                //and this is Test.aspx
                                protected void Page_Load(object sender, EventArgs e)
                                {//Once you have understand the property's world ^^ you can
                                Logo1.txt = "my text";//set the property value
                                TextBox2_GetText.Text = Logo1.txt;//or get it's value
                                }

                                ;) good luck

                                N Offline
                                N Offline
                                Not Active
                                wrote on last edited by
                                #16

                                PunkIsNotDead wrote:

                                good luck

                                Good luck indeed if anyone uses this :omg: First, never expose a public variable, use proprties to expose them. Otherwise you have no way to validate and control access.

                                PunkIsNotDead wrote:

                                TextBox1.Text = value;

                                This is completely backward and unecessary. You have missed the OP's original request and intent and have added nothing but confusion to the subject.


                                I know the language. I've read a book. - _Madmatt

                                P 1 Reply Last reply
                                0
                                • P PunkIsNotDead

                                  mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:

                                  //this is Logo.ascx
                                  public string txt;
                                  private string Text
                                  {
                                  get { return txt; }
                                  set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
                                  }

                                  and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control

                                  //and this is Test.aspx
                                  protected void Page_Load(object sender, EventArgs e)
                                  {//Once you have understand the property's world ^^ you can
                                  Logo1.txt = "my text";//set the property value
                                  TextBox2_GetText.Text = Logo1.txt;//or get it's value
                                  }

                                  ;) good luck

                                  D Offline
                                  D Offline
                                  Dhyanga
                                  wrote on last edited by
                                  #17

                                  Hey, thank you for the reply. Ya I can do it in page_load. That wasn't a problem but I need this with usercontrol event. Like for example, if you have one button in usercontrol(in .ascx page) and i want all those things in that button click. Can you tell further about this please??

                                  suchita

                                  1 Reply Last reply
                                  0
                                  • P PunkIsNotDead

                                    mmm! hard to understanding? let's say that you have an ascx page name Logo.ascx. Then when you add this ascx to a page named Test.aspx, your ascx User Control is named with ID="Logo1" in the Logo.ascx.cs add this:

                                    //this is Logo.ascx
                                    public string txt;
                                    private string Text
                                    {
                                    get { return txt; }
                                    set { txt = value; TextBox1.Text = value; }//You must have a TextBox named TextBox1
                                    }

                                    and now you can call the public property of the ascx file! use this in Test.aspx or any page you've added the User Control

                                    //and this is Test.aspx
                                    protected void Page_Load(object sender, EventArgs e)
                                    {//Once you have understand the property's world ^^ you can
                                    Logo1.txt = "my text";//set the property value
                                    TextBox2_GetText.Text = Logo1.txt;//or get it's value
                                    }

                                    ;) good luck

                                    D Offline
                                    D Offline
                                    Dhyanga
                                    wrote on last edited by
                                    #18

                                    Hey, I forgot to tell one more thing. I can do this in the button click of the aspx page. But I want my button in user control in ascx page for this and in that button click, i want all those things..

                                    suchita

                                    P 1 Reply Last reply
                                    0
                                    • N Not Active

                                      PunkIsNotDead wrote:

                                      good luck

                                      Good luck indeed if anyone uses this :omg: First, never expose a public variable, use proprties to expose them. Otherwise you have no way to validate and control access.

                                      PunkIsNotDead wrote:

                                      TextBox1.Text = value;

                                      This is completely backward and unecessary. You have missed the OP's original request and intent and have added nothing but confusion to the subject.


                                      I know the language. I've read a book. - _Madmatt

                                      P Offline
                                      P Offline
                                      PunkIsNotDead
                                      wrote on last edited by
                                      #19

                                      Ok thanks mark! but that was the first thing that came to mi mind! I'll take and follow your suggestions! X|

                                      1 Reply Last reply
                                      0
                                      • D Dhyanga

                                        Hey, I forgot to tell one more thing. I can do this in the button click of the aspx page. But I want my button in user control in ascx page for this and in that button click, i want all those things..

                                        suchita

                                        P Offline
                                        P Offline
                                        PunkIsNotDead
                                        wrote on last edited by
                                        #20

                                        Ok! I know what you need! create a class (named ResponseEventArgs) that inherits the EventArgs and add a property named myValue. Like this

                                        public class ResponseEventArgs : EventArgs
                                        {
                                        private String _myValue;
                                        public String MyValue
                                        {
                                        get { return _myValue; }
                                        set { _myValue = value; }
                                        }
                                        }

                                        Then in your ascx user control, add the event and the correct delegate

                                        public delegate void ResponseEventHandler(object sender, ResponseEventArgs d);

                                        public event ResponseEventHandler ResultOfMyText;

                                        here you've added the event ResultOfMyText and the handler ResponseEventHandler. Now in the ascx user control add the event as a virtual function

                                        [Description("Event that fires after button click of user control")]
                                        protected virtual void OnResult_Show(ResponseEventArgs re)
                                        {
                                        if (ResultOfMyText != null)
                                        ResultOfMyText(this, re);

                                        }

                                        Now in button Click event of the user control, add the correct value to MyValue Property

                                        //here is button_Click event of the user control
                                        ResponseEventArgs re = new ResponseEventArgs();
                                        re.MyValue = Txt_Value.Text;//You need a textbox named Txt_Value
                                        OnResult_Show(re);

                                        then when you've added this user control, you'll see that it has a event named ResultOfMyText. Trigger that event, and you'll see that you have a d.MyValue in ResultEventArgs and will execute that event every time that you click on the button inside the user control. I hope this won't be confused ;)

                                        D 1 Reply Last reply
                                        0
                                        • P PunkIsNotDead

                                          Ok! I know what you need! create a class (named ResponseEventArgs) that inherits the EventArgs and add a property named myValue. Like this

                                          public class ResponseEventArgs : EventArgs
                                          {
                                          private String _myValue;
                                          public String MyValue
                                          {
                                          get { return _myValue; }
                                          set { _myValue = value; }
                                          }
                                          }

                                          Then in your ascx user control, add the event and the correct delegate

                                          public delegate void ResponseEventHandler(object sender, ResponseEventArgs d);

                                          public event ResponseEventHandler ResultOfMyText;

                                          here you've added the event ResultOfMyText and the handler ResponseEventHandler. Now in the ascx user control add the event as a virtual function

                                          [Description("Event that fires after button click of user control")]
                                          protected virtual void OnResult_Show(ResponseEventArgs re)
                                          {
                                          if (ResultOfMyText != null)
                                          ResultOfMyText(this, re);

                                          }

                                          Now in button Click event of the user control, add the correct value to MyValue Property

                                          //here is button_Click event of the user control
                                          ResponseEventArgs re = new ResponseEventArgs();
                                          re.MyValue = Txt_Value.Text;//You need a textbox named Txt_Value
                                          OnResult_Show(re);

                                          then when you've added this user control, you'll see that it has a event named ResultOfMyText. Trigger that event, and you'll see that you have a d.MyValue in ResultEventArgs and will execute that event every time that you click on the button inside the user control. I hope this won't be confused ;)

                                          D Offline
                                          D Offline
                                          Dhyanga
                                          wrote on last edited by
                                          #21

                                          so just the first part is in aspx right ?? public class ResponseEventArgs : EventArgs{ private String _myValue; public String MyValue { get { return _myValue; } set { _myValue = value; } }}

                                          suchita

                                          P 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups