Asp.net,Response.Redirect无效。为什么?

时间:2012-03-15 05:58:40

标签: asp.net

在我的应用程序中,我在将记录插入到我的数据源后尝试重定向到另一个页面。插入部分工作正常,但页面永远不会重定向。有任何想法吗?这是我目前的代码:

ASPX

   <asp:FormView ID="FormView1" runat="server" DataKeyNames="RecapID" 
    DataSourceID="RecapDataSource" DefaultMode="Insert">
        <InsertItemTemplate>
            Recap Date:
            <asp:TextBox ID="RecapDateTextBox" runat="server" 
                Text='<%# Bind("RecapDate") %>' Width="80px" /><asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1" ControlToValidate="RecapDateTextBox" ErrorMessage="*" display="Dynamic" />
            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Create"/>
        </InsertItemTemplate>
        </asp:FormView>
<asp:SqlDataSource ID="RecapDataSource" runat="server" 
    ConflictDetection="CompareAllValues" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
    InsertCommand="INSERT INTO [ftsrms_Recap] ([RecapDate]) VALUES (@RecapDate) SELECT @NewRecapID = SCOPE_IDENTITY()" 
    OldValuesParameterFormatString="original_{0}">
    <InsertParameters>
        <asp:Parameter DbType="Date" Name="RecapDate" />
        <asp:Parameter Direction="Output" Name="NewRecapID" Type="Int32" />
    </InsertParameters>
</asp:SqlDataSource>

ASPX.VB

Protected Sub RecapDataSource_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)

    'Read the value of the @NewProductID OUTPUT parameter
    Dim newRecapID As Integer = Convert.ToInt32(e.Command.Parameters("@NewRecapID").Value)

    Response.Redirect("Profile.aspx?")

End Sub

1 个答案:

答案 0 :(得分:0)

您需要将事件处理程序添加到 RecapDataSource

<强> oninserted = “RecapDataSource_Inserted”

所以你的aspx代码变成了:

 <asp:FormView ID="FormView1" runat="server" DataKeyNames="RecapID" 
    DataSourceID="RecapDataSource" DefaultMode="Insert">
        <InsertItemTemplate>
            Recap Date:
            <asp:TextBox ID="RecapDateTextBox" runat="server" 
                Text='<%# Bind("RecapDate") %>' Width="80px" /><asp:RequiredFieldValidator runat="server" id="RequiredFieldValidator1" ControlToValidate="RecapDateTextBox" ErrorMessage="*" display="Dynamic" />
            <asp:Button ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Create"/>
        </InsertItemTemplate>
        </asp:FormView>
<asp:SqlDataSource ID="RecapDataSource" runat="server"
    oninserted="RecapDataSource_Inserted"
    ConflictDetection="CompareAllValues" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>"  
    InsertCommand="INSERT INTO [ftsrms_Recap] ([RecapDate]) VALUES (@RecapDate) SELECT @NewRecapID = SCOPE_IDENTITY()" 
    OldValuesParameterFormatString="original_{0}">
    <InsertParameters>
        <asp:Parameter DbType="Date" Name="RecapDate" />
        <asp:Parameter Direction="Output" Name="NewRecapID" Type="Int32" />
    </InsertParameters>
</asp:SqlDataSource>

修改

同样改变这一点:

Protected Sub RecapDataSource_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs)

    'Read the value of the @NewProductID OUTPUT parameter
    Dim newRecapID As Integer = Convert.ToInt32(e.Command.Parameters("@NewRecapID").Value)

    Response.Redirect("Profile.aspx?")

End Sub

到此:

Protected Sub RecapDataSource_Inserted(sender As Object, e As System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)

    'Read the value of the @NewProductID OUTPUT parameter
    Dim newRecapID As Integer = Convert.ToInt32(e.Command.Parameters("@NewRecapID").Value)

    Response.Redirect("Profile.aspx?")

End Sub

如果仍然无效,请删除此行(或评论),然后重试:

Dim newRecapID As Integer = Convert.ToInt32(e.Command.Parameters("@NewRecapID").Value)

可能是上升例外。