FormView更新ASP NET

时间:2019-02-15 11:14:43

标签: c# asp.net forms formview

所以我有一个FormView,我需要在其中更新表单,并且一直说我需要itemupdating事件。

我将点击按钮称为更新

    protected void btnValidarFormulario_Click(object sender, EventArgs e)
    {
        if(Page.IsValid)
        {
            try
            {                    
                fvADefinicao.UpdateItem(true);
            }
            catch(Exception ex)
            {        
                ScriptManager.RegisterStartupScript(this, this.GetType(), "key_name", "ShowToast('Erro', 'topCenter', 'error', 'Falha na ligação à base de dados. tente novamente mais tarde', 2000);", true);
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "key_name", "ShowToast('OK', 'topCenter', 'success', 'Registo validado com sucesso', 2000); setTimeout(function(){window.location.href ='../Inicio.aspx'}, 2000);", true);
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "ShowToast('Atenção', 'topCenter', 'warning', 'Preencha todos os campos com dados válidos.', 2000)", true);
        }
    }

在sqldatasource更新的事件上,我还做了一些其他事情

   protected void sdsADefinicao_Updated(object sender, SqlDataSourceStatusEventArgs e)
    {
        int id = Convert.ToInt32(e.Command.Parameters["@ReturnStatus"].Value);

        FileUpload fuAnexo = fvADefinicao.FindControl("fuAnexo") as FileUpload;

        int numFiles = fuAnexo.PostedFiles.Count;
        int index = 0;
        Ficheiro[] files = new Ficheiro[numFiles];

        foreach (HttpPostedFile file in fuAnexo.PostedFiles)
        {
            Ficheiro anexo = new Ficheiro("application/pdf", "HUTMATER");
            anexo.Nome = Path.GetFileName(file.FileName);
            using (Stream fs = file.InputStream)
            using (BinaryReader br = new BinaryReader(fs))
            {
                anexo.Dados = br.ReadBytes((Int32)fs.Length);
            }
            files[index] = anexo;
        }

        string piloto = SessionManager.Nome;
        Ficheiro.Upload(id, piloto, files); // Guarda anexos do formulario na base de dados            
    }


    <asp:SqlDataSource
                ID="sdsADefinicao"
                runat="server"
                ConnectionString="<%$ ConnectionStrings:ValidacaoFormas_ConnectionString %>"
                UpdateCommand="sp_UPDATE_ADefinicao_Extrusao"
                UpdateCommandType="StoredProcedure"                    
                OnUpdated="sdsADefinicao_Updated"
                SelectCommandType="StoredProcedure"
                SelectCommand="sp_SELECT_ADefinicao">

                <SelectParameters>
                    <asp:ControlParameter ControlID="hdfRegistoID" Name="RegistoID" Type="Int32" />
                </SelectParameters>

                <UpdateParameters>
                    <asp:Parameter Name="Base_Cru" Type="String" />
                    <asp:Parameter Name="Linha_Cru" Type="String" />
                    <asp:Parameter Name="Marcacao_Continua_Cru" Type="String" />
                    <asp:Parameter Name="Diametro_Cru" Type="Decimal" />
                    <asp:Parameter Name="Espessura_Cru" Type="Decimal" />
                    <asp:Parameter Name="Comprimento_Cru" Type="Decimal" />
                    <asp:Parameter Name="ReturnStatus" Type="Int32" Direction="Output" />
                    <asp:ControlParameter ControlID="hdfRegistoID" Name="RegistoID" Type="Int32" Direction="Output" />
                </UpdateParameters>
            </asp:SqlDataSource>

我什至尝试创建一个没有任何内容的onItemUpdating事件,但仍然给我同样的错误,即未处理itemupdating事件。我可以只使用sqldatasource在formview上进行插入,而无需从formview事件中插入任何内容。为什么我不能在表单视图更新上做同样的事情?

<asp:FormView 
    runat="server" 
    ID="fvADefinicao" 
    DataKeyNames="ID_Registo" 
    DefaultMode="Edit" 
    HorizontalAlign="Center" 
    Style="width: 100%; max-width: 1200px;">
                <EditItemTemplate>

0 个答案:

没有答案