开发者

dropdown list as reference arugment to a class

i need to search and find in dropdown list in my project a lot of times, so i wrote a function

 Public Overloads Function findInList(ByVal strValue As String开发者_运维问答, ByRef ddl As DropDownList, ByVal findType As ddlFindtype) As Integer
    Try
        If findType = ddlFindtype.byText Then
            Dim li As ListItem = ddl.Items.FindByText(strValue)
            If Not IsNothing(li) Then
                ddl.SelectedIndex = ddl.Items.IndexOf(li)
            End If
        ElseIf findType = ddlFindtype.byValue Then
            Dim li As ListItem = ddl.Items.FindByValue(strValue)
            If Not IsNothing(li) Then
                ddl.SelectedIndex = ddl.Items.IndexOf(li)
            End If
        End If
        Return ddl.SelectedIndex
    Catch ex As Exception
        Throw ex
    End Try
End Function

it is in a class and obviously it is not working, the ddl is loaded in a aspx page , and it is passed to this function as reference. the ddl as reference is not containing its values from the aspx page. i need to what i am doing is correct, or is there any other way to achieve it.

thanks and regards


I would write it as follows:

Public Function findInList(strValue As String, ByRef ddl As DropDownList, findType As ddlFindtype) As Integer
Try
    Select Case findType
        Case ddlFindtype.byText
            Dim li As ListItem = ddl.Items.FindByText(strValue)
            If (li IsNot Nothing) Then
                ddl.SelectedIndex = ddl.Items.IndexOf(li)
            End If
            Exit Select
        Case ddlFindtype.byValue
            Dim li As ListItem = ddl.Items.FindByValue(strValue)
            If (li IsNot Nothing) Then
                ddl.SelectedIndex = ddl.Items.IndexOf(li)
            End If
            Exit Select
    End Select
    Return ddl.SelectedIndex
Catch ex As Exception
    Throw ex
End Try

End Function

And call it just as Richard laid out:

Dim c As ThisIsMyClass = _
        New ThisIsMyClass()

    Dim i As Integer

    i = c.findInList("3", ddlTest, ddlFindType.byValue)


I just tested this with your function and it seems to be working for me. The function correctly sets the selected item and returns the selected index. Maybe the way you're loading the list items is an issue? For my test, I just statically loaded some values in the markup.

Here's the markup for my test list and button:

<asp:DropDownList ID="ddlTest" runat="server">
    <asp:ListItem Text="Item1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Item3" Value="3"></asp:ListItem>
    <asp:ListItem Text="Item4" Value="4"></asp:ListItem>
    <asp:ListItem Text="Item5" Value="5"></asp:ListItem>
</asp:DropDownList>

<asp:Button ID="btnTest" runat="server" Text="PushMe" OnClick="btnTest_click" />

Here's my button click handler that calls your function

    Protected Sub btnTest_click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim c As ThisIsMyClass = _
            New ThisIsMyClass()

        Dim i As Integer

        i = c.findInList("3", ddlTest, ddlFindType.byValue)

    End Sub

I successsfully tested with both byVal and byText.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜