English 中文(简体)
SiteFinity 4.0: Trouble Accessing Postback Data in Custom User Control
原标题:

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

最佳回答

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.

问题回答

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.

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);
}

`





相关问题
Prevent Postback when user clicks browser s back button

I have a web page that sends email to multiple users (online distribution list). After the submit button is clicked and the email is sent, a status page is shown listing how many emails were sent, ...

An update panel, a postback and jQuery

An update panel, a postback and jQuery. Sounds like a bad joke, but here s my situation. I ve got two grids wrapped up in a MS update panel. The grids each have buttons in them that cause postback ...

ASP.NET - Control Events Not Firing Inside Repeater

This is a absurdly common issue and having exhausted all of the obvious solutions, I m hoping SO can offer me some input... I have a UserControl inside a page which contains a repeater housing several ...

How do I make a Textbox Postback on KeyUp?

I have a Textbox that changes the content of a dropdown in the OnTextChanged event. This event seems to fire when the textbox loses focus. How do I make this happen on the keypress or keyup event? ...

Postback after JQuery Dialog box closes causes js error in IE

I am creating JQuery dialog boxes and need the close functionality to cause a postback to the parent form (so that data changed in the popup form displays immediately on the parent screen). I have ...

How Do I Get a Dynamic Control s Value after Postback?

I have a listview that adds controls in the ItemDataBound Event. When the postback occurs, I cannot find the new controls. After a bit of research, I found that ASP .NET needs these controls created ...

ASP.Net s auto-postback. What happens when its too slow?

I am making a web application. I have gotten a weird error with update panels. Ok, so say you have two update panels and each update panel has a textbox in it. Both of these textboxes are auto-...

热门标签