给静态DataGrid动态添加列的问题

2005-08-03 by 开心就好

这个问题反复在论坛出现,其实这个问题在MSDN上早就有答案
1。英文版:Top Questions about the DataGrid Web Server Control(Mike Pope and Nikhil Kothari)
2。中文版:DataGrid Web 伺服器控制项的常见问题

可惜,论坛上的风气不太好,你即使给了连接,真正去看的人大概并不多

诀窍是,如果动态添加了列的话,需要在下一次PostBack时,在LoadViewState或更早把这些列重新添加。原因是,在Page类递归调用LoadViewState时,会调用DataGrid的CreateChildControls,而DataGrid的(实际上是它的父类的实现)CreateChildControls会调用DataGrid的CreateControlHierarchy()方法。在其中,DataGrid会根据当前的列的数目构造DataGridItem里的东西,然后从ViewState里恢复原来的数据。如果你没有重新添加你的动态列的话,你的动态列在PostBack后就会消失,更不用谈触发列里的控件的事件了

检验你的动态控件在PostBack后是否还在的一个方法是,加一个按钮看PostBack后的行为

下面是一个简单的测试

\<html>
\<body>
\<form runat="server">
\<asp:DataGrid id="DataGrid1" runat="server"
GridLines="Both" AutoGenerateColumns="false"
OnItemCommand="DataGrid1_ItemCommand">
 \<Columns>
 \<asp:ButtonColumn HeaderText="Static Button" Text="Click Me"
 CommandName="Static"/>
 \<asp:TemplateColumn HeaderText="Data">
  \<ItemTemplate>\<%#Container.DataItem%>\</ItemTemplate>
 \</asp:TemplateColumn>
 \</Columns>
\</asp:DataGrid>
\<asp:Button id="btnAddAColumn" runat="server" Text="Add a column" OnClick="AddButtonColumn"/>
\<asp:Button id="btnRefresh" runat="server" Text="Refresh" />
\</form>
\</body>
\</html>

void BindGrid()
{
 DataGrid1.DataSource = new string[] {"a","b","c"};
 DataGrid1.DataBind();
}

void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
 BindGrid();
  }
}

void DataGrid1_ItemCommand(Object sender, DataGridCommandEventArgs e)
{
 Response.Write("ItemCommand is called
");
 LinkButton btn = e.CommandSource as LinkButton;
 if (btn != null)
  Response.Write(String.Format("{0} is clicked on row {1}", btn.CommandName, e.Item.ItemIndex));

}

//lifted from the original post
public void CreateGridColumn(DataGrid OperationDataGrid)
{
   ButtonColumn NewButCol = new ButtonColumn() ;
   NewButCol.Text = "编辑" ;
   NewButCol.HeaderText = "操作" ;
   NewButCol.CommandName = "Edit" ;
   NewButCol.ButtonType = ButtonColumnType.LinkButton;
   NewButCol.Visible = true ;
   OperationDataGrid.Columns.Add(NewButCol) ;
   //OperationDataGrid.Columns.AddAt(1,NewButCol) ;
}

bool ButtonAdded
{
 get {
  object o = ViewState["ButtonAdded"];
  if (o == null)
   return false;
  else return (bool)o;
     }
 set { ViewState["ButtonAdded"] = value;}
}

void AddButtonColumn(object sender, EventArgs e)
{
 CreateGridColumn(DataGrid1);
 ButtonAdded = true;
 BindGrid();
 btnAddAColumn.Visible = false;
}

protected override void LoadViewState(object savedState)
{
 base.LoadViewState(savedState);

//在这里重新添加,假如已经添加的话
 if (ButtonAdded)
 CreateGridColumn(DataGrid1);
}


Comments