简体   繁体   English

从用户控件保存值

[英]Saving values from User Control

I have an aspx page. 我有一个aspx页面。 On pageLoad a call a method that loads a user control pageLoad通话加载用户控件的方法

protected void Page_Load(object sender, EventArgs e)
{
    LoadUC();
}

This loads the user control onto the page (into a placeholder) passing a few generic values. 这将通过几个通用值将用户控件加载到页面上(放入占位符中)。

private void LoadUC()
{
    ucTS ctrl = (ucTS)Page.LoadControl(_ucTSPath);
    ctrl.ParentId = 0;
    ctrl.addNew = false;
    phFG.Controls.Add(ctrl);
}

The user control (which contains a repeater) loads the initial placeholder as well as another placeholder on the initial page: 用户控件(包含转发器)将在初始页面上加载初始占位符以及另一个占位符:

protected void Page_Load(object sender, EventArgs e)
{
    LoadItems();
}

private void LoadBOMItems()
{
    List<Item> dtItem;

    if (ParentId == 0)
    {
        if (ViewState["ItemFG"] == null)
        {
            dtItem = //Gets list of items
        }
        else
        {
            dtItem = (List<Item>)ViewState["ItemFG"];
        }
        ViewState["ItemFG"] = dtItem;
    }
    else
    {
        if (ViewState["Items" + ParentId] == null)
        {
            dtItem = //get list of items
        }
        else
        {
            dtItem = (List<Item>)ViewState["Items" + ParentId];
        }
        ViewState["Items" + ParentId] = dtItem;
    }

    if (dtItem.Count > 0)

        rptTSItem.DataSource = dtItem;
        rptTSItem.DataBind();              
    }
}

in the binding, I bind the repeater, but I am also adding more of the same user control. 在绑定中,我绑定了转发器,但我还要添加更多相同的用户控件。

The problem comes when I click add a new item to the repeater. 当我单击将新项目添加到转发器时,问题就来了。 The initial click, everything saves fine and a new row is added. 初始单击,一切都保存良好,并添加了新行。 The second click the user control is not found on the initial page and so the save method is not fires. 在初始页上找不到用户控件的第二次单击,因此不会触发save方法。 The 3rd click, everything is fine, the 4th, the user control is not found. 第三次单击,一切正常,第四次,未找到用户控件。 This keeps happening. 这一直在发生。 Why is my usercontrol not always found? 为什么不能总是找到我的用户控件? I have tried doing a postback check in multiple places, but that doesn't seem to work. 我曾尝试在多个地方进行回发检查,但这似乎不起作用。

I am still not 100% sure why the event would fire properly every OTHER time as opposed to just the first time, but the solution was simply adding an ID to the user control. 我仍然不是100%知道为什么该事件每隔其他一次就会正确触发,而不是第一次触发,但是解决方案只是在用户控件中添加一个ID。

private void LoadUC()
{
ucTS ctrl = (ucTS)Page.LoadControl(_ucTSPath);
ctrl.ParentId = 0;
ctrl.addNew = false;
ctrl.ID = "someID"
phFG.Controls.Add(ctrl);
}

and whenever I re-added the user control from within the user control, I pass the same ID. 每当我从用户控件中重新添加用户控件时,我都会传递相同的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM