我在这里看了很多类似的问题,但无济于事。
所以这里......
我正在创建一组用户控件,用于显示类似网格的表列表。无论如何,目标是列出网格,弄乱每个网格数据(客户端)并按保存。网格效果很好,但是当我按下保存时,在VIEWER中的转发器总是空的。任何帮助表示赞赏!
基本布局就像这样
PAGE看起来像这样
<asp:Content ID="Content2" ContentPlaceHolderID="body" runat="server">
<asp:UpdatePanel id="udPanel" UpdateMode="Always" runat="server">
<Triggers>
</Triggers>
<ContentTemplate>
<uc:CommitImporterView ID="civData" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
public partial class CommitsAndActualsImporter : BaseForm
{
#region EVENTS
protected void Page_Init(object sender, EventArgs e)
{
setUpEventHandlers();
if (!IsPostBack)
initalize();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void myListOfGroups_DataBinding(object sender, EventArgs e)
{
//.. Code to bind the list goes here ...
myListOfGroups.SelectedIndex = setSelectedIndex(collection);
}
protected void myListOfGroups_SelectedIndexChanged(object sender, EventArgs e)
{
myGroup = myListOfGroups.Find(selectedItem => selectedItem.Key == key);
dataSource = buildViewDataSource(base.ProjectKey, base.AsOf, myGroup.FcrGroupKey, myGroup.ContractorKey, myGroup.WbsKey);
civData.AsOf = base.AsOf;
civData.ProjectKey = base.ProjectKey;
civData.DataSource = dataSource;
civData.DataBind();
}
#endregion
#region METHODS
private void initalize()
{
ContractorInviteItems dataSource = listGroups(ProjectKey, AsOf);
myListOfGroups.DataSource = dataSource;
myListOfGroups.DataBind();
}
private void setUpEvents()
{
myListOfGroups.DataBinding += new EventHandler(myListOfGroups_DataBinding);
myListOfGroups.SelectedIndexChanged += new EventHandler(myListOfGroups_SelectedIndexChanged);
}
#endregion
}
VIEWER用户控件看起来像这样
<%@ Register Src="~/UserControls/CommitImporterGrid.ascx" TagName="CommitImporterGrid" TagPrefix="uc" %>
<asp:Button ID="btnSave" runat="server" Text="Save" />
<asp:HiddenField ID="hidAsOf" runat="server" Value="" />
<asp:HiddenField ID="hidProjectKey" runat="server" Value="0" />
<asp:Repeater ID="repCommitImporterView" runat="server">
<ItemTemplate>
<uc:CommitImporterGrid runat="server" />
</ItemTemplate>
</asp:Repeater>
public partial class CommitImporterView : System.Web.UI.UserControl
{
#region PROPERTIES & FIELDS
private enum basisUsed
{
Dollars,
Hours,
IsInvalid
}
public Object DataSource { get; set; }
public DateTime AsOf { get { return Convert.ToDateTime(hidAsOf.Value); } set { hidAsOf.Value = value.ToString(); } }
public Int32 ProjectKey { get { return Convert.ToInt32(hidProjectKey.Value); } set { hidProjectKey.Value = value.ToString(); } }
#endregion
#region EVENTS
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
setupEvents();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.DataBind();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
public override void DataBind()
{
base.DataBind();
}
protected void btnSave_Click(object sender, EventArgs e)
{
Int32 count = repCommitImporterView.Controls.Count;
foreach (Control control in repCommitImporterView.Controls)
{
if (control is UserControls.CommitImporterGrid)
{
string stop = "";
}
}
}
protected void repCommitImporterView_DataBinding(object sender, EventArgs e)
{
if (DataSource == null)
return;
if (!IsPostBack)
{
Decimal totalSummaryCbudDollars = 0;
Decimal totalSummaryCbudHours = 0;
if (summaryValuesExist(out totalSummaryCbudDollars, out totalSummaryCbudHours))
alignSummarizedRecords(totalSummaryCbudDollars, totalSummaryCbudHours);
repCommitImporterView.DataSource = DataSource;
}
}
protected void repCommitImporterView_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
ContractorInviteCommitsAndActualsImport record = (ContractorInviteCommitsAndActualsImport)e.Item.DataItem;
CommitImporterGrid grid = new CommitImporterGrid();
grid = (UserControls.CommitImporterGrid)Page.LoadControl("~/UserControls/CommitImporterGrid.ascx") as UserControls.CommitImporterGrid;
grid.AsOf = this.AsOf;
grid.ProjectKey = this.ProjectKey;
grid.DataSource = getDataSource(record);
grid.DataBind();
repCommitImporterView.Controls.Add(grid);
}
#endregion
#region METHODS
private void setupEvents()
{
btnSave.Click += new EventHandler(btnSave_Click);
repCommitImporterView.DataBinding += new EventHandler(repCommitImporterView_DataBinding);
repCommitImporterView.ItemDataBound += new RepeaterItemEventHandler(repCommitImporterView_ItemDataBound);
}
#endregion
}
GRID用户控件看起来像
答案 0 :(得分:1)
当用户更改DropDownList时,您似乎只绑定了Viewer控件,并且在页面/控件加载时,无论是在初始加载还是在回发时,它都没有被绑定。如果你没有在回发时绑定转发器,它将没有你期望的任何数据项。
我会将myListOfGroups_SelectedIndexChanged
的内容提取到私有方法中,然后从myListOfGroups_SelectedIndexChanged
调用它,然后从Page_Load
调用它,而不管IsPostBack
的状态如何(确保在数据绑定myListOfGroups
之后调用它。当你从Page_Load
调用它时,它将从DropDownList中获取用户的选择并重新绑定绑定Repeater的Viewer控件。
这里的关键点是,无论IsPostBack
的值如何,您始终需要在每个页面加载时绑定转发器。否则转发器中什么都没有。