2012-12-19 78 views
1

我有一個允許編輯和刪除的列表視圖。 當我點擊列表視圖中的鏈接按鈕時,它啓動page_load,然後啓動OnItemCommand。 在命令的末尾添加了DataBind,它不會刷新我的列表視圖,而是刪除了我的條目。 如果我通過(... aspx?ID = ...)將DataBind重定向回同一頁面,它將返回一個新的新頁面。但在調試模式下,我看到它通過page_load和Databind運行。在ItemCommand事件被觸發後無法刷新Listview

<asp:UpdatePanel ID="UpdateOptions" runat="server" > 
    <ContentTemplate> 
     <asp:Panel ID="OPanel" runat="server" width="350px"> 
      <asp:ListView runat="server" ID="lvPollOptions" DataKeyNames="POptionID" OnItemCommand="lvPollOptions_ItemCommand" OnDataBound="lvPollOptions_ItemDataBound" > 
       <LayoutTemplate> 
        <table cellpadding="0" cellspacing="0" border="0" width="300px"> 
         <tr class="AdminListHeader"> 
         </tr> 

         <tr id="itemPlaceholder" runat="server"> 
         </tr> 

        </table> 
       </LayoutTemplate> 

       <ItemTemplate> 
        <tr> 
         <td> 
          <%#Eval("OptionText")%> 
         </td> 

         <td> 
          <%#Eval("Votes")%> 
         </td> 

         <td align="center"> 
          <asp:ImageButton runat="server" ID="ibtnEditOption" CommandArgument='<%# Eval("POptionID").ToString() %>' CommandName="Edit" ImageUrl="~/images/buttons/EditPencil.gif" AlternateText="Edit" CssClass="AdminImg" /> 
         </td> 

         <td> 
          <asp:ImageButton runat="server" ID="ibtnDeleteOption" CommandArgument='<%# Eval("POptionID").ToString() %>' CommandName="Delete" ImageUrl="~/images/buttons/delete.gif" AlternateText="Delete" CssClass="AdminImg" OnClientClick="return confirm('Warning: This will delete the Poll Option from the database.');" /> 
         </td> 
        </tr> 
       </ItemTemplate> 

      </asp:ListView> 

      <asp:Label ID="lblNoOption" runat="server" Text="No Options Added"></asp:Label> 

      <table width="345px"> 
       <tr> 
        <td width="100px"> 
         Option: 
        </td> 

        <td> 
         <asp:TextBox ID="txtOption" runat="server" width="200px"></asp:TextBox> 
        </td> 
       </tr> 
      </table> 
     </asp:Panel> 
    </ContentTemplate> 
</asp:UpdatePanel> 

代碼隱藏

protected void PollBindData() 
{ 
    SqlConnection connOption = new SqlConnection(connStr); 
    connOption.Open(); 
    SqlDataAdapter da = new SqlDataAdapter("SELECT POptionID, OptionText, Votes FROM [PollOptions] Where PollID = '" + PID + "'", connOption); 
    DataSet dsSel = new DataSet(); 
    da.Fill(dsSel); 
    lvPollOptions.DataSource = dsSel; 
    lvPollOptions.DataBind(); 
    connOption.Close(); 
} 

protected void Page_Load(object sender, EventArgs e) 
{ 
    string PID = Request.QueryString["ID"]; 

    if (!IsPostBack) 
    { 
     if (PID == null) 
     { 

     } 
     else if (PID != null) 
     { 
      PollBindData(); 
     } 
    } 
} 

protected void lvPollOptions_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    string PID = Request.QueryString["ID"]; 

    SqlConnection connOption = new SqlConnection(connStr); 
    connOption.Open(); 
    SqlDataAdapter da = new SqlDataAdapter("SELECT POptionID, OptionText, Votes  FROM [PollOptions] Where PollID = '" + PID + "'", connOption); 
    DataSet dsSel = new DataSet(); 
    da.Fill(dsSel); 
    lvPollOptions.DataSource = dsSel; 
    lvPollOptions.DataBind(); 
    connOption.Close(); 
} 

protected void lvPollOptions_ItemCommand(object sender, ListViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Delete") 
    { 
     string selectedID = e.CommandArgument.ToString(); 
     SqlConnection connDeleteOption = new SqlConnection(connStr); 
     connDeleteOption.Open(); 
     SqlCommand cmdDeleteOption = new SqlCommand("DELETE FROM [PollOptions] WHERE POptionID = '" + selectedID + "'", connDeleteOption); 
     cmdDeleteOption.ExecuteNonQuery(); 
     connDeleteOption.Close(); 

     PollBindData(); 
     //Response.Redirect("aAddEditPoll.aspx?ID=" + selectedID); 
    } 

    if (e.CommandName == "Edit") 
    { 
     string EditID = e.CommandArgument.ToString(); 
     SqlConnection conn = new SqlConnection(connStr); 
     conn.Open(); 
     SqlCommand cmdView = new SqlCommand("SELECT OptionText From [PollOptions] Where POptionID = '" + EditID + "'", conn); 
     SqlDataReader dr1 = cmdView.ExecuteReader(); 

     if (dr1.Read()) 
     { 
      txtOption.Text = dr1["OptionText"].ToString(); 
     } 

     Session["OptionID"] = txtOption.Text; 
     dr1.Close(); 
     conn.Close(); 

     lbOInsert.Visible = false; 
     lbOUpdate.Visible = true; 

     PollBindData(); 
     //Response.Redirect("aAddEditPoll.aspx?ID=" + EditID); 
    } 
} 

回答

0

在我們開始之前 - 你的代碼有安全風險,因爲很容易出現Sql Injection,請確保您Parametrize您查詢......

現在,你的問題不是很清楚,但如果我理解正確,你說在刪除一個對象之後,Listview在回發後不刷新(例如。你仍然可以看到已刪除的項目)。既然你在一個UpdatePanel運行,確保更新後的DataBind面板...

更新PoolBindData這樣

protected void PollBindData() 
{ 
    SqlConnection connOption = new SqlConnection(connStr); 
    connOption.Open(); 

    // POTENTIAL SECURITY RISK - MAKE SURE YOU PARAMETRIZE THE QUERY!! 
    SqlDataAdapter da = new SqlDataAdapter("SELECT POptionID, OptionText, Votes FROM [PollOptions] Where PollID = '" + PID + "'", connOption); 

    DataTable dsSel = new DataTable(); 
    da.Fill(dsSel); 
    lvPollOptions.DataSource = dsSel; 
    lvPollOptions.DataBind(); 
    connOption.Close(); 

    // Update panel 
    UpdateOptions.Update(); 
} 
相關問題