asp.net 後臺cs代碼怎麼操做前臺用ajax實現下拉框進行搜索

html頁面的兩個下拉類別框是聯動的,第一個下拉框是後臺綁定的,第二個下拉框是用ajax來無刷新動態綁定的,在作查詢頁面時,選擇第一個下拉框以後,第二個下拉框也發生變化,點擊搜索以後,第二個下拉類別框沒有數據了, html

<asp:DropDownList ID="dropTownship" runat="server"></asp:DropDownList> ajax

<asp:DropDownList ID="dropVillage" runat="server" Width="230px">
                    <asp:ListItem Value="">---請選擇---</asp:ListItem>
</asp:DropDownList> code

查詢代碼以下 orm

protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strdropTownship = dropTownship.SelectedValue.Trim();
        string strName = txtName.Text.Trim();
        string strdropVillage = dropVillage.SelectedValue.Trim();
        string strWhere = "township=" + Common.UrlEncode(strdropTownship) +
            "&name=" + Common.UrlEncode(strName) +
            "&village=" + Common.UrlEncode(strdropVillage) ;
        Response.Redirect("VillageInforEnrollList.aspx?" + strWhere);
    } server

上面的代碼中dropVillage.SelectedValue.Trim();讀取到永遠爲空,這是爲何呢,由於它的數據不是後臺綁定,而是前臺經過ajax來實現的,這時要經過Request.Form["dropVillage"].ToString();方式來獲取dropVillage下拉列表框的值 htm

Response.Redirect("VillageInforEnrollList.aspx?" + strWhere);這句會從新加載VillageInforEnrollList,執行PageLoad事件 事件

這時參數裏含township和village這個兩個參數而後經過這兩個參數來後臺綁定dropVillage,這樣查詢時dropVillage能夠保留原先搜索的值而不是置空。代碼以下 ip

if (Request.QueryString["township"] != null && Request.QueryString["township"].ToString().Length > 0)
                {
                    DataTable dtVillage = new SysAreaMg().GetList(" flag='5' and PCode='" + Common.GetEncodeQueryString("township") + "' ").Tables[0];
                    Common.DataBind("AreaName", "AreaCode", dtVillage, dropVillage);//綁定dropVillage
                    if (Request.QueryString["village"] != null && Request.QueryString["village"].ToString().Length > 0)
                    { string

                       //綁定dropVillage下拉框默認值
                        dropVillage.Items.FindByValue(Common.GetEncodeQueryString("village")).Selected = true;
                    }
                    else
                    {
                   
                    }
                } 後臺

這裏來經過先後臺來肯定dropVillage下拉框的值

相關文章
相關標籤/搜索