【问题标题】:ASP.Net FormView won't update with Response.Redirect codeASP.Net FormView 不会使用 Response.Redirect 代码更新
【发布时间】:2021-01-15 12:42:26
【问题描述】:

我有一个相当简单的 FormView 控件,用于 ASP.Net/C#。我的问题是当我在更新/编辑后使用代码重定向时,FormView 实际上不会执行更新,但是它会重定向。我不会发布所有 FormView 代码,因为当我注释掉重定向代码时它会更新。我想我要问的是如何在使用重定向代码时更改要更新的代码?同样,当我不执行重定向时,更新工作正常!是否有可能在更新发生之前发生重定向?

重定向代码

         <script runat="server">

    protected void FormView1_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
    {
       
        {
           Response.Redirect("redirect_main.aspx");
        }
    }


</script>

FormView1
          <asp:FormView ID="FormView1" runat="server" onitemupdating="FormView1_ItemUpdating" DataKeyNames="req_submitted_key" DataSourceID="SqlDataSource2" DefaultMode="Edit" >
                   <EditItemTemplate>
                    req_submitted_key:
                    <asp:Label ID="req_submitted_keyLabel1" runat="server" Text='<%# Eval("req_submitted_key") %>' />
                    <br />
                    Role_job_title:
                    <asp:TextBox ID="Role_job_titleTextBox" runat="server" Text='<%# Bind("Role_job_title") %>' />
                    <br />
                    requestname:
                    <asp:TextBox ID="requestnameTextBox" runat="server" Text='<%# Bind("requestname") %>' />
                    <br />
                    submitted_by_name:
                    <asp:TextBox ID="submitted_by_nameTextBox" runat="server" Text='<%# Bind("submitted_by_name") %>' />
                    <br />
                    Submitted_by_email:
                    <asp:TextBox ID="Submitted_by_emailTextBox" runat="server" Text='<%# Bind("Submitted_by_email") %>' />
                    <br />
                    submitted_date:
                    <asp:TextBox ID="submitted_dateTextBox" runat="server" Text='<%# Bind("submitted_date") %>' />
                    <br />
                    submitted_by_comment:
                    <asp:TextBox ID="submitted_by_commentTextBox" runat="server" Text='<%# Bind("submitted_by_comment") %>' />
                    <br />
                    approved_denied:
                    <asp:TextBox ID="approved_deniedTextBox" runat="server" Text='<%# Bind("approved_denied") %>' />
                    <br />
                    <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
                    &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                    </EditItemTemplate>

【问题讨论】:

    标签: c# sql asp.net .net


    【解决方案1】:

    是的...它在更新之前重定向。将您的重定向从 ItemUpdating 中取出并放入 ItemUpdated。

    发件人:DetailsView.ItemUpdatint Event

    DetailsView.ItemUpdating 事件

    在单击 DetailsView 控件中的更新按钮时发生, 但在更新操作之前。

    DetailsView.ItemUpdated 事件

    在单击 DetailsView 控件中的更新按钮时发生, 但在更新操作之后。

    DetailsView.ItemUpdated Event

    您的代码应如下所示:

    protected void FormView1_ItemUpdated(Object sender, FormViewUpdateEventArgs e)
    {
        Response.Redirect("redirect_main.aspx");
    }
    

    【讨论】:

    • 也许我需要再次阅读这篇文章,但我没有在该页面中看到它解释了如何防止我的问题发生。
    • @user2209864 好的..我更新了答案..删除重定向并将其放入 ItemUpdated。
    • 我试试@user2209864
    • 我读到这段代码会有所帮助,但我得到一个异常错误! (不包含 Exception if (e.Exception == null) 的定义
    • protected void FormView1_ItemUpdating(Object sender, FormViewUpdateEventArgs e) { { Response.Redirect("redirect_main.aspx"); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 2018-07-19
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多