1
GridView 컨트롤 안에 놓은 드롭 다운 목록에 대해 SelectedIndexChanged 이벤트를 캡처하려고합니다. 그것은 잘 다시 게시하지만 내 SelectedIndexChanged 이벤트 처리기로 이동하지 않습니다. 여기 내 코드는캡처 DropDownList 인덱스 그리드 뷰 내부에서 이벤트 변경
DropDownList myddl;
protected void Page_Load(object sender, EventArgs e)
{
this.myGridview.RowDataBound += new GridViewRowEventHandler(myGridview_RowDataBound);
myddl = new DropDownList();
myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
if (!Page.IsPostBack)
{
List<Team> teams = giveMeTeams();
this.myGridview.DataSource = teams;
this.myGridview.AutoGenerateColumns = false;
BoundField col1 = new BoundField();
col1.DataField = "Name";
this.myGridview.Columns.Add(col1);
BoundField col2 = new BoundField();
col2.DataField = "Sport";
this.myGridview.Columns.Add(col2);
BoundField col3 = new BoundField();
col3.DataField = "Status";
this.myGridview.Columns.Add(col3);
this.myGridview.DataBind();
}
}
void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
myddl = new DropDownList();
myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
List<string> items = new List<string>();
items.Add("good");
items.Add("bad");
myddl.DataSource = items;
myddl.AutoPostBack = true;
myddl.DataBind();
e.Row.Cells[2].Controls.Add(myddl);
}
void myddl_SelectedIndexChanged(object sender, EventArgs e)
{
string temp = "In Here"; //neve hits this code
}
private List<Team> giveMeTeams()
{
Teams teams = new Teams();
teams.Add(new Team("RedWings", "Hockey", "good"));
teams.Add(new Team("Lions", "Football", "bad"));
teams.Add(new Team("Packers", "Football", "good"));
return teams;
}
어떤 도움을 주시면 감사하겠습니다. 감사합니다,
편집 댓글
당신이 제안 ... 아직도 다시 게시물을 캡처하고 있지 않다 나는이 시도에 따라. 여기 내 새로운 코드
void myGridview_RowCreated(object sender, GridViewRowEventArgs e)
{
DropDownList myddl = new DropDownList();
myddl = new DropDownList();
myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
myddl.ID = "MyID" + e.Row.RowIndex.ToString();
e.Row.Cells[2].Controls.Add(myddl);
}
void myGridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList myddl = e.Row.FindControl("MyID" + e.Row.RowIndex.ToString()) as DropDownList;
//myddl.SelectedIndexChanged += new EventHandler(myddl_SelectedIndexChanged);
List<string> items = new List<string>();
items.Add("good");
items.Add("bad");
myddl.DataSource = items;
myddl.DataMember = "Status";
myddl.AutoPostBack = true;
myddl.DataBind();
e.Row.Cells[2].Controls.Add(myddl);
}
여전히 내 myddl_SelectedIndexChanged() 이벤트 핸들러로하지 않을이다.
답장을 보내 주셔서 감사합니다 .... 방금 제안한 내용을 시도했습니다. – mikemurf22
원래 코드를 새 코드로 업데이트했습니다. 그래도 작동이 안되는. 내가 누락 된 아이디어가 있습니까? – mikemurf22
@ mikemurf22 : DDL의 ID에 RowIndex를 추가 할 필요는 없으며 GridViewRow (고유의 NamingContainer) 내부에서만 고유해야합니다. ASP.Net은 자동으로 고유 한 ID를 생성하며 FindControl ("MyID")를 통해 RowDataBound에서 찾을 수 있습니다. 하지만 주요 문제는 RowDataBound에서 다시 추가한다는 것입니다. RowCreated의 모든 PostBack에서 다시 작성됩니다. 'e.Row.Cells [2] .Controls.Add (myddl);'줄을 지우십시오. RowCreated와 RowDatabound에'if (e.Row.RowType == DataControlRowType.DataRow) { }' –