你希望選擇這一行的DropDownList可以讓另外一行的Label帶出資料
像是選擇幣別然後同一列的其他行帶出匯率之類的
這個功能在SelectedIndexChanged就能辦到
但會有個問題
我怎麼知道是哪一列的DropDownList被異動?
如果我不知道是哪個DropDownList被異動
我又怎麼知道要去修改哪一列的內容
這個問題解法似乎很多
目前我只想到這種解法
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("No", typeof(string)));
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["No"] = i.ToString();
dt.Rows.Add(dr);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
int index = Convert.ToInt32(ddl.Attributes["index"]);
Label txt = GridView1.Rows[index].FindControl("Label1") as Label;
txt.Text = ddl.SelectedValue;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = e.Row.FindControl("DropDownList1") as DropDownList;
ddl.Attributes.Add("index", e.Row.RowIndex.ToString());
}
}
簡單說就是利用每個控制項都有的Attributes
用Attributes取個編號到時候你再取出當下這個DropDownList的編號就可以知道是那一列了
如果是Button可以用RowCommand的事件知道是哪個按鈕被按
印象中應該還有其他解決方式
有研究出來再來分享吧~
沒有留言:
張貼留言