C# 如何使用Ajax ModalPopupXtender控件更新GridView中的特定列?

C# 如何使用Ajax ModalPopupXtender控件更新GridView中的特定列?,c#,asp.net,C#,Asp.net,我是一名新的ASP.NET开发人员,我正在为我的公司开发一个基于web的建议框程序,员工可以在其中提交任何安全建议。现在,我正在处理这个系统的管理部分 管理员将能够看到GridView控件中列出的所有建议。在GridView的最后一列中,状态将列在那里。当管理员单击其中一个建议的状态时,将出现一个新的弹出窗口(asp.net ajax ModalPopupXtender),其中列出了所有可能的状态,例如:已采取行动、已批准。。。当管理员选择其中一种状态时,建议的状态将在数据库中更新我编写了代码,

我是一名新的ASP.NET开发人员,我正在为我的公司开发一个基于web的建议框程序,员工可以在其中提交任何安全建议。现在,我正在处理这个系统的管理部分

管理员将能够看到GridView控件中列出的所有建议。在GridView的最后一列中,状态将列在那里。当管理员单击其中一个建议的状态时,将出现一个新的弹出窗口(asp.net ajax ModalPopupXtender),其中列出了所有可能的状态,例如:已采取行动、已批准。。。当管理员选择其中一种状态时,建议的状态将在数据库中更新我编写了代码,但仍然没有更新建议的状态,

那么您能帮我修改一下吗?

仅供参考,我有以下数据库设计:

Employee Table: Username, Name...
SafetySuggestionsLog: ID, Title, Description, Username, StatusID
SafetySuggestionsStatus: ID, Status
ASP.NET代码:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="900px" CssClass="mGrid" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut" 
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status" 
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server" 
                                        TargetControlID="btnModalPopUp" 
                                        PopupControlID="pnlPopUp" 
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle" 
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:Panel runat="server" ID="pnlPopUp">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm" 
                                OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')" 
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

//var safetySuggestionsId=

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;


        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

public void btnSendStatus_Click(object sender, EventArgs e) {
        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsStatus SET ID= @statusID where ID=@SafetySuggestionsID"";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {

                cmd.Parameters.AddWithValue("@ID", statusID);
                cmd.ExecuteNonQuery();
            }
        }

        SendSuggestionStatusToUser(statusID);
    }
SqlException was unhandled by user code 
 Must declare the scalar variable "@SafetySuggestionsID"
更新:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="900px" CssClass="mGrid" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut" 
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status" 
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server" 
                                        TargetControlID="btnModalPopUp" 
                                        PopupControlID="pnlPopUp" 
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle" 
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:Panel runat="server" ID="pnlPopUp">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm" 
                                OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')" 
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

//var safetySuggestionsId=

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;


        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

public void btnSendStatus_Click(object sender, EventArgs e) {
        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsStatus SET ID= @statusID where ID=@SafetySuggestionsID"";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {

                cmd.Parameters.AddWithValue("@ID", statusID);
                cmd.ExecuteNonQuery();
            }
        }

        SendSuggestionStatusToUser(statusID);
    }
SqlException was unhandled by user code 
 Must declare the scalar variable "@SafetySuggestionsID"
调试代码时,出现以下错误:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="900px" CssClass="mGrid" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut" 
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status" 
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server" 
                                        TargetControlID="btnModalPopUp" 
                                        PopupControlID="pnlPopUp" 
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle" 
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:Panel runat="server" ID="pnlPopUp">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm" 
                                OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')" 
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

//var safetySuggestionsId=

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;


        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

public void btnSendStatus_Click(object sender, EventArgs e) {
        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsStatus SET ID= @statusID where ID=@SafetySuggestionsID"";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {

                cmd.Parameters.AddWithValue("@ID", statusID);
                cmd.ExecuteNonQuery();
            }
        }

        SendSuggestionStatusToUser(statusID);
    }
SqlException was unhandled by user code 
 Must declare the scalar variable "@SafetySuggestionsID"
更新2:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="900px" CssClass="mGrid" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut" 
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status" 
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server" 
                                        TargetControlID="btnModalPopUp" 
                                        PopupControlID="pnlPopUp" 
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle" 
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:Panel runat="server" ID="pnlPopUp">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm" 
                                OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')" 
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

//var safetySuggestionsId=

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;


        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

public void btnSendStatus_Click(object sender, EventArgs e) {
        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsStatus SET ID= @statusID where ID=@SafetySuggestionsID"";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {

                cmd.Parameters.AddWithValue("@ID", statusID);
                cmd.ExecuteNonQuery();
            }
        }

        SendSuggestionStatusToUser(statusID);
    }
SqlException was unhandled by user code 
 Must declare the scalar variable "@SafetySuggestionsID"
我按照您的建议修改了代码:

protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

        //var safetySuggestionsId = 

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;

        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        HiddenField1.Value = gvrow.RowIndex.ToString();

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

    public void btnSendStatus_Click(object sender, EventArgs e) {

        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.AddWithValue("@ID", Convert.ToInt32(statusID));
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }

        //SendSuggestionStatusToUser(statusID);
    }
但是,在调试代码时,出现以下错误:

必须声明标量变量“@statusID”

我不知道为什么我已经定义了这个错误,却得到了这个错误

更新3:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
                        AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
                        width="900px" CssClass="mGrid" 
                        DataSourceID="SqlDataSource1" 
                        OnRowDataBound="GridView1_RowDataBound">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" CssClass="alt" />
            <HeaderStyle Font-Bold = "True" ForeColor="Black" Height="20px"/> 
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                    ReadOnly="True" SortExpression="ID" />
                <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Username" HeaderText="Username" 
                    SortExpression="Username" />
                <asp:BoundField DataField="DivisionShortcut" HeaderText="DivisionShortcut" 
                    SortExpression="DivisionShortcut" />
                <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />

                <%-- This to make status be opened and edited through the Ajax ModalPopUp Window --%>
                <asp:TemplateField HeaderText="Status">
                    <ItemTemplate>
                        <asp:LinkButton runat="server" ID="lnkSuggestionStatus" Text='<%#Eval("Status")%>'
                                        OnClick="lnkSuggestionStatus_Click">
                        </asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>

                <%--<asp:HyperLinkField HeaderText="Status" 
                    SortExpression="Status" />--%>
            </Columns>
            <RowStyle HorizontalAlign="Center" />
        </asp:GridView>

        <asp:Button runat="server" ID="btnModalPopUp" style="display:none" />

        <AjaxToolkit:ModalPopUpExtender ID="modalPopUpExtender1"
                                        runat="server" 
                                        TargetControlID="btnModalPopUp" 
                                        PopupControlID="pnlPopUp" 
                                        BackgroundCssClass="popUpStyle"
                                        PopupDragHandleControlID="panelDragHandle" 
                                        OkControlID="OKButton">
        </AjaxToolkit:ModalPopUpExtender>

        <asp:Panel runat="server" ID="pnlPopUp">

                    <asp:RadioButtonList ID="StatusList" runat="server" RepeatColumns="1" RepeatDirection="Vertical"
                                            RepeatLayout="Table" TextAlign="Left" DataSourceID="SuggestionStatusDataSource"
                                            DataTextField="Status" DataValueField="ID">
                        <asp:ListItem id="option1" runat="server" Value="ACTIONED" />
                        <asp:ListItem id="option2" runat="server" Value="APPROVED" />
                        <asp:ListItem id="option3" runat="server" Value="PENDING" />
                        <asp:ListItem id="option4" runat="server" Value="TRANSFERRED" />
                    </asp:RadioButtonList>
                    <asp:SqlDataSource ID="SuggestionStatusDataSource" runat="server"
                                        ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
                                        SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"></asp:SqlDataSource>

                    <asp:Button ID="confirmButton" runat="server" Text="Confirm" 
                                OnClientClick="javascript:return confirm('Are you sure you want to send an email notification about the safety suggestion to the owner?')" 
                                OnClick="btnSendStatus_Click" />

            <asp:Button ID="OKButton" runat="server" Text="Close" />
        </asp:Panel>
        </ContentTemplate>
        </asp:UpdatePanel>
protected void lnkSuggestionStatus_Click(object sender, EventArgs e)
    {
        LinkButton lnkSuggestionStatus = sender as LinkButton;

//var safetySuggestionsId=

        //get reference to the row selected 
        GridViewRow gvrow = (GridViewRow)lnkSuggestionStatus.NamingContainer;


        //set the selected index to the selected row so that the selected row will be highlighted
        GridView1.SelectedIndex = gvrow.RowIndex;

        //show the modalPopUp
        modalPopUpExtender1.Show();
    }

public void btnSendStatus_Click(object sender, EventArgs e) {
        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsStatus SET ID= @statusID where ID=@SafetySuggestionsID"";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {

                cmd.Parameters.AddWithValue("@ID", statusID);
                cmd.ExecuteNonQuery();
            }
        }

        SendSuggestionStatusToUser(statusID);
    }
SqlException was unhandled by user code 
 Must declare the scalar variable "@SafetySuggestionsID"
我添加了
GridView1.DataBind()
以使用所选建议的更新状态更新GridView,但它对我不起作用

public void btnSendStatus_Click(object sender, EventArgs e) {

        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }

        GridView1.DataBind();

        //SendSuggestionStatusToUser(statusID);
    }
更新4: 我添加了以下内容,但不起作用:

public void btnSendStatus_Click(object sender, EventArgs e) {

        var statusID = StatusList.SelectedValue;

        string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True";
        //For updating the status of the safety suggestion
        string updateCommand = "UPDATE SafetySuggestionsLog SET StatusID= @statusID where ID=@SafetySuggestionsID";
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))
            {
                cmd.Parameters.AddWithValue("@statusID", Convert.ToInt32(statusID));
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", Convert.ToInt32(HiddenField1.Value));
                cmd.ExecuteNonQuery();
            }
            //reset the value of hiddenfield
            HiddenField1.Value = "-1";
        }

        UpdatePanel1.Update();
        GridView1.DataBind();


        //SendSuggestionStatusToUser(statusID);
    }

看看这个示例项目


我看不到任何地方定义了
SafetySuggestionsID
,您已经将其定义为
statusID

另外,当您添加参数时,您指的是
ID
…请进行这些更正或更新查询中的代码

string updateCommand = "UPDATE SafetySuggestionsStatus SET ID= @statusID";  // where??
        using (SqlConnection conn = new SqlConnection(connString))  
        {  
            conn.Open();  
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))  
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))  
            {  

                cmd.Parameters.AddWithValue("@statusID", statusID);  
                cmd.ExecuteNonQuery();  
            }  
        }  
更新

"UPDATE SafetySuggestionsStatus SET ID= @statusID where ID=@SafetySuggestionsID"";
您已定义了2个参数,但仅在参数集合中添加了1个:

cmd.Parameters.AddWithValue("@ID", statusID); // id should be statusID
// also add @SafetySuggestionsID
更新2:
lnkSuggestionStatus\单击事件处理程序,获取第一列(ID)的值并将其存储在类变量safetySuggestionId中

然后在
btnSendStatus\u中单击事件处理程序,只需添加:

using (SqlConnection conn = new SqlConnection(connString))   
        {   
            conn.Open();   
            //using (SqlCommand cmd = new SqlCommand(cmdText, conn))   
            using (SqlCommand cmd = new SqlCommand(updateCommand, conn))   
            {   

                cmd.Parameters.AddWithValue("@statusID ", statusID); 
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", safetySuggestionsId);   
                cmd.ExecuteNonQuery();   
            }   
        } 
更新3: 希望以下内容能够起作用,因为这是未经测试的…:

  • 在模式弹出面板中添加隐藏字段-->

  • 然后在
    lnkSuggestionStatus\中单击事件处理程序,添加:

    hiddenRowIndex.Value=行。单元格[1]。文本;//必要时修剪

  • 然后在保存时,在
    btnSendStatus\中单击事件处理程序,只需添加:

           ...
           ...   
            {   
    
                cmd.Parameters.AddWithValue("@statusID ", statusID); 
                cmd.Parameters.AddWithValue("@SafetySuggestionsID", 
                                            Convert.ToInt32(hiddenRowIndex.Value));   
                cmd.ExecuteNonQuery();   
            } 
            // reset hiddenRowIndex
            hiddenRowIndex.Value = "-1";
        } 
    

  • 最后,我知道如何修复它。我的问题是HiddenField,我将它立即分配给所选行的索引,而没有指定要分配给该行的DataKey

    我只是需要在lnkSuggestionStatus_Click方法中修改这个:

    HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString();
    

    嗨,哥们,在我提问之前,我在网上搜索了一下,但我无法解决它。谢谢你的通知。我修改了代码,但仍然没有得到我想要的。你能帮我吗?我想我有点困惑。你能告诉我现在发生了什么吗?如果你能更新这个问题,让新的人看到它会有帮助,那就更好了。我知道这一点,但我不知道如何定义它。你知道吗?非常感谢。事实上,我知道这一步,我的问题是如何获得身份证?你能看一下我的更新代码并帮我获取身份证吗?我根据你的建议修改了代码,但仍然收到相同的错误。