追加機能を備えたカスタムGridViewを作成しようとしていますが、「Button2」によって「シミュレート」されるPostBackに問題があります。すべてのPostBack(最初のDataBindingの後)で、GridViewが完全に空になるまで1つのDataRowのコンテンツが消去されます。これは、PreRenderイベント内のAddCustomControls()と関係があります。しかし、この問題を解決する方法がわかりません。誰かアイデアがありますか?どんな助けでも大歓迎です。
たぶん、下の画像は私の言葉よりも良い行動を説明しています。
OnLoadイベントのDataBind()とOnDataBoundイベントのAddCustomControlsを使用すると、非常にうまく機能しました。テーブルはPostBacks間でまったく同じままでした。しかし、グリッドの編集を実装しようとすると、この「ソリューション」(これもあまりエレガントではありません)を使用できなくなりました。論理的には、TextBox内の編集された値は、OnLoadのDataBind()によってオーバーライドされました。
どういうわけか、それは「headerTable.Rows.AddAt(1、controlRow);」と関係があります。「headerTable.Rows.Add(controlRow);」を実行すると これにより、すべてが正常に機能する最後に行が追加されます。したがって、この種のヘッダー行の後のコンテンツはクリアされます...
Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<pages>
<controls>
<add tagPrefix="asp" assembly="WebApplication11" namespace="WebApplication11"/>
</controls>
</pages>
</system.web>
</configuration>
Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:mightygridview
ID="mightyGridView"
runat="server"
AllowSorting="true"
AllowHiding="true"
AutoGenerateEditButton="false"
OnRowUpdating="mightyGridView_RowUpdating">
</asp:mightygridview>
<br />
<asp:Button ID="Button1" runat="server" Text="Button - Binding" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button - Nothing" />
</div>
</form>
</body>
</html>
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable tbl = new DataTable();
DataRow row;
tbl.Columns.Add("Spalte A");
tbl.Columns.Add("Spalte B");
tbl.Columns.Add("Spalte C");
row = tbl.NewRow();
row[0] = "Spalte A, Zeile 1";
row[1] = "Spalte B, Zeile 1";
row[2] = "Spalte C, Zeile 1";
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = "Spalte A, Zeile 2";
row[1] = "Spalte B, Zeile 2";
row[2] = "Spalte C, Zeile 2";
tbl.Rows.Add(row);
row = tbl.NewRow();
row[0] = "Spalte A, Zeile 3";
row[1] = "Spalte B, Zeile 3";
row[2] = "Spalte C, Zeile 3";
tbl.Rows.Add(row);
mightyGridView.DataSource = tbl;
mightyGridView.DataBind();
}
MightyGridView.cs
public class MightyGridView : GridView
{
public MightyGridView()
{
_images = new Dictionary<_imageTypes, string>();
_images.Add(_imageTypes.SortAsc, "data:;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAJCAYAAADpeqZqAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOwwAADsMBx2+oZAAAAAd0SU1FB9oGEAstLlCBjxcAAABWSURBVBjTY/z45TsDqYCFkYF0wMTAyMiADW/ese8/LjkmbCZt3r73PzKNYRMjAwMDMkZXuHn73v/oalBs2oTDZHRxJph2XBpQNELVMn759pOM0CMDAADRtiJL6bIYOQAAAABJRU5ErkJggg==");
_images.Add(_imageTypes.SortAscActive, "data:;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAJCAYAAADpeqZqAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oHDgckDaeYtVQAAAA1SURBVBjTY/z//z8DqYCJgQyAU5NK2Yb/JGmCacClkYmQDdg0MhHjJHRxJmL8gC7PSLcgBwAAMRqcsjcZIwAAAABJRU5ErkJggg==");
_images.Add(_imageTypes.SortDesc, "data:;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAJCAYAAADpeqZqAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOwwAADsMBx2+oZAAAAAd0SU1FB9oGEAstDmvvr98AAABhSURBVBjTY/zy7ScDqYCJgQzA8p/hPwMDAwPDpm17/xNS7OflzAix6T8DA8N/BgY/T4gATg2ezowwtUwYErg0IPsJqhmOfdEU+Ho6M6KrwRoQMI2+OGxm/PTlOzmhRzoAADvsH5oZ616EAAAAAElFTkSuQmCC");
_images.Add(_imageTypes.SortDescActive, "data:;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAJCAYAAADpeqZqAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oHDgckFi39fLgAAABBSURBVBjTY/z//z8DqYCJgQzAAmOolG0gaOWdrgBGFJtgAoQ0YDgPl0Z0cSZCCrAa9P//f6xYuXT9f1xyjHQLcgCoNDp3uWsOTwAAAABJRU5ErkJggg==");
_images.Add(_imageTypes.HideColumn, "data:;base64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAAJCAYAAADpeqZqAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAAOxAAADsQBlSsOGwAAAAd0SU1FB9oGEAsyFSzQaK0AAADHSURBVBjThZAxCoNQDIbTnsAbCM4eQfAC4uYstCdwFOd2dXNwFZzcxQtI3+bkrIKz4AW+Ln2l7WtpIJB8yf9DIoAAsq4rVVWx77topnMcR6ZpQvdHecS2bXJTSrIsYxgGNE/TFKWU2LZ90MxwvVwvBEFAXdd4nodSis8dQwTI6XTGsiz6vufb3ABd1+G6Lnme4/v+f9E8zziOQ1mWAFIUxbP+KYqiyHCP49gQPb8nIrIsi4Rh+IokSRJp25Y3+OrQNM3XGz7zDsbcHXO0jDwtAAAAAElFTkSuQmCC");
_images.Add(_imageTypes.ShowAllColumns, "data:;base64,iVBORw0KGgoAAAANSUhEUgAAAB4AAAAfCAIAAAB/DupQAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAYdSURBVEhL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2aoFWucmyrGU2VDzF2DG7iwGAK5v6SXtQJ6kRgqwarGuIguK5kdfUDDXwygmFiyrJqjyQfeYhYdEmMVrlRNyRYUCGQ+sAozbiojmmx+QLkPcUhOPoduc4bGbt57LrGRTt+AdT2xEbt/yOYZQGId9gDF5UZ6wQ5Hb5stYBK8BovbZO822gIGwa+z+HRmT4PRpXlt6grJClZO4XtRlFTodfeFC5UKs6pBr+AZYJLZamRYhdOiSQiQ3nQxlqJZTdJo6HZHqMLlLYYPnagw3MA2o8W8EjnJ7AxyH4jLKgwf9BYP+cu4cBncZ4ky+qK9n1YZFovrlRA8/XyVMeQvVjkvocJsNagPiI75K7Z0uGBxRS+46+hWR5+2dbxqRVINW2nleB+oZ1QJo8Wd4j6gEmhbClUTm9vsQHMfVpMFQq8VNEP0wtX/K8ZWoYf3F9eSXzUMYtzQX1iMGHmF4UOctRg+IEbuL9NbjK4VMJrqJmtgw9bBX8TIVk7rA48OMSbSUnnlEBpVGDFqaMZdYaOhm81XSDLpRR+oEtCLGLWVY1cYA38RI4+O58b7gBhh7rMEieXoSLyCvuRjXHYtGReDGEkyv+48tzTFqHGZXsotUUSM0QT0Mslx4kZiZTQaMhiedtAhHRTc/WZHb7vgM5qdVveAxYWt626z+U1Wr8Hs1Ld3GgW/weLVm926dmdLm6PZZG82CheMjkZjZ4PBXm+w1ettPzUJ6w3hP0jl604W2zyD7FRSf/lVQ27gxtj14XqRG5ReMHZWfFiJ5yF5eV210AOLUWG2GtRfumfOoLYa2umha4nENIsTrL/wN1FoNNgourIVMGQ5ll9vCK0GobVTSdshBKhnWtXoRMSTsNWprTJWo9yHep0ALtfLZ3m1jEZD+PXhP8y8D40GJ/ZNu5bsCgN9XmfjW4dKaLmtFc41WSPgxvPLTC+4z6Ev+Q4crdv72cm39325bWfNK2/seyFDv+i5XC4SiTx80R8w/wMqxePff8NAaAAAAABJRU5ErkJggg==");
}
private enum _imageTypes { SortAsc, SortAscActive, SortDesc, SortDescActive, HideColumn, ShowAllColumns }
private readonly Dictionary<_imageTypes, string> _images;
public new bool AllowSorting { get; set; }
public bool AllowHiding { get; set; }
public override object DataSource
{
get
{
return Page.Session[this.ID + ".DataSource"];
}
set
{
Page.Session[this.ID + ".DataSource"] = value;
base.DataSource = value;
}
}
private void AddCustomControlsRow()
{
if (DataSource != null)
{
if (AllowSorting || AllowHiding)
{
DataTable dataTable = GetDataTableFromDataSource();
if (dataTable.Columns.Count > 0 && dataTable.Rows.Count > 0)
{
if (this.HeaderRow != null && this.HeaderRow.Parent != null)
{
string sortExpression = dataTable.DefaultView.Sort.Replace(" ASC", "").Replace(" DESC", "");
SortDirection sortDirection = dataTable.DefaultView.Sort.EndsWith(" ASC") ? SortDirection.Ascending : SortDirection.Descending;
Table headerTable = (Table)this.HeaderRow.Parent;
GridViewRow controlRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
TableCell controlCell;
ImageButton imageButton;
foreach (TableCell cell in this.HeaderRow.Cells)
{
// Sicherstellen, dass es nicht die Spalte mit den Bearbeiten-Buttons ist, welche keine Überschrift enthält.
if (!(cell.Text.Replace(" ", "").Trim().Length > 0))
{
controlCell = new TableCell();
cell.Attributes.Add("style", "border-bottom: 0");
controlCell.Attributes.Add("style", "border-top: 0; text-align: center");
controlRow.Cells.Add(controlCell);
}
else
{
controlCell = new TableCell();
cell.Attributes.Add("style", "border-bottom: 0");
controlCell.Attributes.Add("style", "border-top: 0; text-align: center");
if (AllowSorting)
{
imageButton = new ImageButton();
imageButton.Attributes.Add("data-column-name", cell.Text);
imageButton.Click += new ImageClickEventHandler(SortColumnAscButton_Click);
if (sortExpression == cell.Text && sortDirection == SortDirection.Ascending)
imageButton.ImageUrl = _images[_imageTypes.SortAscActive];
else
imageButton.ImageUrl = _images[_imageTypes.SortAsc];
controlCell.Controls.Add(imageButton);
imageButton = new ImageButton();
imageButton.Attributes.Add("data-column-name", cell.Text);
imageButton.Click += new ImageClickEventHandler(SortColumnDescButton_Click);
if (sortExpression == cell.Text && sortDirection == SortDirection.Descending)
imageButton.ImageUrl = _images[_imageTypes.SortDescActive];
else
imageButton.ImageUrl = _images[_imageTypes.SortDesc];
controlCell.Controls.Add(imageButton);
controlRow.Cells.Add(controlCell);
}
if (AllowHiding)
{
imageButton = new ImageButton();
imageButton.Attributes.Add("data-column-name", cell.Text);
imageButton.Click += new ImageClickEventHandler(HideColumnButton_Click);
imageButton.ImageUrl = _images[_imageTypes.HideColumn];
controlCell.Controls.Add(imageButton);
controlRow.Cells.Add(controlCell);
}
}
}
headerTable.Rows.AddAt(1, controlRow);
}
}
}
}
}
public static DataTable GetDataTableFromIEnumerable(IEnumerable enumerable)
{
DataTable dataTable = new DataTable();
DataRow row;
PropertyInfo[] propertyInfos;
foreach (object obj in enumerable)
{
propertyInfos = obj.GetType().GetProperties();
if (dataTable.Columns.Count == 0)
foreach (PropertyInfo pi in propertyInfos)
dataTable.Columns.Add(pi.Name, pi.PropertyType);
row = dataTable.NewRow();
foreach (PropertyInfo pi in propertyInfos)
{
object value = pi.GetValue(obj, null);
row[pi.Name] = value;
}
dataTable.Rows.Add(row);
}
return dataTable;
}
private DataTable GetDataTableFromDataSource()
{
DataTable dataTable;
if (DataSource is DataTable)
dataTable = (DataTable)DataSource;
else if (DataSource is IEnumerable)
dataTable = GetDataTableFromIEnumerable((IEnumerable)DataSource);
else
throw new NotSupportedException("Typ wird nicht unterstützt.");
return dataTable;
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
// Weg der funktionierte, jedoch mit Editing Probleme macht: OnLoad: DataBind() / OnDataBound: AddCustomControls.
// Das DataBinding überschrieb die geänderten Werte wieder.
base.OnLoad(e);
}
protected override void OnDataBinding(EventArgs e)
{
base.OnDataBinding(e);
}
protected override void OnDataBound(EventArgs e)
{
base.OnDataBound(e);
}
protected override void OnPreRender(EventArgs e)
{
AddCustomControlsRow();
base.OnPreRender(e);
}
protected override void OnUnload(EventArgs e)
{
base.OnUnload(e);
}
protected void SortColumnAscButton_Click(object sender, ImageClickEventArgs e)
{
DataTable dataTable = GetDataTableFromDataSource();
dataTable.DefaultView.Sort = ((ImageButton)sender).Attributes["data-column-name"] + " ASC";
DataSource = dataTable;
DataBind();
}
protected void SortColumnDescButton_Click(object sender, ImageClickEventArgs e)
{
DataTable dataTable = GetDataTableFromDataSource();
dataTable.DefaultView.Sort = ((ImageButton)sender).Attributes["data-column-name"] + " DESC";
DataSource = dataTable;
DataBind();
}
protected void HideColumnButton_Click(object sender, ImageClickEventArgs e)
{
// todo: Improve. Backup zum resetten, etc.
DataTable dataTable = GetDataTableFromDataSource();
dataTable.Columns.Remove(((ImageButton)sender).Attributes["data-column-name"]);
DataSource = dataTable;
DataBind();
}