开发者

ASP.net dropdown value help

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.S开发者_如何学运维electedItem.Value) never has the correct value when i select a number??

Any help would be great! :o)

David


Try

_rowsPerPage = CInt(ddlPerPage.SelectedValue)


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 [].


Define _rowsPerPage variable as static like that:

private Shared _rowsPerPage as string
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜