边城骆驼 -- 每天进步百分之一

人生,不是得到,就是学到!

取得ListBox多选项的值方法

ASPX部分

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
            <asp:ListItem>Xiamen</asp:ListItem>
            <asp:ListItem>Fuzhou</asp:ListItem>
            <asp:ListItem>Quanzhou</asp:ListItem>
            <asp:ListItem>Zhangzhou</asp:ListItem>
        </asp:ListBox>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></div>
    </form>
</body>
</html>

CS方法

protected void Button1_Click(object sender, EventArgs e)
    {
        int i = 0;
        string value = "";
        string script = "";
        int[] j = ListBox1.GetSelectedIndices();
        for(i = 1;i<=j.Length;i++)
        {
            value += "'" + ListBox1.Items[j[i - 1]].Value + "',";
        }
        value = value.Substring(0, value.Length - 1);
        script = "<script language='javascript'>alert(" + value + ")</script>";
        ClientScript.RegisterStartupScript(script.GetType(), "Alert", script);
    }

VB方法

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim i As Integer = 0
        Dim value As String = ""
        Dim script As String = ""
        Dim j As Integer() = ListBox1.GetSelectedIndices()
        For i = 1 To j.Length
            value += "'" + ListBox1.Items(j(i - 1)).Value + "',"
        Next
        value = value.Substring(0, value.Length - 1)
        script = "<script language='javascript'>alert(" + value + ")</script>"
        ClientScript.RegisterStartupScript(script.GetType, "Alert", script)

    End Sub

开发环境VS2005

阅读更多
个人分类: .NET
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

取得ListBox多选项的值方法

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭