SiteFinity 4.0:在自定义用户控件中访问回发数据时遇到问题

发布于 2024-10-16 02:29:46 字数 2020 浏览 2 评论 0原文

我正在尝试将高度定制的手工构建表单放入 sitefinity 4.0 中,但我的问题是,无论我做什么,我都无法在后面的代码中访问表单的回发数据。我的表单是一个用户控件,我已按照此处描述的方式添加它: http://www.sitefinity.com /40/help/developer-manual/controls-adding-a-new-control.html

经过几个小时的努力,我创建了一个基本的测试表单,但仍然无法访问回发数据。我还尝试在各处添加 EnableViewState="true" 但表单数据在回发时仍然为空。完全相同的用户控件完全在 sitefinity 之外运行和发布数据。我还尝试了其他访问回发数据的方法,我发现 Request.Form 确实包含我需要的数据。不过,我仍然喜欢以通常的方式访问我的表单元素,因此我不必为页面上的每个控件执行 Request.Form 并以这种方式循环,这看起来确实很做作。

以下是基本表单的代码:

"BasicUserControl.ascx"

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BasicUserControl.ascx.cs" Inherits="SitefinityWebApp.UserControls.Assessments.BasicUserControl" EnableViewState="true" %>
<div id="assessmentDiv" runat="server">
    <asp:TextBox ID="TextBox1" runat="server" clientidmode="Static" enableviewstate="true"></asp:TextBox>
    <asp:Literal ID="Literal1" runat="server" clientidmode="Static" enableviewstate="true"></asp:Literal>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</div>

"BasicUserControl.ascx.cs" 代码隐藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SitefinityWebApp.UserControls.Assessments
{
    public partial class BasicUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Literal1.Text = TextBox1.Text;
            }
        }
    }
}

再次,如果我通过上面链接中描述的方法添加控件,我就能够成功创建 Sitefinity 4.0 CMS 页面,将控件拖到其上,运行页面,使用调试器单步执行到后面的代码,但是当 VS2010 到达下面的行时,没有发布任何表单数据:

Literal1.Text = TextBox1.Text;

仅供参考:上面的 usercontrol.ascx 代码中没有表单标记的原因是,通过 sitefinity 运行表单时出现错误,.net 页面上只能存在一个服务器端表单标记(sitefinity 注入它自己的表单)标签)。

预先感谢您的帮助!

I'm trying to get a highly customized hand built form into sitefinity 4.0 and my problem is that no matter what I do I can't access the form's postback data in the code behind. My form is a user control and I've added it in the way described here:
http://www.sitefinity.com/40/help/developer-manual/controls-adding-a-new-control.html

After struggling for several hours, I created a basic test form and I'm still not able to access the postback data. I've also tried adding EnableViewState="true" all over the place but the form data is still empty on postback. The exact same user control runs and posts data perfectly outside of sitefinity. I also tried other methods of accessing the postback data and I discovered that Request.Form does contain the data I need. I'd still love to access my form elements in the usual way though, so I don't have to do Request.Form for every control on the page and loop that way, which seems really hokey.

Here's the code for the basic form:

"BasicUserControl.ascx"

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="BasicUserControl.ascx.cs" Inherits="SitefinityWebApp.UserControls.Assessments.BasicUserControl" EnableViewState="true" %>
<div id="assessmentDiv" runat="server">
    <asp:TextBox ID="TextBox1" runat="server" clientidmode="Static" enableviewstate="true"></asp:TextBox>
    <asp:Literal ID="Literal1" runat="server" clientidmode="Static" enableviewstate="true"></asp:Literal>
    <asp:Button ID="Button1" runat="server" Text="Button" />
</div>

"BasicUserControl.ascx.cs" Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SitefinityWebApp.UserControls.Assessments
{
    public partial class BasicUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Literal1.Text = TextBox1.Text;
            }
        }
    }
}

Again, if I add the control via the method described at the link above, I am able to successfully create a Sitefinity 4.0 CMS page, drag the control onto it, run the page, step into the code behind using the debugger, yet when VS2010 reaches the line below there is no form data being posted:

Literal1.Text = TextBox1.Text;

FYI: The reason why there is no form tag in my usercontrol.ascx code above is because I get an error when running the form thru sitefinity that only one server-side form tag can exist on a .net page (sitefinity injects it's own form tag).

Thanks in advance for your help!

Ben

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如何视而不见 2024-10-23 02:29:46

没关系——我想通了。由于某种原因,在 sitefinity 表单提交中,数据在 .net 页面生命周期的 Page_Load 阶段不可用(至少不能通过自定义用户控件)。如果我等到 Page_PreRender 阶段从页面上的表单检索数据,那么一切都在那里。

我目前流行的理论是,Sitefinity 4.0 在表单提交时获取回发数据,并且在 Page_Load 阶段还没有完成对它的处理,因此您必须等到 Page_PreRender 阶段,然后 sitefinity 才能将数据注入回页面周期。

Never mind - I figured it out. For some reason, data isn't available at the Page_Load stage of the .net page lifecycle in a sitefinity form submission (at least not via a custom user control). If I wait until the Page_PreRender stage to retrieve the data from the form on the page, it's all there.

My current prevailing theory is that Sitefinity 4.0 grabs the postback data when a form submits and hasn't finished monkeying around with it at the Page_Load stage, so you have to wait until the Page_PreRender stage before sitefinity has injected the data back into the page cycle.

浅忆 2024-10-23 02:29:46

我遇到了与您相同的问题并得出了相同的解决方案。花了很多时间在上面 - 希望这个答案当时就存在。

I had the same issue you did and came to the same solution. Spent a bunch of time on it - wish this answer had been around then.

凉宸 2024-10-23 02:29:46

尝试将enableviewstate添加到母版页中。我遇到了同样的情况并解决了这个问题。
创建页面时还检查了“启用视图状态”。

希望这有帮助。

`<%@ Master Language="C#" AutoEventWireup="true" CodeFile="HomePageClubManavgat.master.cs" Inherits="App_Master_HomePageClubManavgat" EnableViewState="true" %>

<script type="text/C#" runat="server">
protected override void OnInit(EventArgs e)
{
    this.Page.EnableViewState = true;
    base.OnInit(e);
}

`

Try adding enableviewstate into masterpages. I had the same situation and solved with this.
Also checked "enable view state" when creating pages.

Hope this help.

`<%@ Master Language="C#" AutoEventWireup="true" CodeFile="HomePageClubManavgat.master.cs" Inherits="App_Master_HomePageClubManavgat" EnableViewState="true" %>

<script type="text/C#" runat="server">
protected override void OnInit(EventArgs e)
{
    this.Page.EnableViewState = true;
    base.OnInit(e);
}

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文