using System.Collections.Generic;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//必须放置在!IsPostBack中,否则会因为DropDownList的AutoPost而清空List
List<Mapping> mapping = new List<Mapping>();
ViewState["MAPPING"] = mapping;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl=(DropDownList)sender;
List<Mapping> mapping = (List<Mapping>)ViewState["MAPPING"];
var p = from u in mapping where u.ControlID==ddl.ClientID select u ;
//得到表的ID
//p.First().DatabaseID;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
List<Mapping> mapping = (List<Mapping>)ViewState["MAPPING"];
mapping.Add(new Mapping(e.Row.Cells[0].Text.Trim(),ddl.ClientID));
//假设e.Row.Cells[0].Text.Trim()是表记录的ID
//将DropDownList的ClientID和表的ID关联
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
List<Mapping> mapping = (List<Mapping>)ViewState["MAPPING"];
mapping.Clear();
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
List<Mapping> mapping = (List<Mapping>)ViewState["MAPPING"];
mapping.Clear();
}
}
[Serializable]
class Mapping
{
private string databaseID;
private string controlID;
public Mapping(string DatabaseID,string ControlID)
{
databaseID = DatabaseID;
controlID = ControlID;
}
public string DatabaseID { get{return databaseID; } }
public string ControlID { get { return controlID; } }
}