开发者

Postback triggers problem

I have a gridview in which I display file names getting from Databas开发者_如何学Pythone. I have made the file name as a link button in my 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>

The link button has a method Onclick="OpenFile" and the code of Open file is:

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
        {
        }   
    }

Now the problem is, this code does not work in update panel and works very fine without update panel. That's why I am trying to add these lines to make a post back.

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

Control Id btnFileUploadSave is another image button in my file that is being used to upload file. Because upload file was also not possible in update panel without this.

Anyway when I run this code, the following exception occurs:

Server Error in '/' Application.

A control with ID 'lbFileName' could not be found for the trigger in UpdatePanel 'upAttachFile'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: A control with ID 'btnOpenFile' could not be found for the trigger in UpdatePanel 'upAttachFile'.

Please help if anyone can. I have wasted a lotttttttttttttt of my time on this.

Here is the GUI of my aspx page:

Postback triggers problem


You can't define a control in an ItemTemplate as an UpdatePanel trigger.

For a solution, check this out. Basically you have to register the control as trigger in the OnItemDataBound event.

Another solution for you is perhaps the AsyncFileUpload, which doesn't require a full postback to work.

EDIT: fixed first link.


I found another alternative for this. I open a new page using window.open() on clicking my link button. That solved the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜