0

我在代码隐藏中有两个下拉列表和它们的隐藏字段,我通过 attribute.add 添加 javascript onchange 事件和一个按钮来执行一些动态操作,例如在运行时添加控件,当我单击该按钮时,下拉列表被重置。为了保持状态,我有一个带有下拉菜单的隐藏字段,我从隐藏字段中获取 selectedvalue 但通过编码 DDCity.Items.FindByValue 似乎不起作用有人能帮忙吗?

  protected void Page_Load(object sender, EventArgs e)
    { DDCountry.Attributes.Add("onChange", "javascript:BufferAddDDCountry('" + DDCountry.ClientID + "');");
            DDCity.Attributes.Add("onChange", "javascript:BufferAddDDCity('" + DDCity.ClientID + "');");}
 if (hiddenDDCityValue.Text != "0")
        {                

DDCity.Items.FindByValue(hiddenDDCityValue.Text).Selected = true;// this dont work

        }

    if (!IsPostBack)
    {   this.populateCountry();populateCity();}

javascript代码

    <script type="text/javascript">
function BufferAddDDCountry(objDd) {
            try {
                var objHidden = document.getElementById('hiddenDDcountryValue');
                objHidden.value = document.getElementById(objDd).value;
            } catch (e) {
                alert(e);
            }
        };
        function BufferAddDDCity(objDd) {
            try {
                var objHidden = document.getElementById('hiddenDDCityValue');
                objHidden.value = document.getElementById(objDd).value;
            } catch (e) {
                alert(e);
            }
        };

   </script>
4

1 回答 1

0

我atlast使它在javascript中工作希望这对其他人有帮助这里是代码
隐藏页面加载

   ScriptManager.RegisterStartupScript(UpdatePanel, this.GetType(), "Dropdownselectedvaluechange", "javascript:setSelectedValue('" + DDCity.ClientID + "','" + hiddenDDCityValue.Text +  "');", true);

javascript代码

 function setSelectedValue(dropdownList, selectedValue) {
            var dropdown = document.getElementById(dropdownList);
            for (var i = 0; i < dropdown.options.length; i++) {
                if (dropdown.options[i].value == selectedValue) {
                    dropdown.options[i].value = selectedValue;
                    dropdown.options[i].selected = true;

                    break;
                }
            }
            return;
        }
于 2012-07-04T07:36:57.933 回答