Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid forgets user control values???

Status
Not open for further replies.

gagz

Programmer
Nov 21, 2002
333
US
Hi

I have a datagrid that has some custom paging... when i was in the early stages of developing the search form that set the query for it, I just had all textboxes to enter the data. I have since written the majority of them as user controls, because they are used in a bunch of forms (things like country drop down lists, etc). Now when i click, for example, the 'next' button, it seems to 'forget' the values that were in the user controls, and assumes i want to query "%"... it does however, remember the values in my textboxes... any ideas what i need to do to fix this???

THanks in advance...
Marc
 
this is killing me...

someone suggested overriding loadviewstate, so i added:
Code:
Protected Overrides Sub LoadViewState(savedState As Object)
   If Not (savedState Is Nothing) Then
      MyBase.LoadViewState(savedState)
   End If
End Sub

But I actually dont' really know what it does, other than not what i want it too... you'd think something like this should just work...

any other ideas?
 
Most likely, your problem is that you're loading the controls programatically, and doing it after viewState has been loaded... i.e. in (or after) the page_load event.

To avoid this problem, load your userControls in the page_init event. They will then "remember" their values.

Just FYI, if you declare your controls on the aspx page declaratively (as opposed to programatically), then this issue will not appear. The problem is, is that this isn't always a viable alternative.

For more information on this issue, type:
page_init viewstate usercontrols
into google. There's a wealth of more in-depth information on the issue.

hope you get it going. :)
paul
penny1.gif
penny1.gif
 
what i actually have is two panels. the first is the search form... that has multiple user controls declared in the html like:
Code:
<STATES:StateList id=&quot;StateList1&quot; runat=&quot;server&quot;></STATES:StateList>

all the user control has in it is an dropdownlist that is filled via sql call to a db.

when the form is submitted, i hide the search form panel, and show the datagrid panel. As i said, the first page is fine, its when i try to go to the second page that it seems to lose the usercontrol values.

thought i would clarify, i will check the google search however...

thanks.
 
another little note... since i databind on every postback, could that be the issue? this may not come into play if i can figure out how to databind just on the first postback. I'm not sure if when I hit the next button, i actually have to rebind since it should be the same set of data, just adjusting the currentpageindex of the datagrid.
 
Yes, databinding on every load will definitely negate viewstate:

if not page.ispostback then
'databind your controls
end if

But keep that other tip in mind, too. It will save you some time down the road if you load any controls programatically.

:)
paul
penny1.gif
penny1.gif
 
but i can't bind before the first postback.
when if not page.ispostback is true, then i'm showing the panel with the search form, hence have nothing to bind yet... i think what i need to do is bind on the first postback... but i'm not sure how to keep track of the number of postbacks?

basically here is the current logic:
Code:
    If Page.IsPostBack Then
         pnSearchForm.Visible = False
         pnDataGrid.Visible = True
         BindSQL()
     Else
         pnSearchForm.Visible = True
         pnDataGrid.Visible = False
     End If

what i need to do is somehow know when its the initial postback, no?
 
Yes, that's what it sounds like... well then you could declare a hidden form variable, set it initially to 0, set it to 1 if not page.ispostback, and then in the .ispostback=true part, check to see if it is =1, and if it is, then it's not the initial postback...

Something like that should work out nicely.
penny1.gif
penny1.gif
 
you know what??? i never even thought to look at the databinding in the actual user controls... turns out i was rebinding everytime in the actual usercontrol... all i did was put that into a if not page.ispostback condition and it worked!

Thanks for all the help though!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top