简体   繁体   English

读取自定义用户控件状态

[英]Read custom user control state

I add a custom user control to my page, with textboxes and radioboxes, but after each postback, the contents are removed and I don't know how to read the values that were entered in it. 我添加了一个自定义用户控件到我的页面,文本框和radioboxes,但每次回发后,内容被删除,我不知道如何读取其中输入的值。

From what I've learned you have to add the user controls on each refresh, but that does not restore their state. 根据我的经验,您必须在每次刷新时添加用户控件,但这不会恢复其状态。 I want the values to be stored in the ViewState but that does not happen for some reason. 我希望将值存储在ViewState中,但由于某种原因不会发生这种情况。

Note: I need to add multiple user controls on one page, so I need to identify each user control, either trough an ID or a linq expression that selects that user control from my page. 注意:我需要在一个页面上添加多个用户控件,因此我需要通过ID或从我的页面中选择该用户控件的linq表达式来标识每个用户控件。

Im programming with C# 4.0 我用C#4.0编程

How I add my user control: 我如何添加我的用户控件:

    private void AddInstrumentDetailToPage()
    {
        RMAItem lItem = (RMAItem)Page.LoadControl("/Controls/RMAItem.ascx");
        InstrumentDetail.Controls.Add(lItem);
    }

I save the amount of controls I have in the ViewState, and call AddInstrumentDetail() that amount of times, but they appear to be empty. 我保存了ViewState中的控件数量,并调用了AddInstrumentDetail()那么多次,但它们看起来都是空的。 I've read somewhere that I have to add it in Page_Init because view state is not initialised yet, but that doesn't help either. 我已经读过某个地方,我必须在Page_Init中添加它,因为视图状态尚未初始化,但这也无济于事。

You need to recreate dynamically added (User-)Controls at latest in Page_Load to maintain ViewState. 您需要最近在Page_Load重新创建动态添加的(用户)控件以维护ViewState。 So you should store the number of already added controls in a ViewState-Property and according to that reload them in Page_Load . 因此,您应该将已添加的控件的数量存储在ViewState-Property中,并根据它在Page_Load中重新加载它们。 You have to sum up this variable with 1 in AddInstrumentDetailToPage . 您必须在AddInstrumentDetailToPage将此变量与1 AddInstrumentDetailToPage

MSDN: Dynamically Adding User Controls MSDN:动态添加用户控件

If you store the number of added controls in a Viewstate variable, you cannot recreate controls in Page's Init-Event because the ViewState-Variable would yet not been reloaded there. 如果在Viewstate变量中存储已添加控件的数量,则无法在Page的Init-Event中重新创建控件,因为ViewState-Variable尚未在那里重新加载。

You won't have any ControlState unless you make a PostBack . 除非您进行PostBack否则您将没有任何ControlState Refreshing your browser/requesting the same link will just be a fresh start. 刷新浏览器/请求相同的链接将是一个全新的开始。

By default, any control added to your page are data stored in ViewState and ControlState for your UserControl . 默认情况下,添加到页面的任何control都是存储在UserControl ViewStateControlState中的数据。

If you are willing to save your contents, I would suggest you save it on Request.Session.Add("yourKey", "yourValue") . 如果您愿意保存您的内容,我建议您将其保存在Request.Session.Add("yourKey", "yourValue")

You can access your session with var yourValue = Request.Session["yourKey"]; 您可以使用var yourValue = Request.Session["yourKey"];访问您的会话var yourValue = Request.Session["yourKey"]; on your protected void Page_Load(object sender, EventArgs e) method. protected void Page_Load(object sender, EventArgs e)方法上。

Specify an ID for your RMAItem instance, otherwise storing data on ControlState might not work! 指定RMAItem实例的ID,否则在ControlState上存储数据可能不起作用!

private void AddInstrumentDetailToPage()
{
    RMAItem lItem = (RMAItem)Page.LoadControl("/Controls/RMAItem.ascx");
    lItem.ID = "rmaItem1"; //<-- This is important
    InstrumentDetail.Controls.Add(lItem);
}

Hope it helps! 希望能帮助到你!

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

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