Executing code that is part of a control after it gets posted back from the form in the users browse

Discussion in 'ASP .Net Building Controls' started by Eric B, Aug 27, 2003.

  1. Eric B

    Eric B Guest

    Hello dear .NET'rs,

    I have made a simple RememberingTextBox control which looks in the session for
    a
    session variable with the same name as the id/name of the textbox.
    When it finds a value in the session it will use this as the
    RememberingTextBox's value.


    I base my class on 'System.Web.UI.WebControls.TextBox'.
    public class RememberingTextBox : System.Web.UI.WebControls.TextBox

    I override the 'AddAttributesToRender' method to write the value attribute if
    it is found in the session.
    So each textbox looks in the session for the value it should use.

    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
    string strValue = "";
    if (_remember == "true")
    {
    if (Page.Session[ID] != null) strValue = Page.Session[ID].ToString();
    }
    writer.AddAttribute("value", strValue );
    writer.AddAttribute("remember", _remember);
    base.AddAttributesToRender(writer);
    }

    That is working like i want it to work.
    So far so good..

    However.. when the form that contains the RememberingTextBox get's post back i
    am now putting the values into the session with a single function.
    That will also store normal textboxesvalues and that is not nescessary.
    The segment that stores it into the session looks like:

    IEnumerator enumKeys = currentPage.Request.Form.Keys.GetEnumerator();
    while(enumKeys.MoveNext())
    {
    if (enumKeys.Current.ToString().IndexOf("txt",0,3) >= 0 ||
    enumKeys.Current.ToString().IndexOf("rb",0,2) >= 0 )
    {
    currentPage.Session.Add (enumKeys.Current.ToString(),
    currentPage.Request.Form.GetValues( enumKeys.Current.ToString() ) [0].ToString
    () );
    }
    }


    As you see this just puts all (Providing the control name has 'rb' or 'txt' at
    the start) control values into the session.
    But i would like this process/code to be part of my control.
    So the retrieval and storing of values is done automatically and a developper
    should only have to put a control on the page to get the functionality.
    I do not want them to explicitly call a method to store the form's values in
    the session for later use.

    I want to add some code, that is part of the control, to do the storing of the
    value into the session.



    I guess basically my question is;
    Can i add code and if so how to a control that get's run serverside AFTER the
    control get's
    posted back from a browser-form?
    There must be some way because the 'Validator' controls muct work like that
    somehow.


    I hope this is clear.

    Kind Regards, Eric B. :)
     
    Eric B, Aug 27, 2003
    #1
    1. Advertising

  2. Eric B

    Eric B Guest

    That override worked FINE Ionita.
    Thanks for that tip! :)

    >> I guess basically my question is;
    >> Can i add code and if so how to a control that get's run serverside AFTER

    >the
    >> control get's
    >> posted back from a browser-form?
    >> There must be some way because the 'Validator' controls muct work like

    >that
    >> somehow.

    >
    >You can use any event that occurs after the post back data gets processed,
    >for
    >example the Load even (see "Control Execution Lifecycle in MSDN for more
    >details).
    >
    >For your task you can override the OnTextChanged() method (because you're
    >interested
    >only in data changes) like this:
    >
    >protected override void OnTextChanged(EventArgs e)
    >{
    > Page.Session[ID] = Text;
    > base.OnTextChanged (e);
    >}
     
    Eric B, Sep 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sylvia sil
    Replies:
    1
    Views:
    636
    Curt_C [MVP]
    Dec 29, 2004
  2. Stefan Mueller
    Replies:
    5
    Views:
    541
    Steven Saunderson
    Jul 10, 2006
  3. Eric B
    Replies:
    1
    Views:
    135
    Eric B
    Sep 2, 2003
  4. John Joyce

    gets gets

    John Joyce, Mar 26, 2007, in forum: Ruby
    Replies:
    2
    Views:
    373
    John Joyce
    Mar 26, 2007
  5. Replies:
    0
    Views:
    238
Loading...

Share This Page