DropDownList 根据客户端的选择执行相关后台代码!

     实现思路:需要在下拉框的onchage客户端事件做相关处理,并且需要在页面放置一个hidvalue放置下拉框改变之前的索引值,用于

在客户取消后回滚到之前选项。如果确定那么直接让下拉框进行提交  __doPostBack(“下拉框ID”,''); 具体实现代码如下:

   aspx: 

   <html xmlns="http://www.w3.org/1999/xhtml" >

      <head runat="server">    

        <title>Untitled Page</title>    

        <script language="javascript">   

                function SelectedChange(ctl)        

                {            

                   if(confirm("确认提交后台数据吗?") == false)            

                  {                

                       ctl.selectedIndex = document.getElementById('HidLastSelect').value;                

                       return false;            

                  }            

                   else            

                  {                

                      document.getElementById('HidLastSelect').value = ctl.selectedIndex;                

                      _doPostBack(''+ ctl.ClientID +'','');            

                   }        

                }

       </script>

      </head>

    <body>     

            <form id="form1" runat="server">    

                   <div>        

                           <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">            

                                  <asp:ListItem>All</asp:ListItem>            

                                  <asp:ListItem>1</asp:ListItem>            

                                  <asp:ListItem>2</asp:ListItem>             
                          </asp:DropDownList>    

                   </div>     

                  <input type="hidden" id="HidLastSelect" value="0" runat="server" />    

            </form>

     </body>

  </html>

 cs:

   public partial class Default3 : System.Web.UI.Page {    

            protected void Page_Load(object sender, EventArgs e)     {               

                 DropDownList1.Attributes.Add("onchange", "return SelectedChange(this)");

            }   

           protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)     {      

                Response.Write("changed");    

           }

  }

posted on 2012-03-12 15:57  朝扬  阅读(862)  评论(1编辑  收藏  举报