删除命令在回发时不触发,asp.net重复器

本文关键字:net asp 命令 删除 | 更新日期: 2023-09-27 18:15:17

我有一个页面,其中包含一个数据中继器,它又包含一个按钮,意味着从SQL数据库中删除一个删除记录。当点击此按钮时,我收到以下错误:过程或函数'usp_setKS3_DeleteAssessment'期望参数'@ intassessmentd ',而没有提供。

如果我注释掉KS3_DataSource_Assessment.Delete();页面后面的代码中的一行没有错误地加载并显示消息,但没有删除记录(我期望这一行被注释掉)。我不知道该怎么做才能做到这一点……什么好主意吗?

ASP.net

    <asp:SqlDataSource ID="KS3_DataSource_Assessment" runat="server" ConnectionString="<%$ ConnectionStrings:sqlWriter %>" ProviderName="System.Data.SqlClient" SelectCommandType="StoredProcedure" SelectCommand="usp_getKS3_ManageAssessments" OnSelected="KS3_DataSource_Assessment_Selected" DeleteCommand="usp_setKS3_DeleteAssessment">
                                    <SelectParameters>
                                        <asp:ControlParameter ControlID="KS3_DropDown_ACYear" PropertyName="SelectedValue" Name="intACYearId" Type="Int32" />
                                        <asp:ControlParameter ControlID="KS3_DropDown_Subject" PropertyName="SelectedValue" Name="intSubjectId" Type="Int32" />
                                        <asp:ControlParameter ControlID="KS3_DropDown_Year" PropertyName="SelectedValue" Name="intYearGroup" Type="Int32" />
                                    </SelectParameters>
                                    <DeleteParameters>
                                        <asp:Parameter Name="intAssessmentId" Type="Int32" />
                                    </DeleteParameters>
                                </asp:SqlDataSource>
                                <asp:Repeater runat="server" ID="KS3_Repeater_Assessments" EnableViewState="true" DataSourceID="KS3_DataSource_Assessment" OnItemDataBound="KS3_Repeater_Assessments_ItemDataBound">
                                    <ItemTemplate>
                                        <!-- Assessments -->
                                        <asp:Table runat="server" ID="KS3_Table_Assessments" CssClass="width100pc">
                                            <asp:TableRow CssClass="SectionSubHeader">
                                                <asp:TableCell><%# Eval("txtAssessmentTitle").ToString() %></asp:TableCell>
                                                <asp:TableCell CssClass="width25px alignCenter alignVerticalMiddle" RowSpan="2"><asp:ImageButton SkinID="imgButtonDelete" runat="server" ID="KS3_Button_DeleteAssessment" OnCommand="KS3_Button_DeleteAssessment_Command" CommandArgument='<%# Eval("intAssessmentID").ToString() %>' OnClientClick="return confirm('Are you sure you want to delete this assessment?'n'nOnce deleted all data is lost.');" /></asp:TableCell>
                                                <asp:TableCell CssClass="width25px alignCenter alignVerticalMiddle" RowSpan="2">Up</asp:TableCell>
                                                <asp:TableCell CssClass="width25px alignCenter alignVerticalMiddle" RowSpan="2">Down</asp:TableCell>
                                            </asp:TableRow>
                                            <asp:TableRow CssClass="BackgroundBlack fontStyleItalic">
                                                <asp:TableCell><%# Eval("txtAssessmentDescription").ToString() %></asp:TableCell>
                                            </asp:TableRow>
                                        </asp:Table>
                                        <!-- Columns -->
                                        <asp:SqlDataSource ID="KS3_DataSource_Columns" runat="server" ConnectionString="<%$ ConnectionStrings:sqlWriter %>" ProviderName="System.Data.SqlClient" SelectCommandType="StoredProcedure" SelectCommand="usp_getKS3_ManageAssessments">
                                            <SelectParameters>
                                                <asp:Parameter Name="intACYearId" Type="Int32" />
                                            </SelectParameters>
                                        </asp:SqlDataSource>
                                    </ItemTemplate>
                                </asp:Repeater>

背后的代码
        protected void Page_Load(object sender, EventArgs e)
{
}
protected void KS3_Button_SelectSubject_Click(object sender, EventArgs e)
{
    if (KS3_DropDown_Subject.SelectedIndex == 0) { DisplayMessage("Validation Error", "Please select a subject from the list below.", "error"); KS3_Panel_DataToManage.Visible = false; }
    else if (KS3_DropDown_Year.SelectedIndex == 0) { DisplayMessage("Validation Error", "Please select a year group from the list below.", "error"); KS3_Panel_DataToManage.Visible = false; }
    else
    {
        //No errors detected, start data display
        Master.systemMessageHide();
        KS3_Panel_DataToManage.Visible = true;
        //Update display
        KS3_Label_Title.Text = KS3_DropDown_Subject.SelectedItem.Text.Trim() + " - " + KS3_DropDown_Year.SelectedItem.Text.Trim();
        //Bind the new data to the repeater
        KS3_Repeater_Assessments.DataBind();
    }
}
public void DisplayMessage(string title, string message, string type)
{
    Master.systemMessageShow(title, message, type);
}
protected void KS3_DataSource_Assessment_Selected(object sender, SqlDataSourceStatusEventArgs e)
{
    if (e.AffectedRows < 1) { KS3_Panel_NoAssessments.Visible = true; }
    else { KS3_Panel_NoAssessments.Visible = false; }
}

protected void KS3_Button_DeleteAssessment_Command(object sender, CommandEventArgs e)
{
    KS3_Label_Title.Text = "DataSubmitted";
    KS3_DataSource_Assessment.DeleteParameters["intAssessmentId"].DefaultValue = e.CommandArgument.ToString();
    Trace.Write("Item Value: " + e.CommandArgument.ToString());
    KS3_DataSource_Assessment.Delete();
    KS3_Repeater_Assessments.DataBind();
    DisplayMessage("Assessment Delete", "The selected assessment has been deleted and all associated data has been removed.", "success");
} 

删除命令在回发时不触发,asp.net重复器

看起来您缺少数据源控件的这个属性:

DeleteCommandType="StoredProcedure"

默认值为Text,使控件认为提供的命令是SQL语句。由于使用的是存储过程,所以这不是您想要的行为。