【问题标题】:Postback triggers problem回发触发问题
【发布时间】:2011-04-22 09:40:59
【问题描述】:

我有一个gridview,我在其中显示从数据库获取的文件名。我已将文件名作为我的 gridview 中的链接按钮。

<asp:GridView ID="gdvMainList" runat="server" CssClass="Grid" 
AutoGenerateColumns="False" DataSourceID="dtsFilesUploaded" 
AllowPaging="True" DataKeyNames="Id" SkinID="PagedGridView" AllowSorting="True" 
onrowediting="gdvMainList_RowEditing" OnRowDataBound="gdvMainList_RowDataBound"                                                     onrowupdating="gdvMainList_RowUpdating" onrowcommand="gdvMainList_RowCommand">                                                    
<Columns>

<asp:TemplateField HeaderText="File Name" SortExpression="FileName">
<ItemTemplate>
<asp:LinkButton ID="lbFileName" runat="server" Text='<%# Bind("FileName") %>' OnClick="OpenFile" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Uploaded On" SortExpression="CreatedDateTime">
<ItemTemplate>
<asp:Label ID="lblCreatedDate" runat="server" Text='<%# Bind("CreatedDateTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category" SortExpression="glCategoryId">
<ItemTemplate>
<asp:Label ID="lblglCategoryId" runat="server" Text='<%# Bind("GeneralLookup.LookupItem") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlglCategoryId" runat="server" CssClass="textEntry2" DataSourceID="dtsglCategoryId"                                                            DataTextField="LookupItem" DataValueField="Id" AppendDataBoundItems="true" Width="120px">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
<asp:BoundField DataField="CreatedBy" HeaderText="Created By" Visible="false" />
</Columns>
</asp:GridView>

链接按钮有一个方法Onclick="OpenFile",打开文件的代码是:

protected void OpenFile(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        string fileName = btn.Attributes["FileName"].ToString();

        //string path = Server.MapPath(".") + "\\Files\\" + fileName;
        string path = Server.MapPath("~") + "Upload\\" + this.fileUpload.FileName;
        if (File.Exists(path))
        {
            Response.AppendHeader("content-disposition", "filename=" + fileName);
            string type = "Application/word";
            if (type != "")
                Response.ContentType = type;
            Response.WriteFile(path);
            Response.End();
        }
        else
        {
        }   
    }

现在的问题是,此代码在更新面板中不起作用,并且在没有更新面板的情况下工作得很好。这就是为什么我试图添加这些行来回帖。

<Triggers>
     <asp:PostBackTrigger ControlID="btnFileUploadSave" />
     <asp:PostBackTrigger ControlID="lbFileName" />

Control Id btnFileUploadSave 是我文件中用于上传文件的另一个图像按钮。因为没有这个也无法在更新面板中上传文件。

无论如何,当我运行此代码时,会发生以下异常:

Server Error in '/' Application.

在 UpdatePanel“upAttachFile”中找不到触发器的 ID 为“lbFileName”的控件。 说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.InvalidOperationException:在 UpdatePanel“upAttachFile”中找不到触发器的 ID 为“btnOpenFile”的控件。

如果有人可以,请提供帮助。我在这上面浪费了很多时间。

这是我的 aspx 页面的 GUI:

【问题讨论】:

  • 我认为URGENT在这里没有必要
  • 请注意Urgent在这里被认为是粗鲁的。这里的人无偿提供帮助,您无权要求他们的时间或他们在什么时间帮助您。
  • 你提到的 btnOpenFile 和 upAttachFile 控件在哪里?
  • @KoMet:哦,对不起,它是“lbFileName”,我已经更新了我的帖子。upAttachFile 是我的 UpdatePanel 的 ID。
  • "!"也不是很好,你想起了“客户”这个物种

标签: asp.net gridview triggers updatepanel postback


【解决方案1】:

您不能将 ItemTemplate 中的控件定义为 UpdatePanel 触发器。

如需解决方案,请查看this。基本上你必须在 OnItemDataBound 事件中将控件注册为触发器。

您的另一个解决方案可能是AsyncFileUpload,它不需要完整的回发即可工作。

编辑:修复了第一个链接。

【讨论】:

  • 我正在注册控件但没有任何反应!!
  • 我使用这些行来注册我的链接按钮:LinkBut​​ton lb = ((LinkBut​​ton)e.Row.FindControl("lbFileName")); lb.Attributes.Add("文件名", lb.Text); ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(lb);
  • 尝试 RegisterPostBackControl 而不是异步
  • 我都试过了。当我使用 RegisterPostBack 时,它只是回发页面。我的文件打不开。
【解决方案2】:

我为此找到了另一种选择。单击链接按钮时,我使用 window.open() 打开一个新页面。这样就解决了问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-06
    • 1970-01-01
    • 2011-07-18
    • 2012-04-24
    • 2021-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多