0

Hey all, i am in need of some help trying to figure out why this dropdown is not selecting the value after i pick one.

Here is the ASP.net for the dropdown part:

 <div id="divItemsPerPage" runat="server" style="font-size:60%;font-weight:bolder;padding-right:15px;">
     Items per Page:
       <asp:DropDownList ID="ddlPerPage" runat="server" AutoPostBack="true">
                <asp:ListItem Text="10" Value="10" />
                <asp:ListItem Text="25" Value="25" Selected="True" />
                <asp:ListItem Text="50" Value="50" />
                <asp:ListItem Text="100" Value="100" />
       </asp:DropDownList>
 </div>

and this is my codebehind:

 Private Sub ddlPerPage_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlPerPage.SelectedIndexChanged
        'LoadFromViewstate()
        _rowsPerPage = Convert.ToInt32(ddlPerPage.SelectedItem.Value)
        gvData.PageIndex = 0
        SaveRowsCountToCookie(_rowsPerPage)
 End Sub

 Public Property RowsPerPage() As Integer
        Get
            Return Utils.cleanInt(ddlPerPage.SelectedValue)
        End Get
        Set(ByVal value As Integer)
            Dim itmX As New ListItem(value.ToString, value.ToString)

            ddlPerPage.SelectedIndex = -1
            If ddlPerPage.Items.Contains(itmX) Then
                ddlPerPage.Items.FindByValue(value.ToString).Selected = True
            Else
                ddlPerPage.SelectedIndex = -1
                For i As Integer = 0 To ddlPerPage.Items.Count - 1
                    If Utils.cleanInt(ddlPerPage.Items(i).Value) > value Then
                        ddlPerPage.Items.Insert(i, itmX)
                        ddlPerPage.SelectedIndex = i
                        Exit For
                    End If
                Next
            End If
            _rowsPerPage = value
            ViewState("RowsPerPage") = _rowsPerPage
        End Set
    End Property

No matter what i select it still has the same value as when the page started. The _rowsPerPage = value always seems to be the same.

UPDATE

Ok, looking at it more closely it seems that Convert.ToInt32(ddlPerPage.SelectedItem.Value) never has the correct value when i select a number??

Any help would be great! :o)

David

3
  • Did you modify the viewstate, or turn it off? Commented Sep 27, 2010 at 13:05
  • This is what is under the LoadFromViewstate If Not ViewState("RowsPerPage") Is Nothing Then _rowsPerPage = CType(ViewState("RowsPerPage"), Integer) but i turned that off to test without it and it still seems to stay on one value. Commented Sep 27, 2010 at 13:09
  • Did you debugged your code? May be your selectedindex method is called more than once because you set the index in rowsperpage method again. What is the value that you receive each time? Commented Sep 27, 2010 at 13:42

3 Answers 3

2

Try

_rowsPerPage = CInt(ddlPerPage.SelectedValue)

Sign up to request clarification or add additional context in comments.

Comments

1

Not sure about this, but it could be that the SelectedItem gets updated after SelectedIndex. In that case you may want to do something similar to that:

Instead of ddlPerPage.SelectedItem.Value

Use ddlPerPage.Items[SelectedIndex].Value

I think in vb.net you should use () instead [].

2 Comments

It tells me that SelectedIndex is not declared?
Tried this CInt(ddlPerPage.Items(ddlPerPage.SelectedIndex).Value) but still does not work.
1

Define _rowsPerPage variable as static like that:

private Shared _rowsPerPage as string

2 Comments

It wont allow me to add static. I already had it dimmed Private _rowsPerPage As Integer = 10
Shared is the VB equivalent of Static

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.