如何在Page_Load

时间:2019-02-12 08:24:42

标签: c# asp.net

我有一个转发器元素,在转发器内部有一个FileUpload控件。

当页面上的其他操作导致其提交时,FileUpload控件将丢失所选文件。为了解决这个问题,我每次发布页面时都会保存文件。

在转发器中,我也有一个按钮,我使用转发器的OnItemCommand。

当按下按钮时,将发生验证事件,因为我在页面加载和ItemCommand事件中都有数据绑定。如果提交是由于ItemCommand事件触发的,我想抑制页面加载中的数据绑定,但是我无法找到如何判断它是被触发的。

我已经检查了Page.Request.Params [“ __ EVENTTARGET”];和Page.Request.Params [“ __ EVENTARGUMENT”];而且都是空的。

是否有一种简便的方法来找出触发事件,还是应该使用其他方法?

            <asp:Repeater ID="rptrAccounts" runat="server" OnItemCommand="rptrAccounts_ItemCommand">
            <ItemTemplate>
                <div style="padding:5px;">
                    <div style="float:left;font-weight:bold;font-size:larger;">
                        Enquiry <%# Eval("Number") %>
                    </div>
                    <div <%# Eval("Heading") %> style="float:right;">
                        <asp:Button ID="butRemove" runat="server" Text="Remove" CssClass="button submit-button" CommandName="Remove" CommandArgument='<%# Eval("Number") %>' />
                    </div>
                </div>
                <table style="border: transparent; width: 100%;">
                    <tr>
                        <td>
                            <asp:FileUpload ID="uplAI1" runat="server" Width="95%" onchange="verifyFileSize()" onkeydown="javascript:clearUpload(event)" /> <asp:CheckBox ID="chkFile1" runat="server" TextAlign="Left" Visible='<%# Eval("ShowFile1") %>' Text='<%# Eval("File1Text") %>' ToolTip='<%# Eval("Record1") %>' />
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:FileUpload ID="uplAI2" runat="server" Width="95%" onchange="verifyFileSize()" onkeydown="javascript:clearUpload(event)" /> <asp:CheckBox ID="chkFile2" runat="server" TextAlign="Left" Visible='<%# Eval("ShowFile2") %>' Text='<%# Eval("File2Text") %>' ToolTip='<%# Eval("Record2") %>' />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>

在Page_Load中,我有

            if (IsPostBack)
        {
    List<EnquiryAccountInfo> Accounts = new List<EnquiryAccountInfo>();

            for (int i = 1; i <= rptrAccounts.Items.Count; i++)
            {
                    RepeaterItem ri = rptrAccounts.Items[i - 1];
                    EnquiryAccountInfo ai = new EnquiryAccountInfo();

                    ai.Number = i;

                        string num;
                        ai.File1 = ProcessFile((FileUpload)ri.FindControl("uplAI1"), (CheckBox)ri.FindControl("chkFile1"), oTrim, container, type, ref errors, out num, docTypeError);
                        ai.Record1 = num;
                        ai.File2 = ProcessFile((FileUpload)ri.FindControl("uplAI2"), (CheckBox)ri.FindControl("chkFile2"), oTrim, container, type, ref errors, out num, docTypeError);
                        ai.Record2 = num;
                    }


                    Accounts.Add(ai);
            }

            rptrAccounts.DataSource = Accounts;
            rptrAccounts.DataBind();
        }

然后我具有以下功能:

        private string ProcessFile(FileUpload fileUpload, CheckBox checkBox, Database oTrim, Record container, RecordType type, ref string errors, out string num, string docTypeError)
    {
        if (fileUpload.HasFile)
        {
            if (!ValidateDocument(ref errors, fileUpload, docTypeError))
            {
                num = checkBox.ToolTip;
                return checkBox.Text;
            }

            if (checkBox.Visible)
                oTrim.GetRecord(checkBox.ToolTip).Delete();

            string fileName = null;
            try
            {
                fileName = Path.GetTempPath() + fileUpload.FileName;

                fileUpload.PostedFile.SaveAs(fileName);

                //Store the file code
                num = reference;
                return fileUpload.FileName;
            }
            finally
            {
                if (fileName != null && File.Exists(fileName))
                {
                    try
                    {
                        File.Delete(fileName);
                    }
                    catch { }
                }
            }
        }
        else if (checkBox.Checked)
        {
            oTrim.GetRecord(checkBox.ToolTip).Delete();
            checkBox.Text = string.Empty;
            checkBox.ToolTip = string.Empty;
            checkBox.Checked = false;
            checkBox.Visible = false;
        }

        num = checkBox.ToolTip;
        return checkBox.Text;
    }

        protected void rptrAccounts_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        List<EnquiryAccountInfo> Accounts = RefreshAccounts();

        int remove = int.Parse((string)e.CommandArgument) - 1;
        Accounts.RemoveAt(remove);

        for (int i = remove; i < Accounts.Count; i++)
            Accounts[i].Number--;

        rptrAccounts.DataSource = Accounts;
        rptrAccounts.DataBind();
    }

0 个答案:

没有答案