2012-03-10 216 views
2

我正在開發一個ASP.NET內聯網Web應用程序,它是一個測驗引擎。由於我是一名新的ASP.NET開發人員,因此我有一張表格,顯示從用戶收到的一些反饋,並且管理員可以使用DropDownList接受或拒絕這些反饋。如何刷新表而不刷新整個頁面?

在這張表下,有一個JQuery手風琴,當管理員點擊它時,他會看到一張表,並在最近三個月收到反饋。但是,當管理員接受或拒絕其中一個反饋,然後單擊「手風琴」時,即使反饋是上個月提交的反饋之一,他也會看到該表,但沒有反饋。但是,當您刷新頁面並點擊手風琴時,他會看到它。

那麼有什麼功能可以幫助我刷新整個頁面來獲得自動刷新表嗎? 請幫忙嗎?

ASP.NET代碼:

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
         AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" 
         width="950px" CssClass="mGrid" 
         AlternatingRowStyle-CssClass="alt" 
         RowStyle-HorizontalAlign="Center" 
         DataSourceID="SqlDataSource1" 
         OnRowDataBound="GridView1_RowDataBound" > 
      <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
      <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="Division" 
        SortExpression="DivisionShortcut" /> 
       <asp:TemplateField HeaderText="Status"> 
        <ItemTemplate> 
         <asp:DropDownList ID="DropDownList" runat="server" DataSourceID="SqlDataSource2" 
              Font-Bold="True" ForeColor="#006666" AppendDataBoundItems="false" 
              DataTextField="Status" DataValueField="ID" AutoPostBack="true" 
              OnDataBound="DropDownList_DataBound" OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged"> 
         </asp:DropDownList> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
     </asp:GridView> 
     <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
      ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
      SelectCommand="SELECT  dbo.SafetySuggestionsLog.ID, dbo.SafetySuggestionsLog.Title, dbo.SafetySuggestionsLog.Description, dbo.employee.Name, dbo.SafetySuggestionsLog.Username, 
         dbo.Divisions.DivisionShortcut 
FROM   dbo.employee INNER JOIN 
         dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN 
         dbo.Divisions ON dbo.employee.DivisionCode = dbo.Divisions.SapCode" 
         FilterExpression="[DivisionShortcut] like '{0}%'"> 

         <FilterParameters> 
         <asp:ControlParameter ControlID="ddlDivision" Name="DivisionShortcut" 
               PropertyName="SelectedValue" Type="String" /> 
        </FilterParameters> 
     </asp:SqlDataSource> 

     <%--For the DropDownList--%> 
     <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
          ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
          SelectCommand="SELECT * FROM [SafetySuggestionsStatus]"> 
     </asp:SqlDataSource> 

     <%--Filtering by Division--%> 
     <asp:SqlDataSource ID="sqlDataSourceDivision" runat="server" 
     ConnectionString="<%$ ConnectionStrings:testConnectionString %>" 
     SelectCommand="SELECT [DivisionShortcut] FROM [Divisions]"></asp:SqlDataSource> 

<dl id="jfaq"> 
      <br /> 
       <dt>Safety Suggestions List (for the last three months)</dt> 
        <dd> 
         <br /> 
         <asp:Panel ID="Panel1" runat="server"> 
         <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
             AllowSorting="True" CssClass="mGrid" 
             AlternatingRowStyle-CssClass="alt" 
             RowStyle-HorizontalAlign="Center" 
             DataSourceID="SqlDataSource4"> 
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
          <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
          <Columns> 
           <asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month" 
            SortExpression="SubmittedMonth" ReadOnly="True" /> 
           <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
            SortExpression="DivisionShortcut" /> 
           <asp:BoundField DataField="Username" HeaderText="Username" 
            SortExpression="Username" /> 
           <asp:BoundField DataField="Name" HeaderText="Name" 
            SortExpression="Name" /> 
           <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 
           <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression="Description" /> 
           <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> 
           <asp:BoundField DataField="Status" HeaderText="Status" 
            SortExpression="Status" /> 
          </Columns> 
           <RowStyle HorizontalAlign="Center"></RowStyle> 
         </asp:GridView> 
         </asp:Panel> 
         <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
          ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT  TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted) 
         AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title, 
         dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status 
FROM   dbo.Divisions INNER JOIN 
         dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN 
         dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN 
         dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN 
         dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID 
WHERE  (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) &lt; 3) 
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC"> 
         </asp:SqlDataSource> 

         <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" /> 
         </dd> 
       </dl> 

代碼隱藏:

protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DropDownList ddl = (DropDownList)sender; 
     int suggestionStatus = int.Parse(ddl.SelectedValue); 
     GridViewRow row = (GridViewRow)ddl.NamingContainer; 
     string strID = GridView1.DataKeys[row.RowIndex]["ID"].ToString(); 
     int ID = Int32.Parse(strID); 
     //For inserting the status in the database 
     string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdbTest;Integrated Security=True"; 
     string updateCommand = "UPDATE SafetySuggestionsLog SET [StatusID] = @StatusID WHERE [ID] = @ID"; 
     using (SqlConnection conn = new SqlConnection(connString)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand(updateCommand, conn)) 
      { 
       cmd.Parameters.Clear(); 
       cmd.Parameters.AddWithValue("@StatusID", suggestionStatus); 
       cmd.Parameters.AddWithValue("@ID", ID); 
       cmd.ExecuteNonQuery(); 
      } 
      conn.Close(); 
     } 

    } 

UPDATE: 我使用的UpdatePanel控件得到部分更新了,以前<triggers>,但我得到以下錯誤,我不知道爲什麼:

在UpdatePanel的「UpdatePanel1」中找不到觸發器的關聯控件 'GridView2'上名爲'Click'的事件。

<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <Triggers> 
     <asp:AsyncPostBackTrigger ControlID="GridView2" EventName="Click" /> 
    </Triggers> 
    <ContentTemplate> 
     <dl id="jfaq"> 
      <br /> 
       <dt>Safety Suggestions List (for the last three months)</dt> 
        <dd> 
         <br /> 
         <asp:Panel ID="Panel1" runat="server"> 
         <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
             AllowSorting="True" CssClass="mGrid" 
             AlternatingRowStyle-CssClass="alt" 
             RowStyle-HorizontalAlign="Center" 
             DataSourceID="SqlDataSource4"> 
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
          <HeaderStyle Font-Bold = "true" ForeColor="Black" Height="20px"/> 
          <Columns> 
           <asp:BoundField DataField="SubmittedMonth" HeaderText="Submitted Month" 
            SortExpression="SubmittedMonth" ReadOnly="True" /> 
           <asp:BoundField DataField="DivisionShortcut" HeaderText="Division" 
            SortExpression="DivisionShortcut" /> 
           <asp:BoundField DataField="Username" HeaderText="Username" 
            SortExpression="Username" /> 
           <asp:BoundField DataField="Name" HeaderText="Name" 
            SortExpression="Name" /> 
           <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" /> 
           <asp:BoundField DataField="Description" HeaderText="Description" 
            SortExpression="Description" /> 
           <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" /> 
           <asp:BoundField DataField="Status" HeaderText="Status" 
            SortExpression="Status" /> 
          </Columns> 
           <RowStyle HorizontalAlign="Center"></RowStyle> 
         </asp:GridView> 
         </asp:Panel> 
         <asp:SqlDataSource ID="SqlDataSource4" runat="server" 
          ConnectionString="<%$ ConnectionStrings:testConnectionString %>" SelectCommand="SELECT  TOP (100) PERCENT LEFT(DATENAME(month, dbo.SafetySuggestionsLog.DateSubmitted), 3) + '-' + DATENAME(year, dbo.SafetySuggestionsLog.DateSubmitted) 
         AS SubmittedMonth, dbo.Divisions.DivisionShortcut, dbo.SafetySuggestionsLog.Username, dbo.employee.Name, dbo.SafetySuggestionsLog.Title, 
         dbo.SafetySuggestionsLog.Description, dbo.SafetySuggestionsType.Type, dbo.SafetySuggestionsStatus.Status 
FROM   dbo.Divisions INNER JOIN 
         dbo.employee ON dbo.Divisions.SapCode = dbo.employee.DivisionCode INNER JOIN 
         dbo.SafetySuggestionsLog ON dbo.employee.Username = dbo.SafetySuggestionsLog.Username INNER JOIN 
         dbo.SafetySuggestionsType ON dbo.SafetySuggestionsLog.TypeID = dbo.SafetySuggestionsType.ID INNER JOIN 
         dbo.SafetySuggestionsStatus ON dbo.SafetySuggestionsLog.StatusID = dbo.SafetySuggestionsStatus.ID 
WHERE  (DATEDIFF(month, dbo.SafetySuggestionsLog.DateSubmitted, GETDATE()) &lt; 3) 
ORDER BY dbo.SafetySuggestionsLog.DateSubmitted DESC"> 
         </asp:SqlDataSource> 

         <asp:Button ID="btnPrint" runat="server" Text="Print" OnClick="btnPrint_Click" /> 
         </dd> 
       </dl> 

     </ContentTemplate> 
    </asp:UpdatePanel> 

回答

1

在你提到的Click事件觸發,但在後面的代碼你只有DropDownList_SelectedIndexChanged事件。

相反,你可以觸發爲DropDownList SelectedIndexChanged事件作爲

<asp:AsyncPostBackTrigger ControlID="DropDownList" EventName="SelectedIndexChanged" /> 

你只需要背後觸發代碼中的一個有效的事件。

更新:由於SelectedIndexChanged更新數據庫,所以您需要刷新DataGrid以填充更新的數據。

在的SelectedIndexChanged方法的末尾添加這行代碼:

GridView2.DataBind(); 
+1

更新後只刷新網格。您可以使用GridView2.DataBind() – Maxali 2012-03-10 11:05:00

+0

謝謝。我非常感謝你的幫助。 – 2012-03-10 11:39:29

1

使用AJAX更新面板,這可能有助於http://ajax.net-tutorials.com/controls/updatepanel-control/

+0

感謝您的幫助。但是,你的意思是我應該只將Jquery Accordion放入UpdatePanel控件中?或者我也應該使用觸發器? – 2012-03-10 09:39:19

+0

你也應該使用觸發器.. – Karthik 2012-03-10 09:47:51

+0

你能看到我上面更新的問題嗎? – 2012-03-10 09:56:51