2012-07-03 56 views
0

我是一名新的ASP.NET开发人员,我正在为我的公司开发一个基于Web的建议框程序,员工可以在其中提交任何安全建议。现在,我正在研究这个系统的管理部分。如何使用Ajax ModalPopUpExtender控件更新GridView中的特定列?

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

所以请你帮我修改吗?

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

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 [email protected]""; 
     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); 
    } 

UPDATE:

当我调试的代码,我得到了以下错误:

SqlException was unhandled by user code 
Must declare the scalar variable "@SafetySuggestionsID" 

更新2:

我修改了代码,如你所说:

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 [email protected]"; 
     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); 
    } 

但是,当我调试代码时,我去了t以下错误:

​​

我不知道为什么我得到这个错误,而我已经定义它。

更新3:

我加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 [email protected]"; 
     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); 
    } 

UPDATE 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 [email protected]"; 
     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); 
    } 

回答

1

我没有看到任何地方SafetySuggestionsID定义... u必须把它定义为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 [email protected]""; 

您已经定义了2个参数,但你只是1在参数colleciton补充说:

cmd.Parameters.AddWithValue("@ID", statusID); // id should be statusID 
// also add @SafetySuggestionsID 

更新2:lnkSuggestionStatus_Click事件处理程序,得到的第一列(ID)的值,并将它存储在类变量中,比如safeSuggestionsId。

然后在你的btnSendStatus_Click事件处理程序,你可以简单地添加:

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: 希望下面的作品,这是未经测试...:

  1. 添加在你的模式弹出面板隐藏字段 - >

  2. 然后在lnkSuggestionStatus_Click事件处理程序中,添加:

    hiddenRowIndex.Value = row.Cells [1]。文本; //修剪如有必要

  3. 然后当你保存在你的btnSendStatus_Click事件处理程序,你可以简单地添加:

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

感谢您的通知。我修改了代码,但仍然没有得到我想要的。你能帮我一下吗?我觉得我有点困惑。 – user1395782

+0

你可以更新现在发生的事情吗?如果你更新这个问题会更好,这样看到它的新人可以帮助 – NoviceProgrammer

+0

我更新了它并感谢你的支持 – user1395782

0

最后,我知道如何解决它。我的问题是与我立即没有指定要指派给该行的DataKey其分配给选定行的索引HiddenField。

我只是需要在lnkSuggestionStatus_Click方法来修改此:

HiddenField1.Value = GridView1.DataKeys[gvrow.RowIndex].Value.ToString(); 
相关问题