1

DDL に別の DDL を設定していますが、別のページから値を取得しています

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--Select country--");

        }


        if(Session["uname"]!=null)
        {
              DropDownList1.SelectedValue = Session["country"].ToString();
           ProfileMasterBLL bll=new ProfileMasterBLL();
            foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
            {
                if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
                {
                    var query = (ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));
                    DropDownList2.DataSource = query;
                    DropDownList2.DataBind();
                 }
            }


            TextBox8.Text = Session["email"].ToString();
            string pwd = Session["pwd"].ToString();
            TextBox9.Attributes.Add("value",pwd);
            TextBox10.Attributes.Add("value", pwd);

        }
    }

しかし、問題は、DDL 値を変更するたびに、page_load のようにセッション値に固定されるため、DDL で選択した項目に値を変更するにはどうすればよいかということです。

4

3 に答える 3

0

ドロップダウンリストのプロパティをtrueに設定するとともに、OnSelectedIndexChangedイベントを使用します。AutoPostBack

そして、OnSelectedIndexChangedイベントハンドラーで、2番目のドロップダウンリストに入力するコードを追加します。

于 2012-08-26T04:59:48.380 に答える
0

質問が正しいことを理解していれば、DropDownList1 の値に応じて DropDownList2 の値を変更する必要がありました。dropdownlist の初期値は別のページから取得されます。

    protected void Page_Load(object sender, EventArgs e)
     {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--Select country--");

            //get the selected country from another page
            string selectedCountry = Convert.ToString(Session["country"]);

            //set the selected value
            DropDownList1.Items.FindByValue(selectedCountry).Selected = true;

            //Bind Dropdonwlist2
             BindDropDownList(DropDownList1.SelectedItem.Text);

        }

        /*
         remaining code
         */
    }

dropdonwList 2 コードをバインドする

    /// <summary>
    /// Bind dropdownlist2 
    /// </summary>
    /// <param name="selectedCountry"></param>
    protected void BindDropDownList(string selectedCountry)
    {
        ProfileMasterBLL bll = new ProfileMasterBLL();
        foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
        {
            if (VARIABLE.ToString().Contains(selectedCountry))
            {
                var query = (ProfileMasterDAL.GetStatesByCountrys(selectedCountry));
                DropDownList2.DataSource = query;
                DropDownList2.DataBind();
            }
        }

    }

dropdonwlist1 の選択されたインデックスの変更で、値が変更されるようになりました

dropdownlist1 の autopostback を true に設定します

DropDownList1.AutoPostBack = true;

    /// <summary>
    /// DropDownList1 Selcted Index change
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected  void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
         BindDropDownList(DropDownList1.SelectedItem.Text);
    }

これがあなたの問題を解決することを願っています

于 2012-08-26T05:18:42.537 に答える