简体   繁体   中英

Cannot refresh Listview after ItemCommand event is fired

I've a listview that allows Editing and Delete. When I Click on the linkbutton in the listview, it fires up page_load then to OnItemCommand. At the end of the command I added DataBind Which does not refresh my listview but deleted my entry. If I change the DataBind to Redirect back to the same page with (...aspx?ID=...) it will return me a fresh new page. but in debug mode, I saw it run through page_load and the Databind.

<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>

Code Behind

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);
    }
}

Before we start - your code has a security risk since it is prone to Sql Injection , make sure you Parametrize your queries...

Now, your question is not very clear, but if I understand correctly, you're saying that after you delete an object, the Listview isn't refreshed after postback (eg you still see the deleted item). Since you're running in an UpdatePanel, make sure to update the panel after the DataBind...

Update PoolBindData like this

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM