<form id="Form1" method="post" runat="server">
   <asp:button id="Button1" Text="Button" Runat="server"></asp:button>
   <asp:dropdownlist id="ddlType" Runat="server"></asp:dropdownlist>
   <INPUT onclick="ReloadType()" type="button" value="Button">
</form><SCRIPT language="javascript">
   function ReloadType(){
        var ddlType = document.getElementById("ddlType");
var newOption = document.createElement("OPTION");
newOption.text = "AAA";
newOption.value = "BBB";
ddlType.options.add(newOption);
   }
</script>/////////////////////////////////////////////////////////我要在ASPX頁面中調用function ReloadType()來改變ddlType的值:如果用客戶端的HTML按鈕控件來調用ReloadType()的話(以下示例)沒問題。
<INPUT onclick="ReloadType()" type="button" value="Button">
如果通過單擊服務器控件Button1來調用ReloadType()的話(以下示例),因為頁面刷新的原因ddlType中的值改變後又恢復原來初始狀態,無法保留新值!private void Page_Load(object sender, System.EventArgs e){
    if(!this.IsPostBack){
         Button1.Attributes.Add("onclick","javascript:ReloadType()");
     }
}Button1的EnableViewState=true
ddlType的EnableViewState=true     AutoPostBack=false
請問這是為什麼?如何解決?

解决方案 »

  1.   

    由浏览器端产生的ListItem应该不会被保存到ViewState中,所以在重新载入页面的时候就被ViewState中的值给还原了吧,不知道对不对
      

  2.   

    既然按钮事件要在后台执行,那你为什么不把产生ListItem的这个过程放到后台去做,你现在这么写 等于绕了个圈子
      

  3.   

    樓主的意思是用服務器控件產生的客戶端事件去執行
    Button1.Attributes.Add("onclick","javascript:ReloadType()");也許是方便客戶端調用吧
      

  4.   

    樓主的意思是用服務器控件產生的客戶端事件去執行
    Button1.Attributes.Add("onclick","javascript:ReloadType()");也許是方便客戶端調用吧