1

我有一个允许编辑和删除的列表视图。当我单击列表视图中的链接按钮时,它会启动 page_load 然后到 OnItemCommand。在命令结束时,我添加了 DataBind,它不会刷新我的列表视图但删除了我的条目。如果我将 DataBind 更改为使用 (...aspx?ID=...) 重定向回同一页面,它将返回一个全新的页面。但在调试模式下,我看到它通过 page_load 和 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>

代码背后

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

1 回答 1

0

在我们开始之前 - 您的代码存在安全风险,因为它容易发生Sql Injection,请确保您参数化您的查询......

现在,您的问题不是很清楚,但如果我理解正确,您是说在删除对象后,回发后不会刷新 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();
}
于 2012-12-19T15:03:24.057 回答