Asp.net隐藏字段后面的代码没有值,但*在回发后是*保留值

时间:2022-08-25 21:49:03

In my ASCX, I have an asp.net hidden field defined as <asp:HiddenField ID="hdnNewAsset" runat="server" />.

在我的ASCX中,我有一个asp.net隐藏字段,定义为

In the Code Behind I have the following code:

在Code Behind中我有以下代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        _service = new ArticleDataService(PortalId);

        if (!IsPostBack)
        {
            string rawId = Request[ArticleQueryParams.ArticleId];
            DisplayArticleDetails(rawId);
        }

        if (hdnNewAsset.Value.Trim() != string.Empty)
            ProcessNewAsset();
    }

Now, in my frontend, I have a javascript function to react to an event and set the hidden field and trigger a postback:

现在,在我的前端,我有一个javascript函数来响应事件并设置隐藏字段并触发回发:

function assetSelected(assetGuid) {
    $('input[id*="hdnNewAsset"]').val(assetGuid);
    __doPostBack()
}

What's happening is that my hidden field is being set in the markup (chrome shows [ <input type=​"hidden" name=​"dnn$ctr466$Main$ctl00$hdnNewAsset" id=​"dnn_ctr466_Main_ctl00_hdnNewAsset" value=​"98d88e72-088c-40a4-9022-565a53dc33c4">​ ] for $('input[id*="hdnNewAsset"]')).

发生的事情是我的隐藏字段正在标记中设置(chrome显示[] for $('input [id * =”hdnNewAsset“]'))。

However, when the postback occurs, hdnNewAsset.Value is an empty string.

但是,当发生回发时,hdnNewAsset.Value是一个空字符串。

What's even more puzzling is that at the beginning of Page_Load Request.Params["dnn$ctr466$Main$ctl00$hdnNewAsset"] shows 98d88e72-088c-40a4-9022-565a53dc33c4, and after the postback my hidden field has the same value (so the hidden field is persisting across postbacks), yet I cannot access this value via hdnNewAsset.Value.

更令人费解的是,在Page_Load Request.Params [“dnn $ ctr466 $ Main $ ctl00 $ hdnNewAsset”]的开头显示了98d88e72-088c-40a4-9022-565a53dc33c4,并且在回发后我的隐藏字段具有相同的值(所以隐藏字段在回发中持续存在),但我无法通过hdnNewAsset.Value访问此值。

Can anyone see what I"m doing wrong?

谁能看到我做错了什么?

1 个解决方案

#1


0  

Ok I figured out the the issue.

好的我弄明白了这个问题。

The issue is that the code posted above was part of an ASCX user control. That user control was being loaded dynamically into an asp.net placeholder during the Page_Load event of the parent control.

问题是上面发布的代码是ASCX用户控件的一部分。在父控件的Page_Load事件期间,该用户控件被动态加载到asp.net占位符中。

Therefore, it seems that since both of these calls were in Page_Load of their respective calls, the inner control did not have it's values bound in the inner control's page_load. Modifying it so my inner control is loaded in Page_Init instead of Page_Load fixed all bindings.

因此,似乎因为这两个调用都在它们各自调用的Page_Load中,所以内部控件没有在内部控件的page_load中绑定它的值。修改它,以便我的内部控件加载到Page_Init而不是Page_Load修复所有绑定。

Not sure if I wrote that in a way that makes sense to the general public.

不确定我是否以对公众有意义的方式写出来。


Edit: It seems this part of the MSDN documentation is relevant to my issue:

If controls are created dynamically at run time or declaratively within templates of data-bound controls, their events are initially not synchronized with those of other controls on the page. For example, for a control that is added at run time, the Init and Load events might occur much later in the page life cycle than the same events for controls created declaratively. Therefore, from the time that they are instantiated, dynamically added controls and controls in templates raise their events one after the other until they have caught up to the event during which it was added to the Controls collection.

如果控件是在运行时动态创建的,或者在数据绑定控件的模板中以声明方式创建,则它们的事件最初不会与页面上其他控件的事件同步。例如,对于在运行时添加的控件,Init和Load事件可能在页面生命周期的后期发生,而不是以声明方式创建的控件的相同事件。因此,从实例化它们开始,模板中动态添加的控件和控件会一个接一个地引发它们的事件,直到它们赶上将其添加到Controls集合中的事件。

#1


0  

Ok I figured out the the issue.

好的我弄明白了这个问题。

The issue is that the code posted above was part of an ASCX user control. That user control was being loaded dynamically into an asp.net placeholder during the Page_Load event of the parent control.

问题是上面发布的代码是ASCX用户控件的一部分。在父控件的Page_Load事件期间,该用户控件被动态加载到asp.net占位符中。

Therefore, it seems that since both of these calls were in Page_Load of their respective calls, the inner control did not have it's values bound in the inner control's page_load. Modifying it so my inner control is loaded in Page_Init instead of Page_Load fixed all bindings.

因此,似乎因为这两个调用都在它们各自调用的Page_Load中,所以内部控件没有在内部控件的page_load中绑定它的值。修改它,以便我的内部控件加载到Page_Init而不是Page_Load修复所有绑定。

Not sure if I wrote that in a way that makes sense to the general public.

不确定我是否以对公众有意义的方式写出来。


Edit: It seems this part of the MSDN documentation is relevant to my issue:

If controls are created dynamically at run time or declaratively within templates of data-bound controls, their events are initially not synchronized with those of other controls on the page. For example, for a control that is added at run time, the Init and Load events might occur much later in the page life cycle than the same events for controls created declaratively. Therefore, from the time that they are instantiated, dynamically added controls and controls in templates raise their events one after the other until they have caught up to the event during which it was added to the Controls collection.

如果控件是在运行时动态创建的,或者在数据绑定控件的模板中以声明方式创建,则它们的事件最初不会与页面上其他控件的事件同步。例如,对于在运行时添加的控件,Init和Load事件可能在页面生命周期的后期发生,而不是以声明方式创建的控件的相同事件。因此,从实例化它们开始,模板中动态添加的控件和控件会一个接一个地引发它们的事件,直到它们赶上将其添加到Controls集合中的事件。