关闭

取得ListBox多选项的值方法

标签: buttonaspintegerjavascriptstringserver
1576人阅读 评论(0) 收藏 举报
分类:

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:245403次
    • 积分:3577
    • 等级:
    • 排名:第9480名
    • 原创:102篇
    • 转载:2篇
    • 译文:0篇
    • 评论:97条
    文章分类
    最新评论