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

    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
                • D Dhyanga

                  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 Offline
                  P Offline
                  PunkIsNotDead
                  wrote on last edited by
                  #22

                  have you create a class with name ResponseEventArgs.cs?? the content of that class should have

                  private String _myValue;
                  public String MyValue
                  {
                  get
                  { return _myValue; }
                  set { _myValue = value; }
                  }}

                  if you've created! then look where it is! for example, if I've created the class in a folder named "Classes", I have to reference it like Classes.ResponseEventArgs ;) Ahh and the first part is from the ResponseEventArgs.cs class! the other parts are from the ascx userControl. And in the aspx page you will see the event ResultOfMyText and when you've triggered, it would look like this

                  protected void ResultOfMyText(object sender, ResponseEventArgs d)
                  {
                  String response = d.MyValue;
                  }

                  modified on Wednesday, May 19, 2010 4:28 PM

                  D 1 Reply Last reply
                  0
                  • P PunkIsNotDead

                    have you create a class with name ResponseEventArgs.cs?? the content of that class should have

                    private String _myValue;
                    public String MyValue
                    {
                    get
                    { return _myValue; }
                    set { _myValue = value; }
                    }}

                    if you've created! then look where it is! for example, if I've created the class in a folder named "Classes", I have to reference it like Classes.ResponseEventArgs ;) Ahh and the first part is from the ResponseEventArgs.cs class! the other parts are from the ascx userControl. And in the aspx page you will see the event ResultOfMyText and when you've triggered, it would look like this

                    protected void ResultOfMyText(object sender, ResponseEventArgs d)
                    {
                    String response = d.MyValue;
                    }

                    modified on Wednesday, May 19, 2010 4:28 PM

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

                    hey i still couldn't get out of it. Let me put down what all i did. I have one button and a textbox in usercontrol1.ascx page. the code inside this page is shown as: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Globalization; using System.Text; namespace ProjectRequestTest { public delegate void ResponseEventHandler(object sender, ResponseEventArgs d); public partial class usercontrol1 : System.Web.UI.UserControl { public event ResponseEventHandler ResultOfMyText; protected void Page_Load(object sender, EventArgs e) { } [Description("Event that fires after button click of user control")] protected virtual void OnResult_Show(ResponseEventArgs re) { if (ResultOfMyText != null) ResultOfMyText(this, re); } protected void Button1_Click(object sender, EventArgs e) { ResponseEventArgs re = new ResponseEventArgs(); re.MyValue = TextBox1.Text;//You need a textbox named Txt_ValueOnResult_Show(re); OnResult_Show(re); } } } now the ResponseEventArgs.cs class has code like this: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ProjectRequestTest { public class ResponseEventArgs : EventArgs { private String _myValue; public String MyValue { get { return _myValue; } set { _myValue = value; } } } } now for usercontrol.aspx page, i have one textbox and that usercontrol from ascx page. the code behind in this page is : using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ProjectRequestTest { public partial class usercontrol : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ResultOfMyText(object sender, ResponseEventArgs d) { TextBox1.Text = d.MyValue; } } } but when i click the button, the textbox of aspx page is still not showing the value ?? What could have gone wrong ???

                    suchi

                    P 1 Reply Last reply
                    0
                    • D Dhyanga

                      hey i still couldn't get out of it. Let me put down what all i did. I have one button and a textbox in usercontrol1.ascx page. the code inside this page is shown as: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.Globalization; using System.Text; namespace ProjectRequestTest { public delegate void ResponseEventHandler(object sender, ResponseEventArgs d); public partial class usercontrol1 : System.Web.UI.UserControl { public event ResponseEventHandler ResultOfMyText; protected void Page_Load(object sender, EventArgs e) { } [Description("Event that fires after button click of user control")] protected virtual void OnResult_Show(ResponseEventArgs re) { if (ResultOfMyText != null) ResultOfMyText(this, re); } protected void Button1_Click(object sender, EventArgs e) { ResponseEventArgs re = new ResponseEventArgs(); re.MyValue = TextBox1.Text;//You need a textbox named Txt_ValueOnResult_Show(re); OnResult_Show(re); } } } now the ResponseEventArgs.cs class has code like this: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace ProjectRequestTest { public class ResponseEventArgs : EventArgs { private String _myValue; public String MyValue { get { return _myValue; } set { _myValue = value; } } } } now for usercontrol.aspx page, i have one textbox and that usercontrol from ascx page. the code behind in this page is : using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ProjectRequestTest { public partial class usercontrol : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void ResultOfMyText(object sender, ResponseEventArgs d) { TextBox1.Text = d.MyValue; } } } but when i click the button, the textbox of aspx page is still not showing the value ?? What could have gone wrong ???

                      suchi

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

                      Maybe it is showing! all the code is correct! :confused: i'm sure that if you put a breakpoint, the d.MyValue is the textbox.Text of user control. I've copied the text you have written! and it works for me! that simple! Have you added the event in the aspx page??

                      <%--'This is how i have named my user control (TestUserControl)'--%>
                      < uc1:TestUserControl ID="UsrCtr_Test" runat="server"
                      OnResultOfMyText="ResultOfUserControlTextBox" / >

                      to codebehind

                      //triggered by the event 'OnResultOfMyText="ResultOfUserControlTextBox"'
                      protected void ResultOfUserControlTextBox(object sender, ResponseEventArgs d)
                      {
                      Txt_MyTextBox.Text = d.MyValue;
                      }

                      good luck! I hope you finish with this ;P ;P

                      D 1 Reply Last reply
                      0
                      • P PunkIsNotDead

                        Maybe it is showing! all the code is correct! :confused: i'm sure that if you put a breakpoint, the d.MyValue is the textbox.Text of user control. I've copied the text you have written! and it works for me! that simple! Have you added the event in the aspx page??

                        <%--'This is how i have named my user control (TestUserControl)'--%>
                        < uc1:TestUserControl ID="UsrCtr_Test" runat="server"
                        OnResultOfMyText="ResultOfUserControlTextBox" / >

                        to codebehind

                        //triggered by the event 'OnResultOfMyText="ResultOfUserControlTextBox"'
                        protected void ResultOfUserControlTextBox(object sender, ResponseEventArgs d)
                        {
                        Txt_MyTextBox.Text = d.MyValue;
                        }

                        good luck! I hope you finish with this ;P ;P

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

                        Thank you very much..

                        suchita

                        P 1 Reply Last reply
                        0
                        • D Dhyanga

                          Thank you very much..

                          suchita

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

                          Ok no problem! that's why we are here! ;)

                          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