我一直在寻找问题的答案,但是什么都没找到,所以请您帮忙!
我有一个页面,该页面可以具有多个网格视图,并根据需求动态创建,并作为子控件附加到服务器端div。
ASPX:
<div ID="DivStreams" runat="server">
</div>
后面的代码:
// gridview
GridView gv = new GridView
{
ID = "gridFiles_" + stream.Part,
CssClass = "gridview",
EmptyDataText = "There are no files in this stream",
AutoGenerateColumns = false,
DataKeyNames = new string[] { "FileID", "PrismPart" },
ShowFooter = true
};
gv.RowDataBound += gridFiles_RowDataBound;
-- more css styling here, removed to shorten message --
// gridview columns
gv.Columns.Clear();
gv.Columns.Add(new BoundField() { HeaderText = "Print Filename", DataField = "PrintFilename" });
gv.Columns.Add(new BoundField() { HeaderText = "Envelopes", DataField = "Envelopes" });
gv.Columns.Add(new BoundField() { HeaderText = "Sheets", DataField = "Sheets" });
gv.Columns.Add(new BoundField() { HeaderText = "Status", DataField = "Status" });
gv.Columns.Add(new TemplateField() { ItemTemplate = new GridDmChangeStream(), HeaderText = "Change Stream" });
DivStreams.Controls.Add(gv);
// bind
gv.DataSource = stream.Files;
gv.DataBind();
protected void gridFiles_RowDataBound(object sender, GridViewRowEventArgs e)
{
List<DmStream> streams = ((List<DmStream>)ViewState["streams"]);
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlPrismPart");
Dictionary<int, string> partsAndNames = new Dictionary<int, string>();
partsAndNames.Add(0, "");
foreach (DmStream stream in streams)
{
if (!stream.Locked && stream.Part != 0 && e.Row.NamingContainer.ID != "gridFiles_" + stream.Part)
partsAndNames.Add(stream.StreamID, "Part " + stream.Part + " - " + stream.StreamName);
}
partsAndNames.Add(-1, "Cancel File");
ddl.DataSource = partsAndNames;
ddl.DataBind();
if (e.Row.NamingContainer.ID == "gridFiles_0")
{
ddl.CssClass += " missingDetails";
btnManageJob.Enabled = false;
}
ddl.SelectedIndexChanged += ddlChangeStream_SelectedIndexChanged;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
foreach (DmStream stream in streams)
{
if (e.Row.NamingContainer.ID == "gridFiles_" + stream.Part)
{
e.Row.Cells[0].Text = "Totals";
e.Row.Cells[1].Text = stream.Files.Sum(o => o.Envelopes).ToString();
e.Row.Cells[2].Text = stream.Files.Sum(o => o.Sheets).ToString();
}
}
}
}
class GridDmChangeStream : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
DropDownList ddl = new DropDownList
{
ID = "ddlPrismPart",
CssClass = "form-control",
DataValueField = "Key",
DataTextField = "Value",
AutoPostBack = true
};
container.Controls.Add(ddl);
}
}
这很好,但是现在我需要在上面的动态网格中添加一个复选框列。
gv.Columns.Add(new CheckBoxField() { HeaderText = "Change Stream", DataField="Selected" });
因此,用户将选择所需的复选框,然后单击一个按钮(仍要添加-它可能位于网格页脚中,并且仅适用于同一网格中的复选框)。
但是当通过单击新按钮进行回发时,如何在服务器端打勾呢?
如果它是静态网格,我知道该怎么做,但是有很多动态网格,我不确定该怎么做?
我认为可能可以做一个client-onclick事件,即调用一些JS,以遍历网格,构建一个带分隔符的复选框的复选框,然后手动调用__doPostbackAction,并在服务器端拦截该调用,解析定界的字符串,然后采取相应措施。
但我希望有更好/更简便的方法?
谢谢!
PS,我对此并不陌生,所以请提供示例和清晰的解释,以便我的简单头脑可以理解:)谢谢