why property will change back to default?

Discussion in 'ASP .Net Building Controls' started by matt, Sep 15, 2003.

  1. matt

    matt Guest

    I build a control,there has a property name JsDirectory
    it's default value is "/g_Scripts/"
    when using this control in a web form,I change the
    JsDirectory in the Property designer
    to "../g_scripts/",and then it runs well at once,but when
    I postback,and try to using this property inside a button
    click event,I found the prpoerty value is back to the
    default one!!!
    is this one bug of framework 1.0.3705?
    but values it will keep and runs well every times,any
    suggestion? many thanks....
     
    matt, Sep 15, 2003
    #1
    1. Advertisements

  2. matt

    webgenie Guest

    Hi matt.

    i think your control works correctly.
    because post back process reload your web form page.

    any elements of html form has a default values when the page is reloaded.
    post back is same.

    any web form controls in .net base class library, use a ViewState object to keep there values.
    so your control have to use ViewState object too.

    sample code is here:

    public string JsDirectory {
    get {
    if (Page.EnableViewState) {
    if ((Page.ViewState["jsdir"] != null) && (Page.ViewState["jsdir"] != string.Empty))
    return Page.ViewState["jsdir"];
    else
    return m_strJsDir;
    }
    else
    return m_strJsDir;
    }//end of get

    set {
    if (Page.EnableViewState) {
    if (Page.ViewState["jsdir"] != null)
    Page.ViewState["jsdir"] = value;
    else
    Page.ViewState.Add("jsdir", value);
    m_strJsDir = value;
    }
    else
    m_strJsDir = value;
    }
    }//end of property JsDirectory

    private void Class1() {
    if (Page.EnableViewState) {
    if ((Page.ViewState["jsdir"] != null) && (Page.ViewState["jsdir"] != string.Empty))
    m_strJsDir = Page.ViewState["jsdir"];
    else
    m_strJsDir = "/g_Scripts/";
    }
    else
    m_strJsDir = "/g_Scripts/";
    }//end of constructor


    - webgenie
    msn messenger :
     
    webgenie, Sep 17, 2003
    #2
    1. Advertisements

  3. matt

    matt alex Guest

    maybe you misunderstand what I mean...
    If I use code like the one you describe above,it works correctly,but If
    I use the Property Window to change the property JsDirectory,and run
    it,it will work when not postback,but error when postback!!!
    I don't know how to explain,some kinds of characters in the property
    Window won't keep it's value!!!
     
    matt alex, Sep 17, 2003
    #3
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.