2009-07-25 6 views
4

드롭 다운 목록에서 값을 선택하면 내 selectedindexchanged 이벤트가 실행되지 않습니다. 이러한 드롭 다운 목록은 다음 코드에서 동적으로 구현됩니다. autopostback 및 enableviewstate 설정을 사용하지 않으려 고 시도했습니다. 정적 패널을 사용하고 있습니다. 아무도 내가 선택한 selectedexchanged 이벤트를 발생시킬 수있는 방법을 볼 수 있습니까?ASP.NET/C# : DropDownList SelectedIndexChanged 이벤트가 실행되지 않음

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Data; 
using System.Data.SqlClient; 
using System.Diagnostics; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using ADONET_namespace; 

namespace AddFileToSQL 
{ 
    public partial class DataMatch : _Default 
    { 
     protected System.Web.UI.WebControls.PlaceHolder phTextBoxes; 
     protected System.Web.UI.WebControls.PlaceHolder phDropDownLists; 
     protected System.Web.UI.WebControls.Button btnAnotherRequest; 
     protected System.Web.UI.WebControls.Panel pnlCreateData; 
     protected System.Web.UI.WebControls.Literal lTextData; 
     protected System.Web.UI.WebControls.Panel pnlDisplayData; 
     //Panel pnlDropDownList; 

     protected static string inputfile2; 
     static string[] headers = null; 
     static string[] data = null; 
     static string[] data2 = null; 
     static DataTable myInputFile = new DataTable("MyInputFile"); 
     static string[] myUserSelections; 

     // a Property that manages a counter stored in ViewState 
     protected int NumberOfControls 
     { 
      get { return (int)ViewState["NumControls"]; } 
      set { ViewState["NumControls"] = value; } 
     } 

     public void EditRecord(object recordID) 
     { 
      SelectedRecordID = recordID; 
      // Load record from database and show in control 
     } 

     protected object SelectedRecordID 
     { 
      get 
      { 
       return ViewState["SelectedRecordID"]; 
      } 
      set 
      { 
       ViewState["SelectedRecordID"] = value; 
      } 
     } 

     // Page Load 
     private void Page_Load(object sender, System.EventArgs e) 
     { 
      if (!Page.IsPostBack) 
      { 
       this.NumberOfControls = 0; 
      } 
     } 

     // Add DropDownList Control to Placeholder 
     private void CreateDropDownLists() 
     { 
      for (int counter = 0; counter < NumberOfControls; counter++) 
      { 
       DropDownList ddl = new DropDownList(); 
       SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable); 
       ddl.ID = "DropDownListID" + (counter + 1).ToString(); 
       ddl.DataTextField = "COLUMN_NAME"; 
       ddl.DataValueField = "COLUMN_NAME"; 
       ddl.DataSource = dr; 
       ddl.DataBind(); 

       //myUserSelections[counter] = ""; 

       ddl.AutoPostBack = true; 
       ddl.EnableViewState = true; //Preserves View State info on Postbacks 
       ddl.Style["position"] = "absolute"; 
       ddl.Style["top"] = 100 * counter + 80 + "px"; 
       ddl.Style["left"] = 250 + "px"; 
       ddl.SelectedIndexChanged += new EventHandler(SelectedIndexChanged); 

       pnlDisplayData.Controls.Add(ddl); 
       pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>")); 
       pnlDisplayData.Visible = true; 

       // pnlDropDownList.FindControl(ddl.ID); 
       dr.Close(); 
      } 
     } 

     protected void SelectedIndexChanged(object sender, EventArgs e) 
     { 
      DropDownList ddl = (DropDownList)sender; 
      string ID = ddl.ID; 
     } 

     // Add TextBoxes Control to Placeholder 
     private void RecreateDropDownLists() 
     { 
      for (int counter = 0; counter < NumberOfControls; counter++) 
      { 
       DropDownList ddl = new DropDownList(); 
       SqlDataReader dr = ADONET_methods.DisplayTableColumns(targettable); 

       ddl.ID = "DropDownListID" + (counter + 1).ToString(); 
       ddl.DataTextField = "COLUMN_NAME"; 
       ddl.DataValueField = "COLUMN_NAME"; 
       ddl.DataSource = dr; 
       ddl.DataBind(); 
       myUserSelections[counter] = ""; 
       dr.Close(); 

       ddl.AutoPostBack = true; 
       ddl.EnableViewState = false; //Preserves View State info on Postbacks 
       ddl.Style["position"] = "absolute"; 
       ddl.Style["top"] = 100 * counter + 80 + "px"; 
       ddl.Style["left"] = 250 + "px"; 
       pnlDisplayData.Controls.Add(ddl); 
       pnlDisplayData.Controls.Add(new LiteralControl("<br><br><br>")); 
      } 
     } 

     // Create TextBoxes and DropDownList data here on postback. 
     protected override void CreateChildControls() 
     { 
      // create the child controls if the server control does not contains child controls 
      this.EnsureChildControls(); 

      // Creates a new ControlCollection. 
      this.CreateControlCollection(); 

      // Here we are recreating controls to persist the ViewState on every post back 
      if (Page.IsPostBack) 
      { 
       RecreateDropDownLists(); 
       RecreateLabels(); 
      } 
      // Create these conrols when asp.net page is created 
      else 
      { 
       PopulateFileInputTable(); 
       CreateDropDownLists(); 
       CreateLabels(); 
      } 

      // Prevent child controls from being created again. 
      this.ChildControlsCreated = true; 
     } 

     // Read all the data from TextBoxes and DropDownLists 
     protected void btnSubmit_Click(object sender, System.EventArgs e) 
     { 
      int cnt = FindOccurence("DropDownListID"); 
      EditRecord("DropDownListID" + Convert.ToString(cnt + 1)); 
      AppendRecords(); 
      pnlDisplayData.Visible = false; 
     } 

     private int FindOccurence(string substr) 
     { 
      string reqstr = Request.Form.ToString(); 
      return ((reqstr.Length - reqstr.Replace(substr, "").Length)/substr.Length); 
     } 
    } 
} 

답변

5

당신은 당신의 RecreateDropDownLists() 방법 :

+0

예, 이렇게했습니다! 감사! – salvationishere

2

에 이벤트 핸들러를 지정하지 않는 당신은 다시 게시의 경우 DropDownList로의의 SelectedIndexChanged 이벤트까지 배선 할 것 같지 않습니다.

재생성과 생성이 기능적으로 동일하다면 왜 이들을 병합하지 않을까요? 다양한 것을 캡슐화하십시오.

+0

좋은 지적 - 나는이 충고를 따를 것이다. – salvationishere

관련 문제