开发者

Avoid duplicate data when using asp:control to insert

开发者 https://www.devze.com 2023-03-17 09:45 出处:网络
I have a ASP.NET code, I use it to insert and view my data from a *.mdb file. When I reload the browser reload, it insert the same old data.

I have a ASP.NET code, I use it to insert and view my data from a *.mdb file.

When I reload the browser reload, it insert the same old data.

    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" 
        DataKeyNames="user_id" DataSourceID="AccessDataSource1" Height="50px" 
        Width="125px" DefaultMode="Insert">
        <Fields>
            <asp:BoundField DataField="user_id" HeaderText="user_id" InsertVisible="False" 
                ReadOnly="True" SortExpression="user_id" />
            <asp:BoundField DataField="user_name" HeaderText="user_name" 
                SortExpression="user_name" />
            <asp:BoundField DataField="user_pass" HeaderText="user_pass" 
                SortExpression="user_pass" />
            <asp:BoundField DataField="user_email" HeaderText="user_email" 
                SortExpression="user_email" />
            <asp:CommandField ButtonType="Button" ShowInsertButton="True" />
        </Fields>
    </asp:DetailsView>

    <div ID="Div1" runat="server"></div>


    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/App_Data/esn.mdb" 
        SelectCommand="SELECT * FROM [user]"
        InsertCommand="INSERT INTO [user] ([user_name], [user_pass], [user_email]) VALUES (@user_name, @user_pass, @user_email)">
        <InsertParameters>
            <asp:Parameter Name="user_name" Type="String" />
            <asp:Parameter Name="user_pass" Type="String" />
            <asp:Parameter Name="user_email" Type="String" />
    开发者_如何转开发    </InsertParameters>
    </asp:AccessDataSource>





    <asp:GridView ID="GridView1" runat="server" 
        AutoGenerateColumns="False" DataKeyNames="user_id" 
        DataSourceID="AccessDataSource1">
        <Columns>
            <asp:BoundField DataField="user_id" HeaderText="user_id" InsertVisible="False" 
                ReadOnly="True" SortExpression="user_id" />
            <asp:BoundField DataField="user_name" HeaderText="user_name" 
                SortExpression="user_name" />
            <asp:BoundField DataField="user_pass" HeaderText="user_pass" 
                SortExpression="user_pass" />
            <asp:BoundField DataField="user_email" HeaderText="user_email" 
                SortExpression="user_email" />
        </Columns>
    </asp:GridView>

</form>


I think sendig user to a success insert page is an option!

Try look here:

detect duplicate insert

or here:

ASP.Net - Prevent Duplicate(Double) Inserts when Page is refreshed

How to Prevent Duplicate Record Insertion on Refresh Click in Browser in ASP.Net ?

Here a Microsoft article:

Trap the Browser Refresh


use viewstate or session to store a flag "hasSaved". when the page loads for the first time :

    protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        ViewState["hasSaved"] = false;
}

and change the value of "hasSaved" to true when the the data is saved note that save the value if "hasSaved" is false See the full code here Avoid Repeated insertion due to page refresh

0

精彩评论

暂无评论...
验证码 换一张
取 消