p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Professional (http://p2p.wrox.com/forumdisplay.php?f=57)
-   -   Dynamic List Box Post Back Problem (http://p2p.wrox.com/showthread.php?t=39174)

rstelma February 28th, 2006 07:04 PM

Dynamic List Box Post Back Problem
 
Hi All,

 I have a page with three list boxes that are linked with Handles
 XXX.SelectedIndexChanged

 The boxes run very smoothly and quickly at first. But after I submit the query with the values from the list boxes with a button click event and the data is bound to the data grid the list boxes take forever to post back with the new values if I want to change their values.

 I've been thinking of trying a work around to actually post to another page for the results of the query but can you think of any reason why this is happening? Here is the code for the first list box. The other two are pretty much duplicates of this one.

Code:

      Sub getAreaID(ByVal FacID As Integer, ByVal RuleID As Integer)
          Dim SQL As String = "SELECT Areas.AreaID, Areas.AreaName FROM Areas
 INNER JOIN Facilities ON Areas.FacID = Facilities.FacID WHERE
 Facilities.FacID = " & FacID & "  ORDER BY Areas.AreaName ASC;"
          Dim oDA As New SqlDataAdapter(SQL, _oConn)
          Dim oDS As New DataSet
          oDA.Fill(oDS)
          ddlAreaID.DataSource = oDS
          ddlAreaID.DataTextField = "AreaName"
          ddlAreaID.DataValueField = "AreaID"
          ddlAreaID.DataBind()
          ddlAreaID.Items.Insert(0, New ListItem("All", 0))
          ddlSubID.Items.Insert(0, New ListItem("All", 0))
          ddlLocID.Items.Insert(0, New ListItem("All", 0))
          btnAdd.Visible = False
          grdInventory.Visible = False
      End Sub
 
      Private Sub ddlAreaID_SelectedIndexChanged(ByVal sender As Object, ByVal
 e As EventArgs) Handles ddlAreaID.SelectedIndexChanged
          If ddlAreaID.SelectedValue = 0 Then
              btnAdd.Visible = False
          End If
          ddlSubID.Items.Clear()
          ddlLocID.Items.Clear()
          getSubID(ddlAreaID.SelectedValue, _RuleID)
          grdInventory.Visible = False
      End Sub



Thanks,
Richard


rstelma March 1st, 2006 03:28 PM

Added this to the page and it seems to have resolved the problem. Now the page posts back to refill the list boxes immediately.

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        Dim dgi As DataGridItem
        For Each dgi In grdInventory.Items
            dgi.EnableViewState = False
        Next
    End Sub




Imar March 1st, 2006 03:33 PM

Hi Richard,

Wouldn't it be a lot better to disable ViewState on the GridView altogether?

In ASP.NET 1.x, that also meant you lost state for important events (sorting, paging etc), but in .NET 2 this is now stored in control state, separate from ViewSate.

So, I think you're better off setting EnableViewState to False in the control declaration...

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: We've Got A Bigger Problem Now by Dead Kennedys (Track 20 from the album: Plastic Surgery Disasters + In God We Trust) What's This?

rstelma March 1st, 2006 04:33 PM

So you're saying, in ASP.NET 1.x, to do it as below?

<asp:DataGrid ID="grdInventory" AutoGenerateColumns="False" CssClass="inventory" EnableViewState="False" Runat="server">


rstelma March 1st, 2006 04:38 PM

OK. I added EnableViewState="False" and its also working great, posting back immediately. Sweet.

Thanks,
Richard


Imar March 1st, 2006 05:21 PM

Oops. sorry. for some reason I thought you were using .NET 2. I saw grdInventory and thought that was a GridView, but I think it's a DataGrid?

ViewState is used to store information about controls. What this information is, depends on the control. For a DataGrid, you'll see a lot of ViewState being added to the page (look in the HTML source for the __ViewState field). This is because the data for the grid (not the DataSource, but the actual data) is stored in ViewState. That's why you can page or sort without the need to rebind on a post back.

By disabling ViewState on a control, you tell it not to keep its state in ViewState. This is good for performance, because not the entire data is stored in the hidden field. However, you also loose a few benefits:

1. You need to rebind the data on every post back. That is, get the original data from the source or from the cache.

2. The control also looses state about the PageIndex sorting properties.
That means you can't page with a DataGrid without ViewState, unless you deploy your own paging mechanism.

If those aren't problems for you, turning off ViewState is a good thing, as it improves page performance. In fact, I often turn off ViewState on all controls, and only turn it back on when I need it.

In ASP.NET 2 these problems have been minimized. ASP.NET 2 now has ViewState and ControlState. ControlState cannot be turned off, as it is where controls store little bits of information about page index, and other relevant properties.

Does this help?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Urami Bushi by Kill Bill OMPST- Meiko Kaji (Track 15 from the album: Kill Bill (Volume Two)) What's This?

rstelma March 1st, 2006 05:40 PM

That really helps. Yes, grdInventory is a datagrid. Yes, I've seen the montrous __VIEWSTATE on the page.

As fas as the benefits:

1. I don't mind binding the data on every post since I'm using the drop-downs for filters and I figure that every post is more or less going to be unique.

2. Since I'm going to probably have very large pages on this search page with 20,000 to 30,000 row results I was going to deploy custom paging anyway.

And thanks for the tip about turning off the ViewState on controls unless you need it. There is now an incredible difference in performance after making the ViewState False for this datagrid.

Thanks Imar. Much appreciated.

Richard



Imar March 1st, 2006 06:03 PM

You could even turn in it off for the entire page, through the <@ Page directive.

Just make sure you understand the consequences when you do so (listed earlier), and test thoroughly.... ;)

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.

rstelma March 2nd, 2006 02:03 PM

Well its good to know all of the different options. So, that's at least three options:

1. For the entire page through the @pagedirective, which turns view state off for everything.
2. On the control itself.
3. Through the Pre_Render function:

    Private Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRender
        Dim dgi As DataGridItem
        For Each dgi In grdInventory.Items
            dgi.EnableViewState = False
        Next
    End Sub

Excellent!


Imar March 2nd, 2006 05:14 PM

Yeah, correct. Each options has its own advantages and disadvantages.

I never used the code method, by setting the EnableViewState through code. I guess it works, but I never had the need to use it...

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.


All times are GMT -4. The time now is 08:36 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.