这工作正常,但现在每次我从页面执行插入时都会插入重复的行。两行都很好,所有数据都正确插入,但每次将记录放入数据库中两次。我该如何阻止这个?
VB.net 代码:
Protected Sub gvOnCallSchedule_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles gvOnCallSchedule.RowCommand
If e.CommandName = "Insert" AndAlso Page.IsValid Then
Try
dsOncallGroup.Insert()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "SQL Error")
End Try
End If
If e.CommandName = "Delete" Then
Try
dsOncallGroup.Delete()
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical, "SQL Error")
End Try
'Response.Redirect("Default.aspx")
End If
End Sub
Protected Sub dsOnCallGroup_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs) Handles dsOncallGroup.Inserting
Dim strddlGroupName As New DropDownList
Dim txtStartOnCall As New TextBox
Dim txtEndOnCall As New TextBox
Dim txtRank As New TextBox
Dim strddlEmpName As New DropDownList
Dim strddlOCPreferredContact As New DropDownList
Dim txtEmpWorkPhone As New TextBox
Dim txtEmpHomePhone As New TextBox
Dim txtEmpHomeCellPhone As New TextBox
Dim txtEmpAltPhone As New TextBox
strddlGroupName = CType(gvOnCallSchedule.FooterRow.FindControl("ftrddlOCGroup"), DropDownList)
txtStartOnCall = CType(gvOnCallSchedule.FooterRow.FindControl("txtStartOnCallFtr"), TextBox)
txtEndOnCall = CType(gvOnCallSchedule.FooterRow.FindControl("txtEndOnCallFtr"), TextBox)
txtRank = CType(gvOnCallSchedule.FooterRow.FindControl("txtCallOrderFtr"), TextBox)
strddlEmpName = CType(gvOnCallSchedule.FooterRow.FindControl("ddlAnalystFtr"), DropDownList)
strddlOCPreferredContact = CType(gvOnCallSchedule.FooterRow.FindControl("ftrddlOCPreferredContact"), DropDownList)
txtEmpWorkPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtWorkPhoneFtr"), TextBox)
txtEmpHomePhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtHomePhoneFtr"), TextBox)
txtEmpHomeCellPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtHomeCellPhoneFtr"), TextBox)
txtEmpAltPhone = CType(gvOnCallSchedule.FooterRow.FindControl("txtAltPhoneFtr"), TextBox)
e.Command.Parameters("@fldOnCallGroup").Value = strddlGroupName.Text
e.Command.Parameters("@fldStartOnCall").Value = txtStartOnCall.Text
e.Command.Parameters("@fldEndOnCall").Value = txtEndOnCall.Text
e.Command.Parameters("@fldRank").Value = txtRank.Text
e.Command.Parameters("@fldEmpName").Value = strddlEmpName.Text
e.Command.Parameters("@fldOCPreferredContact").Value = strddlOCPreferredContact.Text
e.Command.Parameters("@fldEmpWorkPhone").Value = txtEmpWorkPhone.Text
e.Command.Parameters("@fldEmpHomePhone").Value = txtEmpHomePhone.Text
e.Command.Parameters("@fldEmpHomeCellPhone").Value = txtEmpHomeCellPhone.Text
e.Command.Parameters("@fldEmpAltPhone").Value = txtEmpAltPhone.Text
End Sub
ASP代码数据源:
<asp:SqlDataSource ID="dsOncallGroup" runat="server"
ConnectionString="<%$ ConnectionStrings:DiscussSQLConnectionString %>"
SelectCommand="SELECT * FROM tblOnCallSchedule WHERE (tblOnCallSchedule.fldOnCallGroup = @fldOnCallGroup)
AND (tblOnCallSchedule.fldEndOnCall > GETDATE())
ORDER BY tblOnCallSchedule.fldOnCallGroup, tblOnCallSchedule.fldEndOnCall, tblOnCallSchedule.fldRank"
OldValuesParameterFormatString="original_{0}"
DeleteCommand="DELETE FROM tblOnCallSchedule WHERE fldOCID = @original_fldOCID"
InsertCommand="INSERT INTO [tblOnCallSchedule] ([fldOnCallGroup], [fldStartOnCall], [fldEndOnCall], [fldRank], [fldEmpName], [fldOCPreferredContact], [fldEmpWorkPhone],
[fldEmpHomePhone],[fldEmpHomeCellPhone],[fldEmpAltPhone])
VALUES (@fldOnCallGroup, @fldStartOnCall, @fldEndOnCall, @fldRank, @fldEmpName, @fldOCPreferredContact, @fldEmpWorkPhone, @fldEmpHomePhone,
@fldEmpHomeCellPhone, @fldEmpAltPhone)"
UpdateCommand="UPDATE [tblOnCallSchedule] SET [fldOnCallGroup] = @fldOnCallGroup, [fldStartOnCall] = @fldStartOnCall,
[fldEndOnCall] = @fldEndOnCall, [fldRank] = @fldRank, [fldEmpName] = @fldEmpName, [fldOCPreferredContact] = @fldOCPreferredContact,
[fldEmpWorkPhone] = @fldEmpWorkPhone,[fldEmpHomePhone] = @fldEmpHomePhone,[fldEmpHomeCellPhone] = @fldEmpHomeCellPhone,
[fldEmpAltPhone] = @fldEmpAltPhone
WHERE [fldOCID] = @original_fldOCID">
<SelectParameters>
<asp:ControlParameter ControlID="cboOncallGroup" Name="fldOnCallGroup"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
<DeleteParameters>
<asp:Parameter Name= "original_fldOCID" Type="Int16" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="fldStartOnCall" DbType ="Date" />
<asp:Parameter Name="fldEndOnCall" DbType ="Date" />
<asp:Parameter Name= "fldRank" Type="Int16" />
<asp:Parameter Name= "fldEmpName" Type="String" />
<asp:Parameter Name= "fldOCPreferredContact" Type="String" />
<asp:Parameter Name= "fldEmpWorkPhone" Type="String" />
<asp:Parameter Name= "fldEmpHomePhone" Type="String" />
<asp:Parameter Name= "fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name= "fldEmpAltPhone" Type="String" />
<asp:Parameter Name="original_fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="original_fldStartOnCall" DbType="Date" />
<asp:Parameter Name="original_fldEndOnCall" DbType="Date" />
<asp:Parameter Name="original_fldRank" Type="Int16" />
<asp:Parameter Name="original_fldEmpName" Type="String" />
<asp:Parameter Name="original_fldOCPreferredContact" Type="String" />
<asp:Parameter Name="original_fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomePhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="original_fldEmpAltPhone" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="fldOnCallGroup" Type="String" />
<asp:Parameter DbType="Date" Name="fldStartOnCall" />
<asp:Parameter DbType="Date" Name="fldEndOnCall" />
<asp:Parameter Name="fldRank" Type="Int16" />
<asp:Parameter Name="fldEmpName" Type="String" />
<asp:Parameter Name="fldOCPreferredContact" Type="String" />
<asp:Parameter Name="fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="fldEmpHomePhone" Type="String" />
<asp:Parameter Name="fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="fldEmpAltPhone" Type="String" />
<asp:Parameter Name="original_fldOnCallGroup" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="original_fldStartOnCall" DbType="Date" />
<asp:Parameter Name="original_fldEndOnCall" DbType="Date" />
<asp:Parameter Name="original_fldRank" Type="Int16" />
<asp:Parameter Name="original_fldEmpName" Type="String" />
<asp:Parameter Name="original_fldOCPreferredContact" Type="String" />
<asp:Parameter Name="original_fldEmpWorkPhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomePhone" Type="String" />
<asp:Parameter Name="original_fldEmpHomeCellPhone" Type="String" />
<asp:Parameter Name="original_fldEmpAltPhone" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
网格视图代码:
<asp:Gridview ID="gvOnCallSchedule" runat="server" AllowSorting="True" AllowPaging="True" showfooter="true"
OnRowUpdating="GvOnCall_Update" onRowCommand="gvOnCallSchedule_RowCommand" DataKeyNames="fldOCID"
BackColor="Aqua" DataSourceID="dsOncallGroup" Font-Bold="True"
Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" ForeColor="#333333" HorizontalAlign="Justify"
CellPadding="4" GridLines="None" PageSize="20" AutoGenerateColumns ="false"
OnSelectedIndexChanged="cboOnCallGroup_SelectedIndexChanged">
<Columns>
<asp:TemplateField ShowHeader="false">
<FooterTemplate>
<asp:ImageButton ID="AddButton" runat="server" CommandName="Insert" ImageURL="~/images/New.png"
Text="Add" ToolTip="Add New On Call Record" />
</FooterTemplate>
<EditItemTemplate>
<asp:ImageButton ID="UpdateButton" runat="server" CausesValidation="False" CommandName="Update"
ImageUrl="~/images/Save.png" Text="Update" ToolTip="Update" />
<asp:ImageButton ID="CancelButton" runat="server" CausesValidation="false" CommandName="Cancel"
ImageURL="~/images/Cancel.png" Text="Cancel" ToolTip="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
ImageUrl="~/images/Edit.gif" Text="Edit" ToolTip="Edit" />
<asp:ImageButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
ImageUrl="~/images/Delete.png" Text="Delete" ToolTip="Delete" />
</ItemTemplate>
<ItemStyle Wrap="false" />
</asp:TemplateField>
<asp:Boundfield headertext="fldOCID" datafield="fldOCID" sortexpression="fldOCID" visible="false" InsertVisible="False" />
<asp:TemplateField headertext="On Call Group">
<ItemTemplate>
<asp:DropDownList ID="ddlOCGroup" width="200px" runat="server" datavaluefield="fldOnCallGroup" Enabled="false"
DataSourceID= "dsListbox" SelectedValue="<%# Bind('fldOnCallGroup') %>">
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrddlOCGroup" width="200px" runat="server" datavaluefield="fldOnCallGroup"
DataSourceID= "dsListbox" SelectedValue="<%# Bind('fldOnCallGroup') %>">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Start Date/Time">
<ItemTemplate>
<asp:Textbox ID="txtStartOnCall" runat="server" Text='<%# Bind("fldStartOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="StartOnCallCal" runat="server" TargetControlID="txtStartOnCall"
PopupButtonID="calpopup" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtStartOnCallFtr" runat="server" Text='<%# Bind("fldStartOncall") %>'>
</asp:Textbox>
<asp:Image ID="calpopupFtr" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="StartOnCallCalFtr" runat="server" TargetControlID="txtStartOnCallFtr"
PopupButtonID="calpopupFtr" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="End Date/Time">
<ItemTemplate>
<asp:Textbox ID="txtEndOnCall" runat="server" Text='<%# Bind("fldEndOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup2" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="EndOnCallCal" runat="server" TargetControlID="txtEndOnCall"
PopupButtonID="calpopup2" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtEndOnCallFtr" runat="server" Text='<%# Bind("fldEndOnCall") %>'>
</asp:Textbox>
<asp:Image ID="calpopup2Ftr" runat="server" ImageUrl="~/images/calendar2.png" />
<asp:CalendarExtender ID="EndOnCallCalFtr" runat="server" TargetControlID="txtEndOnCallFtr"
PopupButtonID="calpopup2Ftr" PopupPosition="Right" format="MM/dd/yyyy">
</asp:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Call Order">
<ItemTemplate>
<asp:Textbox ID="lblCallOrder" runat="server" width="100" Text='<%# Bind("fldRank") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtCallOrderFtr" runat="server" width="100" Text='<%# Bind("fldOnCallGroup") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="OnCall Analyst">
<ItemTemplate>
<asp:DropDownList ID="ddlAnalyst" runat="server" datavaluefield="fldEmpName"
datasourceID="dsEmp" SelectedValue='<%# Bind("fldEmpName") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlAnalystFtr" runat="server" datavaluefield="fldEmpName"
DataSourceID= "dsEmp" SelectedValue='<%# Bind("fldEmpName") %>'>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Preferred Contact">
<ItemTemplate>
<asp:DropDownList ID="ddlOCPreferredContact" Width="150px" runat="server" datavaluefield="fldOCPreferredContact"
datasourceID="dsPref" SelectedValue='<%# Bind("fldOCPreferredContact") %>'>
</asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ftrddlOCPreferredContact" width="150px" runat="server" datavaluefield="fldOCPreferredContact"
DataSourceID= "dsPref" SelectedValue='<%# Bind("fldOCPreferredContact") %>'>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Work Phone">
<ItemTemplate>
<asp:Textbox ID="lblWorkPhone" runat="server" width="90" Text='<%# Bind("fldEmpWorkPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtWorkPhoneFtr" runat="server" width="90" Text='<%# Bind("fldEmpWorkPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Home Phone">
<ItemTemplate>
<asp:Textbox ID="lblHomePhone" runat="server" width="90" Text='<%# Bind("fldEmpHomePhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtHomePhoneFtr" runat="server" width="90" Text='<%# Bind("fldEmpHomePhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Personal Cell">
<ItemTemplate>
<asp:Textbox ID="lblHomeCellPhone" runat="server" width="95" Text='<%# Bind("fldEmpHomeCellPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtHomeCellPhoneFtr" runat="server" width="95" Text='<%# Bind("fldEmpHomeCellPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField headertext="Alternate Phone">
<ItemTemplate>
<asp:Textbox ID="lblAltPhone" runat="server" width="105" Text='<%# Bind("fldEmpAltPhone") %>'>
</asp:Textbox>
</ItemTemplate>
<FooterTemplate>
<asp:Textbox ID="txtAltPhoneFtr" runat="server" width="105" Text='<%# Bind("fldEmpAltPhone") %>'>
</asp:Textbox>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#7C6F57" />
<AlternatingRowStyle BackColor="White" />
</asp:Gridview>