为什么我的HiddenField在第二次尝试实例化时失败?

时间:2015-07-21 17:19:42

标签: c# sharepoint-2010 pageload hidden-field

在我的网页上,几乎所有元素(分为面板/ div)都是在用户选择两个选项之一(显示或显示)后动态创建的。

表单提交后,表单将返回其原始的pristne / austere状态。因此,我必须在代码隐藏的Page_Load()事件中重新创建这些部分:

if (Page.IsPostBack) 
{
    GenerateSection1();
    GenerateSection2();
    GenerateSection3();
    GenerateSection4();
    GenerateSection5();
} 

然而,这在GenerateSection1()方法中失败,在下面显示的最后一行:

boxDeptDivSection1 = new TextBox
{
    CssClass = "finaff-webform-field-input",
    ID = "deptdivtextbox_MailStopRow"
};
if (!(String.IsNullOrEmpty(userDepartment)))
{
    boxDeptDivSection1.Text = userDepartment;
}
cellDeptDivTextbox.Controls.Add(boxDeptDivSection1);
dynamicTable.Rows.Add(row6);

HiddenField hfUserEmail = new HiddenField();
hfUserEmail.ID = "hfuseremail";
hfUserEmail.Value = usersEmail;
this.Controls.Add(hfUserEmail);

所以我可以添加"普通"元素的类型,但试图[重新]创建HiddenField导致它爆炸:

System.Web.HttpException未被用户代码处理   消息=无法加载视图状态。正在加载视图状态的控制树必须与在上一个请求期间用于保存视图状态的控制树匹配。例如,在动态添加控件时,在回发期间添加的控件必须与初始请求期间添加的控件的类型和位置相匹配。   来源=的System.Web   错误码= -2147467259   堆栈跟踪:        在System.Web.UI.Control.LoadViewStateRecursive(Object savedState)        在System.Web.UI.Control.AddedControl(控件控件,Int32索引)        在DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.GetSection1Table()        在DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.GenerateSection1()        at DirectPaymentSectionsWebPart.DPSVisualWebPart.DPSVisualWebPartUserControl.Page_Load(Object sender,EventArgs e)        在System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e)        在System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender,EventArgs e)        在System.Web.UI.Control.OnLoad(EventArgs e)        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Control.LoadRecursive()        在System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)   InnerException:

我怎样才能解决这个问题?

更新

也许问题在于自动生成的前导gobbledygook到元素的ID,正如您在下面看到的那样,最初是" ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_hfuseremail " (即使我为它分配了一个非常棒的ID" hfuseremail"

这些是" hfuseremail"的唯一引用。在"查看来源":

$(document).on("click", '[id$=rbPaymentForSelf]', function () {
    if (this.checked) {
        $('[id$=panelSection2]').slideUp();
        $('[id$=panelSection3]').slideUp();

        hide_sections();

        $('[id$=rbPaymentToIndividual]').attr('checked', true).click();     

        $('[id$=boxemailsection1]').val(function () {
            return $('[id$=hfuseremail]').val();
        })
    }
});


$(document).on("click", '[id$=rbCampusAddress]', function () {
    if (this.checked) {
        $('[id$=_MailStopRow]').slideDown();
        $('[id$=_AddressRows]').slideUp();
        $('[id$=boxemailsection1]').val(function () {
            return $('[id$=hfuseremail]').val();
        })
    }
});

<input type="hidden" name="ctl00$ctl24$g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33$ctl00$hfuseremail" id="ctl00_ctl24_g_5f3fedca_19f7_4bc3_b84e_efbef0c48a33_ctl00_hfuseremail" value="platypus@ucsc.edu" />

更新2

毕竟,它可能与HiddenField无关;我深入研究了Sharepoint的DOS-esque相关ID日志文件的肮脏内容,并发现:

System.Web.HttpException:具有相同ID的多个控件&gt; pdfgenbtnclicked&#39;被找到。 FindControl要求控件具有唯一ID。

更新3

这已经变成了一个真正的傻瓜问题,因为在添加代码以确保控件(pdfgenbtnclicked&#39;)没有重新添加之后,我得到:

...具有相同ID的多个控件&amp; panelSection1&#39; ...

不管它是否像海盗一样,我说,&#34; arrggghhhhhhhhhh !!!&#34;

......和&#34; Yo Ho Ho,还有一瓶啤酒!&#34;

更新4

好吧,我明白为什么Sharepoint不想重新创建与现有元素具有相同ID的元素(即使隐藏了那些现有元素) - ID可以说是唯一键。那么,在有条件地动态创建元素之后,然后提交表单,将表单/页面反转回初始状态,并希望再次显示这些元素,我的追索是什么?他们不存在/是空的,但没有&#34;释放&#34;他们的ID重复使用。我知道元素不再存在,因为我首先尝试将它们设置为&#34; Visible&#34;:

panelSec1.Visible = true; // this is null (as are the rest)
. . .

...但是崩溃了,因为他们(panelSec1,panelSec2等)是空的。

此时我唯一能想到的就是给他们一个随机的ID,例如:

panelSec1.ID = GetRandomVal() + "panelSection1";

...而不是现有的:

panelSec1.ID = "panelSection1";

思想?

2 个答案:

答案 0 :(得分:1)

还有其他事情发生了(您是否阅读了错误消息)?我的猜测是,代码中存在某种if条件导致控件树与渲染呈现略有不同,这会导致视图状态错误。

为什么动态生成隐藏字段?把它放在页面上。无论如何它都是隐藏的。如果你真的不希望它发出任何HTML,你可以禁用它

答案 1 :(得分:0)

似乎第二次重复使用的ID是问题。

相关问题