0

我正在使用 asp.net c# 来制作一个项目。我需要在其中创建一个包含汽车型号的下拉列表的部分,该下拉列表是从我的数据库中获取的。之后,每次在下拉列表中选择一个新值时,我都需要更改面板的内容(不刷新整个页面)。

我尝试使用以下代码:

 <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataTextField="MODEL" Height="39px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="151px">
        </asp:DropDownList>
<asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>           
        <br />            
        <div id="finalDisplay" runat="server"></div>            
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger controlid="DropDownList1" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

我相信这里应该使用的结构是 updatePanel。所以我需要我的 id 为 finalDisplay 的 div 来显示所有也存储在数据库中的评论。这是我的代码:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        //get car id from database
        string model = DropDownList1.SelectedValue.ToString();
        string sql1 = "SELECT CAR_ID FROM CARS WHERE MODEL='" + model + "'";
        string[] attr1 = { "CAR_ID"};
        List<String> cid = getList(sql1, attr1);
        string car_id = cid[0].ToString().Trim();
        //get all comments stored in the database for that car
        string sql2 = "SELECT USER_ID,CONTENT FROM COMMENTS where car_id='" + car_id + "'";
        string[] attr2 = { "CONTENT" };
        List<String> contents = getList(sql2, attr2);
        string class_names= "usernames";
        string class_contents= "contents";
        for (int i = 0; i < contents.Count-1; i++) {
            string uid = contents[i];
            string tmp_sql = "Select user_name from users where user_id='" + uid + "'";
            string[] tmp_attr={"USER_NAME"};
            List<String> tmp_list=getList(tmp_sql,tmp_attr);
            finalDisplay.InnerHtml += "<p class="+class_names+">" + tmp_list[0] + ":</p>";
            finalDisplay.InnerHtml += "<p class="+class_contents+">" + contents[i + 1] + "</p>";
            i = i + 1;
        }
    }

在 id 为 finalDisplay 的 div 中没有任何反应,它是空的。我究竟做错了什么?这是我第一次使用 ajax(如果这甚至是 ajax)。

4

1 回答 1

0

移动DropDownList内部UpdatePanel

于 2013-07-02T14:55:49.503 回答