1
我想捕獲SelectedIndexChanged事件的下拉列表,我已經放入了一個GridView控件。它回發很好,但不會進入我的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 2011-03-17 18:23:37
我用我的新代碼更新了原始問題。還是行不通。任何想法我失蹤? – mikemurf22 2011-03-17 18:30:31
@ mikemurf22:您不必將RowIndex追加到DDL的ID,它只能是GridViewRow(它們的NamingContainer)的唯一內容。 ASP.Net自動生成唯一的ID,您可以通過FindControl(「MyID」)在RowDataBound中找到它們。但主要的問題是你在RowDataBound中再次添加它們。它們將在RowCreated的每個回傳中重新創建。刪除行e.Row.Cells [2] .Controls.Add(myddl);'。你還必須在RowCreated和RowDatabound中嵌套你的代碼,在if(e.RowTypeRowType == DataControlRowType.DataRow){ }' – 2011-03-17 19:32:29