2012-02-29 5 views
1

私のページの上部に2つのボタンがあります。コントロールを含むパネルのリストのメンテナンス。イベントハンドラを失う

[追加]コントロールをクリックすると、新しいパネルと新しいDropDownListを作成する関数が起動します。

DropDownListにSelectedIndexChangedのイベントハンドラを割り当てます。

私のDropDownListの選択に応じて、そのパネル内に別のコントロールを作成します。

最終的に、パネルには、自分のクエリのパラメータとして渡すことができるいくつかのフィールドが含まれます。

ポストバックごとに再作成されるパネルのリストにパネルを保存します。

問題は、パネルを追加してパネルを削除し、リストからすべてのパネルをクリアできますが、SelectedIndexChangedトリガを起動することはできないようです...割り当てられていない各ポストバックで失われます。

私は私のグーグルの専門知識を使い果たしました。私は自分自身にかなり不満を感じています。私は提案/修正に開放されています。

ありがとうございます。

List<Panel> persistControls = new List<Panel>(); 

protected override void OnInit(EventArgs e) 
{ 
    if (Session["persistControls"] != null) 
    { 
     persistControls = (List<Panel>)Session["persistControls"]; 
     int count = 0; 

     foreach (Panel dynamicControl in persistControls) 
     { 
      AddQuestionTypeDropDownList(count); 
      dynamicControl.ID = "panel" + count; 

      Button btnRemove = new Button(); 
      btnRemove.Click += new EventHandler(btnDelete_Click); 
      btnRemove.Text = "Remove"; 
      btnRemove.CommandArgument = count.ToString(); 

      // Pushing to Placeholder 
      myPlaceholder.Controls.Add(dynamicControl); 
      myPlaceholder.Controls.Add(btnRemove); 
      count++; 
     } 
    } 
    base.OnInit(e); 
} 

// Calls three functions responsible for pulling from the Database and binding the Datagrid. 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     GetClustersFromDatabase(userid); 
     BindGrid(); 
     BindState();    
    } 
} 

private DropDownList AddQuestionTypeDropDownList(int count) 
{ 
    DropDownList list = new DropDownList(); 

    list.ID = "list" + count;  
    list.Items.Add(new ListItem("--Select One--", "")); 
    list.Items.Add(new ListItem("Title", "1")); 
    list.Items.Add(new ListItem("Contact", "2")); 
    list.Items.Add(new ListItem("Date Created", "3")); 
    list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); 
    list.AutoPostBack = true; 

    return list; 
} 

private DropDownList AddQuestionTypeDropDownList() 
{ 
    DropDownList list = new DropDownList(); 
    list.Items.Add(new ListItem("--Select One--", "")); 
    list.Items.Add(new ListItem("Title", "1")); 
    list.Items.Add(new ListItem("Contact", "2")); 
    list.Items.Add(new ListItem("Date Created", "3")); 
    list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged); 
    list.AutoPostBack = true; 

    return list; 
} 

protected void btnAdd_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     Panel panelContainer = new Panel(); 
     panelContainer.ID = "panel" + persistControls.Count; 

     panelContainer.Controls.Add(AddQuestionTypeDropDownList()); 

     Button btnRemove = new Button(); 
     btnRemove.Click += new EventHandler(btnDelete_Click); 
     btnRemove.Text = "Remove"; 
     btnRemove.CommandArgument = persistControls.Count.ToString(); 

     myPlaceholder.Controls.Add(panelContainer); // Pushes the Panel to the page. 
     persistControls.Add(panelContainer);// Adds our Panel to the Control list 

     myPlaceholder.Controls.Add(btnRemove); // Pushes our Button to the page. 
     Session["persistControls"] = persistControls; // put it in the session 
    } 
    catch 
    { 
     throw; 
    } 
} 

private void list_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    //I have no code here but thats because I currently can't even get it to fire. 
    try 
    { 

    } 
    catch 
    { 
     throw; 
    } 
} 

protected void btnClear_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     Session["persistControls"] = null; 
     Response.Redirect(Request.Url.ToString()); 
    } 
    catch 
    { 
     throw; 
    } 
} 

protected void btnDelete_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     int deleteThisOne = int.Parse(((Button)sender).CommandArgument); 
     persistControls.Remove(persistControls[deleteThisOne]); 
     Session["persistControls"] = persistControls;   
     Response.Redirect(Request.Url.ToString()); 
    } 
    catch 
    { 
     throw; 
    } 
}  
+0

Page_Loadのコントロールにイベントを追加する必要があります。 OnInitに登録されたイベントは決して発射されません... –

答えて

0

私はforeachのと、パネルの私のリストを反復処理し、そのパネル内の各コントロールを反復処理するために、ネストされたのforeachを持つことによって、問題を解決することができました。私はコントロールのタイプをチェックし、すべてのポストバックでイベントハンドラを修正します。

関連する問題