DropDownList & Viewstate

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

    #1

    DropDownList & Viewstate

    Hi,

    I've been working on minimizing the use of the ViewState in my asp.net
    applications because of its overhead. The problem I am having is that
    I keep losing SelectedIndex in the DropDownLists. I don't lose the
    SelectedIndex when the ViewState is enabled, only when it is off.

    I have a dropdownlist named countryCbo inside of a UserControl and
    countryCbo's EnableViewState property is false. In the OnInit()
    method of the UserCountry it loads the country list from the database
    and DataBinding it.

    public void LoadCountryList () {
    // SQL Stuff ...

    this.countryCbo .DataTextField = "Name";
    this.countryCbo .DataValueField = "Code";
    this.countryCbo .DataSource = countries;
    this.countryCbo .DataBind();
    }

    // Doing this will load the countries but I lose the SelectedIndex on
    a postback.
    protected override void OnInit(EventArg s e) {
    LoadCountryList ();
    base.OnInit(e);
    }

    // Doing this will load the countries but when a postback occurs I
    lose all the countries and the list is empty.
    protected override void OnInit(EventArg s e) {
    if (!Page.IsPostBa ck) {
    LoadCountryList ();
    }
    base.OnInit(e);
    }

    I have read somewhere that the SelectedIndex resets back to -1 if a
    databind occurs. Would that be the case?
    Could someone tell me how they have gotten their dropdownlists to work
    without the ViewState being enabled?

    Thank you!

    JW
  • =?Utf-8?B?QW5nZWw=?=

    #2
    RE: DropDownList & Viewstate

    // Enable postback for the dropdown

    // in page load event

    int ndx = page.request("D ropDownList1");

    // Now do you databinding

    // then restore the selected index

    DropDownList1.S electedIndex = ndx;


    --
    aaa


    "jwnews1231@gma il.com" wrote:
    Hi,
    >
    I've been working on minimizing the use of the ViewState in my asp.net
    applications because of its overhead. The problem I am having is that
    I keep losing SelectedIndex in the DropDownLists. I don't lose the
    SelectedIndex when the ViewState is enabled, only when it is off.
    >
    I have a dropdownlist named countryCbo inside of a UserControl and
    countryCbo's EnableViewState property is false. In the OnInit()
    method of the UserCountry it loads the country list from the database
    and DataBinding it.
    >
    public void LoadCountryList () {
    // SQL Stuff ...
    >
    this.countryCbo .DataTextField = "Name";
    this.countryCbo .DataValueField = "Code";
    this.countryCbo .DataSource = countries;
    this.countryCbo .DataBind();
    }
    >
    // Doing this will load the countries but I lose the SelectedIndex on
    a postback.
    protected override void OnInit(EventArg s e) {
    LoadCountryList ();
    base.OnInit(e);
    }
    >
    // Doing this will load the countries but when a postback occurs I
    lose all the countries and the list is empty.
    protected override void OnInit(EventArg s e) {
    if (!Page.IsPostBa ck) {
    LoadCountryList ();
    }
    base.OnInit(e);
    }
    >
    I have read somewhere that the SelectedIndex resets back to -1 if a
    databind occurs. Would that be the case?
    Could someone tell me how they have gotten their dropdownlists to work
    without the ViewState being enabled?
    >
    Thank you!
    >
    JW
    >

    Comment

    • jwnews1231@gmail.com

      #3
      Re: DropDownList & Viewstate

      Thanks alot, that fixed it. I didn't think to check the Request's
      Form variable for the UniqueID of the control.

      JW

      Comment

      Working...