0

我的 ASPX

<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="tz.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="js/jquery.js"></script>
<script type="text/javascript">

    function ddl_changed() {
        var dc = "#" + "<%=ddl2.ClientID%>";
        var link = '/test.aspx/testfun';
        $.ajax({
            type: 'POST',
            url: link,
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (result) {
                $(dc).get(0).options.length = 0;
                $(dc).get(0).options[0] = new Option("--------Select--------", "--------Select--------");
                $.each(result.d, function (item, value) {
                    var d = value;
                    $(dc).get(0).options[$(dc).get(0).options.length] = new Option(d);
                });                
            },
            error: function (result) {
                alert("Failed");
            }
        });
    }

</script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblInfo" runat="server" Text=""></asp:Label>
        <hr />
        <asp:dropdownlist ID="ddl1" runat="server" onchange="ddl_changed()">
            <asp:ListItem>-----Select-----</asp:ListItem>
            <asp:ListItem>-----Test-----</asp:ListItem>
        </asp:dropdownlist>    

        <asp:dropdownlist ID="ddl2" runat="server">
            <asp:ListItem>-----Select-----</asp:ListItem>
        </asp:dropdownlist>    
        <asp:Button ID="btnTest" runat="server" Text="Test" OnClick="btnTest_Click" />

    </div>
    </form>
</body>

</html>

和后面的代码

public partial class test : System.Web.UI.Page
{

    [WebMethod(EnableSession = true)]
    public static ArrayList testfun()
    {
        ArrayList al = new ArrayList();
        al.Add("test1");
        al.Add("test2");
        al.Add("test3");
        al.Add("test4");
        al.Add("test5");
        return al;
    }

    protected void btnTest_Click(object sender, EventArgs e)
    {
        lblInfo.Text = ddl2.Text;
    }

}

选择ddl1更改后,ddl2使用示例数据填充。从中选择任何项目ddl2并单击“测试”按钮后,它只显示第一个项目..为什么?如何发布我所做的选择?

4

1 回答 1

0

请改用更新面板。

标记:

<asp:dropdownlist AutoPostback="True" OnSelectedIndexChange="BindDDL2" ID="ddl1" runat="server">
    <asp:ListItem>-----Select-----</asp:ListItem>
    <asp:ListItem>-----Test-----</asp:ListItem>
</asp:dropdownlist>

<asp:UpdatePanel id="up1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsynchronousPostbackTrigger ControlId="ddl1" Event="SelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:dropdownlist ID="ddl2" runat="server" AppendDataBoundItems="True">
            <asp:ListItem>-----Select-----</asp:ListItem>
        </asp:dropdownlist>  
    <ContentTemplate>
</asp:UpdatePanel>

代码隐藏:

public void BindDDL2(object sender, EventArgs e)
{
    ArrayList al = new ArrayList();
    al.Add("test1");
    al.Add("test2");
    al.Add("test3");
    al.Add("test4");
    al.Add("test5");
    ddl2.DataSource = a1;
    ddl2.DataBind();
}

顺便说一句,您需要ScriptManager在aspx页面上添加一个。

于 2013-08-02T23:29:19.730 回答