如何从隐藏的字段中继器获取值而不通过无效的回发

时间:2016-09-18 14:54:43

标签: c# file-io webforms

我正在尝试使用内联pdf浏览器,但是当我调用代码时,它会显示以下错误

  

invvalid postback或callback参数。使用配置或<%@ Page EnableEventValidation =" true"启用事件验证。 %GT;在一个页面中。出于安全考虑,此功能可验证回发或回调事件的参数是否来自最初呈现它们的服务器控件。如果数据有效且符合预期,请使用ClientScriptManager.RegisterForEventValidation方法注册回发或回调数据以进行验证。

我在大多数表单上看到它建议不要禁用上面的内容并使用RegisterForEventValidation但我的问题是如何启用如果对于转发器内的按钮我已经尝试了以下但是它不是我的按钮方法

protected override void Render(HtmlTextWriter writer)
{
    Page.ClientScript.RegisterForEventValidation(btnViewDocumentBrowser_Click);
    base.Render(writer);
}

这是在浏览器中呈现导致上述异常的pdf的代码

protected void btnDownloadDocument_Click(object sender, EventArgs e)
{
    HiddenField hf = (HiddenField)rptDocumentsProposal.FindControl("currentFileName");

    if (hf != null)
    {
        string filename = hf.Value.ToString();
        Response.Clear();
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attach;filename=" + filename);
        Response.TransmitFile(filename);
        Response.End();
    }
}

转发器代码

<asp:Repeater ID="rptDocumentsProposal" runat="server">
    <ItemTemplate>
        <div class="col-md-6 sidesthick">
            <div class="windowbox">
                <div class="pull-left">
                    <h2><asp:Label runat="server" Text='<%# Eval("documentTitle") %>' /></h2>
                </div>
                <div class="pull-right icon-doc">
                    <asp:Label ID="IconDocument" runat="server" />
                </div>
                <div style="clear: both"></div>
                <div>
                    <p>
                        Name:
                        <asp:Label runat="server" Text='<%# Eval("documentTitle") %>' />
                        <br /> Description:
                        <asp:Label runat="server" Text='<%# Eval("documentDescription") %>' />
                        <br /> Type:
                        <asp:Label ID="lblType" runat="server" Text='<%# Eval("type") %>' />
                        <asp:HiddenField ID="currentFileName" Value='<%# DataBinder.Eval(Container.DataItem, "serverPath") %>' runat="server" />
                        <br />
                    </p>
                </div>
                <div class="center">
                    <asp:Button ID="btnDownloadDocument" CssClass="btn btn-default" OnClick="btnDownloadDocument_Click" runat="server" Text="Download Document" />
                    <asp:Button ID="btnViewDocumentBrowser" CommandArgument='<%# Eval("serverPath") %>' CssClass="btn btn-default" OnClick="btnViewDocumentBrowser_Click" runat="server" Text="View In Browser" />
                </div>
            </div>
        </div>
    </ItemTemplate>
</asp:Repeater>

该位似乎导致问题是HiddenField当我试图找到它时找不到正确的文件名,即使在该转发器中填充了一个

1 个答案:

答案 0 :(得分:0)

你无法让Repeater内的项目变得如此简单,你需要这样做:

foreach (RepeaterItem item in rptDocumentsProposal.Items)
{
    var hf = (HiddenField)item.FindControl("currentFileName");
    // now you access to hf.Value
    // btw please you hfCurrentFileName instead of just currentFileName as your HiddenField name :)
}

所以,那就是发现HiddenField问题。

为了处理Button's click,我建议使用Repeater's OnItemCommand event代替Button's OnClick event。使用OnItemCommand非常简单,您可以将OnClick event normal used Buttonsinside Repeater used Buttons分开(希望您明白)。

所以,这是代码:

您需要将OnItemCommand添加到转发器,如下所示:

<asp:Repeater ID="rptDocumentsProposal" runat="server"
    OnItemCommand="rptDocumentsProposal_ItemCommand">

CommandName&amp; CommandArgument Button,如下所示:

<asp:Button ID="btnDownloadDocument" runat="server"
    CommandArgument='<%# Eval("SOME_VALUE") %>' CommandName="Download" />

最后 C#

protected void rptDocumentsProposal_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandArgument == null)
        return;

    // I prefer to use switch/case here to handle more Buttons click easily
    // feel free to use if/else instead of switch/case
    switch (e.CommandName)
    {
        case "Download":
            // deal with the Button click here
            break;

        default:
            // hello !!, there is nothing to do with
            break;
    }
}

顺便说一句,当我尝试使用ASP controls更改JS值并将其发送回服务器时,我收到了您提到的错误。

我在共享代码中没有看到这样的内容,所以我认为你会以这种方式获胜。:)

尝试此解决方案(,如果您喜欢)并报告结果。