简体   繁体   English

gridview编辑行RegisterPostBackControl

[英]gridview Edit Row RegisterPostBackControl

After much searching and testing, it's time to ask for opinions. 经过大量搜索和测试之后,该征求意见了。
A GridView inside an Update Panel with a file upload in an EditItemTemplate: 更新面板内部的GridView,在EditItemTemplate中带有文件上传:

 <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
     <asp:UpdatePanel ID="UpdatePanel1" runat="server">
         <ContentTemplate>
                    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
                        AutoGenerateColumns="False" OnRowUpdating="GridView1_RowUpdating"
                        OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing"
                        OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand" >
                        <Columns>
                            <asp:CommandField ShowEditButton="True" ShowDeleteButton="true" >                                    
                            </asp:CommandField>
                            <asp:TemplateField HeaderText="Attachment" SortExpression="FileName">
                                <EditItemTemplate>
                                    <asp:FileUpload ID="FileUpload1" runat="server" /><br />
                                    <asp:Button ID="btnAddAttachment" runat="server" Text="Upload File" CommandName="AddAttachment"
                                        CommandArgument='<%# Bind("ID") %>' />
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <a id="ancLink" runat="server" href='<%# "~/Files/" + (DataBinder.Eval(Container.DataItem,"FileName")) %>'
                                        target="_blank">
                                        <asp:Label ID="lblAnchor" runat="server"></asp:Label></a>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>
         </ContentTemplate>
     </asp:UpdatePanel>
 </asp:Content>

Then for the button in the EditItemTemplate, add the RegisterPostBackControl: 然后为EditItemTemplate中的按钮添加RegisterPostBackControl:

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState == DataControlRowState.Edit) || ((e.Row.RowState & DataControlRowState.Edit) > 0))
            {
                Button btnAddAttachment = (Button)e.Row.FindControl("btnAddAttachment");
                AjaxControlToolkit.ToolkitScriptManager ToolkitScriptManager1 = (AjaxControlToolkit.ToolkitScriptManager)Master.FindControl("ToolkitScriptManager1");
                ToolkitScriptManager1.RegisterPostBackControl(btnAddAttachment);
            }
        }
    }

The problem is that the RegisterPostBackControl will not work the first time an attempt is made to upload a file. 问题在于,第一次尝试上传文件时,RegisterPostBackControl将不起作用。 If a user edits the same row again, the second attempt works fine. 如果用户再次编辑同一行,则第二次尝试可以正常进行。
Most likely because the RegisterPostBackControl takes effect on the second post back. 最有可能是因为RegisterPostBackControl在第二次发回时生效。
Is there a way to have the button have a full postback the first time? 有没有办法让按钮在第一次有完整的回发?
I know there is an easy way for a work around but this defeats the purpose of the UpdatePanel: 我知道有一个简单的解决方法,但这违反了UpdatePanel的目的:

     <Triggers>
         <asp:PostBackTrigger ControlID="GridView1" />
     </Triggers>

And since only Admins will have access to editing, setting the PostBackTrigger for the grid in the code behind for only admins is also an option, but once again, defeating the purpose of the Update Panel. 而且,由于只有管理员才能进行编辑,因此也可以选择仅在后面的代码中为仅管理员设置网格的PostBackTrigger,但这再次违反了更新面板的目的。
Any suggestions are welcome. 欢迎任何建议。

Depending on lots of factors, you could try the updatepannel option: 根据许多因素,您可以尝试使用updatepannel选项:

ChildrenAsTriggers="true"

It may work as a temporary workaround if you need to push something out now. 如果您现在需要推出某些产品,它可能是一种临时解决方法。

Based on your code you have the UpdatePanel using the default values so ChildrenAsTriggers is true and UpdateMode is Always, so you should get a full postback every time. 根据您的代码,您具有使用默认值的UpdatePanel ,因此ChildrenAsTriggers为true, UpdateMode为Always,因此您每次都应获得完整的回发。

But I don't see you setting the Gridview's DataSourceID so it won't Databind unless you do so somewhere in the code behind. 但是我看不到您设置Gridview的DataSourceID,因此除非您在后面的代码中的某个位置进行设置,否则它不会进行Databind设置。 But you would have to be in edit mode initially in order to even find the control you're trying to register. 但是您最初必须处于编辑模式才能找到您要注册的控件。 So you need to register the control when you go into edit mode, try finding and registering the control in the gridview RowEditing event 因此,当您进入编辑模式时,您需要注册该控件,尝试在gridview RowEditing事件中查找并注册该控件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM