2015-01-05 76 views
0

後IndexChanged事件上的下拉列表中不堅持/火我已經如何在asp.net即時創建動態控件;回發

幾個動態下拉菜單

與選定的指數變化事件附屬於它們,但會發生什麼情況是第一次觸發索引更改事件,並且一旦回發完成,它們將丟失該indexchange事件,但不會丟失項目值

因此它保存下拉菜單的狀態,但不會在回發後附加 選定的索引更改事件。

AspxPage:

<div> 
    <asp:PlaceHolder ID="mp_1" runat="server"></asp:PlaceHolder> 
    </div> 

C#:

public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Init(object sender, EventArgs e) 
     { 

     } 

     private void ddl_IndexChanged(object sender, EventArgs e) 
     { 
      Response.Write("dsfsdf"); 
     }   

     protected void Page_Load(object sender, EventArgs e) 
     { 

DropDownList ddl_1 = new DropDownList(); 
       ddl_1.ID = "ddl_1"; 
       ddl_1.AutoPostBack = true; 
       ddl_1.EnableViewState = true; 
       ddl_1.SelectedIndexChanged += new EventHandler(ddl_IndexChanged); 
       this.mp_1.Controls.Add(ddl_1); 


      if(!IsPostBack) 
      { 
       DropDownList ddl_1 = (DropDownList)Page.FindControl("ddl_1"); 
       if (ddl_1 != null) 
       { 
        List<ListItem> items = new List<ListItem>(); 
        items.Add(new ListItem("Item 2", "Value 2")); 
        items.Add(new ListItem("Item 1", "Value 1")); 
        items.Add(new ListItem("Item 3", "Value 3")); 
        ddl_1.Items.AddRange(items.ToArray()); 
       }     
      }    
     } 
    } 
+1

如果您的控件不存在回發他們不能打的情況下handelers,如果去掉'(頁.IsPostBack)'並嘗試 – prospector

回答

0

AspxPage:

<div> 
    <asp:PlaceHolder ID="mp_1" runat="server"></asp:PlaceHolder> 
    </div> 

C#:

public partial class WebForm1 : System.Web.UI.Page 
    { 
     protected void Page_Init(object sender, EventArgs e) 
     { 
      DropDownList ddl_1 = new DropDownList(); 
      ddl_1.ID = "ddl_1"; 
      ddl_1.AutoPostBack = true; 
      ddl_1.EnableViewState = true; 
      ddl_1.SelectedIndexChanged += new EventHandler(ddl_IndexChanged); 
      this.mp_1.Controls.Add(ddl_1); 
     } 

     private void ddl_IndexChanged(object sender, EventArgs e) 
     { 
      Response.Write("dsfsdf"); 
     }   

     // add entries in page load method not in init method 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if(!IsPostBack) 
      { 
       DropDownList ddl_1 = (DropDownList)Page.FindControl("ddl_1"); 
       if (ddl_1 != null) 
       { 
        List<ListItem> items = new List<ListItem>(); 
        items.Add(new ListItem("Item 2", "Value 2")); 
        items.Add(new ListItem("Item 1", "Value 1")); 
        items.Add(new ListItem("Item 3", "Value 3")); 
        ddl_1.Items.AddRange(items.ToArray()); 
       }     
      }    
     } 
    } 
相關問題