Access the value of a textbox from a custom control after postback

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adiel_g@hotmail.com

    Access the value of a textbox from a custom control after postback

    Hello Everyone, I created a custom control. On the
    CreateChildCont rols, I added a textbox to the control as follows:

    // TextBox
    TextBox txtValue = new TextBox();
    txtValue.ID = "txtValue";

    protected override void CreateChildCont rols()
    {
    // Add the control to the form
    Controls.Add(tx tValue);

    }

    Now during postback, I am trying to access the value of the control:

    // Property inside custom control that is being called from the
    webform
    public bool isValidMatch()
    {
    if (txtValue.Text == "test")
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    The isValidMatch method is being called from the webform during
    postback. However the value of the textbox is blank no matter what
    the user enters in the webform. I must be missing something in the
    postback logic configuration of custom controls but cannot figure out
    what the problem is and why is the textbox value always blank.

    Thanks Before Hand,
    Adiel
  • Harv

    #2
    Re: Access the value of a textbox from a custom control afterpostback

    You might want to check into creating a custom class and use the
    IPostBackDataHa ndler interface. I use something simliar for an int
    value normally in drop downs...

    You can have a property on the control like:

    public int PostedValue
    {
    get { return postedValue; }
    }
    private int postedValue = -1;

    Your interface methods might look something like:

    bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
    System.Collecti ons.Specialized .NameValueColle ction postCollection)
    {
    int presentValue = SelectedIndex;
    postedValue = int.Parse(postC ollection[postDataKey]);

    if (!presentValue. Equals(postedVa lue))
    {
    return true;
    }
    return false;
    }

    void IPostBackDataHa ndler.RaisePost DataChangedEven t()
    {
    OnSelectedIndex Changed(EventAr gs.Empty);
    }

    I'm sure this can point you in a direction at least


    On Sep 17, 9:13 am, adie...@hotmail .com wrote:
    Hello Everyone, I created a custom control.  On the
    CreateChildCont rols, I added a textbox to the control as follows:
    >
     // TextBox
    TextBox txtValue = new TextBox();
    txtValue.ID = "txtValue";
    >
    protected override void CreateChildCont rols()
            {
                // Add the control to the form
                Controls.Add(tx tValue);
    >
            }
    >
    Now during postback, I am trying to access the value of the control:
    >
    // Property inside custom control that is being called from the
    webform
    public bool isValidMatch()
            {
                if (txtValue.Text == "test")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    >
    The isValidMatch method is being called from the webform during
    postback.  However the value of the textbox is blank no matter what
    the user enters in the webform.  I must be missing something in the
    postback logic configuration of custom controls but cannot figure out
    what the problem is and why is the textbox value always blank.
    >
    Thanks Before Hand,
    Adiel

    Comment

    • adiel_g@hotmail.com

      #3
      Re: Access the value of a textbox from a custom control afterpostback

      Thank you for your help. This is what I have done now. I have added
      the two methods you mentioned to the custom control:

      bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,

      System.Collecti ons.Specialized .NameValueColle ction postCollection)
      {
      //int presentValue = SelectedIndex;
      string postedValue = postCollection[postDataKey];

      //if (!presentValue. Equals(postedVa lue))
      //{
      // return true;
      //}
      return false;
      }


      void IPostBackDataHa ndler.RaisePost DataChangedEven t()
      {
      //OnSelectedIndex Changed(EventAr gs.Empty);
      }


      I then set a breakpoint on both methods and ran the test webform.
      When I hit submit on the test webform, none of the two methods above
      where called. It looks like I must still be missing something?

      Thanks Again,
      Adiel

      Comment

      • Harv

        #4
        Re: Access the value of a textbox from a custom control afterpostback

        Sorry meant to reply to post not just you...

        Would you post what I sent you?

        Thanks

        On Sep 17, 12:21 pm, adie...@hotmail .com wrote:
        Thank you for your help.  This is what I have done now.  I have added
        the two methods you mentioned to the custom control:
        >
                bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
        >
        System.Collecti ons.Specialized .NameValueColle ction postCollection)
                {
                    //int presentValue = SelectedIndex;
                    string postedValue = postCollection[postDataKey];
        >
                    //if (!presentValue. Equals(postedVa lue))
                    //{
                    //    return true;
                    //}
                    return false;
                }
        >
                void IPostBackDataHa ndler.RaisePost DataChangedEven t()
                {
                    //OnSelectedIndex Changed(EventAr gs.Empty);
                }
        >
        I then set a breakpoint on both methods and ran the test webform.
        When I hit submit on the test webform, none of the two methods above
        where called.  It looks like I must still be missing something?
        >
        Thanks Again,
        Adiel

        Comment

        • Harv

          #5
          Re: Access the value of a textbox from a custom control afterpostback

          Also... the comment I made about setting AutoPostBack is not
          correct... when using the TextBox and the Submit you don't want to set
          that otherwise the post back will happen when you leave the control...
          since you want to wait for the Submit event do not set AutoPostBack
          true... which should then fire the event for the text box upon submit.

          Sorry bout that.

          On Sep 17, 12:39 pm, Harv <harve...@gmail .comwrote:
          Sorry meant to reply to post not just you...
          >
          Would you post what I sent you?
          >
          Thanks
          >
          On Sep 17, 12:21 pm, adie...@hotmail .com wrote:
          >
          >
          >
          Thank you for your help.  This is what I have done now.  I have added
          the two methods you mentioned to the custom control:
          >
                  bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
          >
          System.Collecti ons.Specialized .NameValueColle ction postCollection)
                  {
                      //int presentValue = SelectedIndex;
                      string postedValue = postCollection[postDataKey];
          >
                      //if (!presentValue. Equals(postedVa lue))
                      //{
                      //    return true;
                      //}
                      return false;
                  }
          >
                  void IPostBackDataHa ndler.RaisePost DataChangedEven t()
                  {
                      //OnSelectedIndex Changed(EventAr gs.Empty);
                  }
          >
          I then set a breakpoint on both methods and ran the test webform.
          When I hit submit on the test webform, none of the two methods above
          where called.  It looks like I must still be missing something?
          >
          Thanks Again,
          Adiel- Hide quoted text -
          >
          - Show quoted text -

          Comment

          • Harv

            #6
            Re: Access the value of a textbox from a custom control afterpostback

            Not sure if he will post so I will just put basically what I said in
            case someone else has this problem.

            void IPostBackDataHa ndler.RaisePost DataChangedEven t()
            {
            //OnSelectedIndex Changed(EventAr gs.Empty);
            }

            Needs to be

            void IPostBackDataHa ndler.RaisePost DataChangedEven t()
            {
            OnTextChanged(E ventArgs.Empty) ;
            }

            And of course the TextChanged event will need to be implemented where
            ever you have the control.

            On Sep 17, 12:43 pm, Harv <harve...@gmail .comwrote:
            Also... the comment I made about setting AutoPostBack is not
            correct... when using the TextBox and the Submit you don't want to set
            that otherwise the post back will happen when you leave the control...
            since you want to wait for the Submit event do not set AutoPostBack
            true... which should then fire the event for the text box upon submit.
            >
            Sorry bout that.
            >
            On Sep 17, 12:39 pm, Harv <harve...@gmail .comwrote:
            >
            >
            >
            Sorry meant to reply to post not just you...
            >
            Would you post what I sent you?
            >
            Thanks
            >
            On Sep 17, 12:21 pm, adie...@hotmail .com wrote:
            >
            Thank you for your help.  This is what I have done now.  I have added
            the two methods you mentioned to the custom control:
            >
                    bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
            >
            System.Collecti ons.Specialized .NameValueColle ction postCollection)
                    {
                        //int presentValue = SelectedIndex;
                        string postedValue = postCollection[postDataKey];
            >
                        //if (!presentValue. Equals(postedVa lue))
                        //{
                        //    return true;
                        //}
                        return false;
                    }
            >
                    void IPostBackDataHa ndler.RaisePost DataChangedEven t()
                    {
                        //OnSelectedIndex Changed(EventAr gs.Empty);
                    }
            >
            I then set a breakpoint on both methods and ran the test webform.
            When I hit submit on the test webform, none of the two methods above
            where called.  It looks like I must still be missing something?
            >
            Thanks Again,
            Adiel- Hide quoted text -
            >
            - Show quoted text -- Hide quoted text -
            >
            - Show quoted text -

            Comment

            • adiel_g@hotmail.com

              #7
              Re: Access the value of a textbox from a custom control afterpostback

              Thanks Harv, here is an update:

              I added this to the custom control:

              void IPostBackDataHa ndler.RaisePost DataChangedEven t()
              {
              OnTextChanged(E ventArgs.Empty) ;
              }

              Then I receive an error "The name 'OnTextChanged' does not exist in
              the current context.

              Thanks,
              Adiel

              Comment

              • Harv

                #8
                Re: Access the value of a textbox from a custom control afterpostback

                Your class looks something like:

                public class SomeTextBox : System.Web.UI.W ebControls.Text Box,
                System.Web.UI.I PostBackDataHan dler
                {
                public string PostedValue
                {
                get { return postedValue; }
                }
                private string postedValue = string.Empty;

                public SomeTextBox() { }

                bool IPostBackDataHa ndler.LoadPostD ata(string postDataKey,
                System.Collecti ons.Specialized .NameValueColle ction postCollection)
                {
                string presentValue = this.Text;
                postedValue = postCollection[postDataKey];

                if (!presentValue. Equals(postedVa lue))
                {
                return true;
                }

                return false;
                }

                void IPostBackDataHa ndler.RaisePost DataChangedEven t()
                {
                OnTextChanged(E ventArgs.Empty) ;
                }
                }

                Make sure you have all your references of course if you are using the
                "using" instead of spelling out the path...

                On Sep 18, 8:02 am, adie...@hotmail .com wrote:
                Thanks Harv, here is an update:
                >
                I added this to the custom control:
                >
                        void IPostBackDataHa ndler.RaisePost DataChangedEven t()
                        {
                            OnTextChanged(E ventArgs.Empty) ;
                        }
                >
                Then I receive an error "The name 'OnTextChanged' does not exist in
                the current context.
                >
                Thanks,
                Adiel

                Comment

                Working...