开发者

How to delete all selected checkbox value and there particular row?

开发者 https://www.devze.com 2023-03-06 22:55 出处:网络
i have a table which has only title field. Iam not using ID field in this table as its not required.

i have a table which has only title field. I am not using ID field in this table as its not required. Now on my front page i have a grid view which display title and there is a column item template check box. I want to delete all selected checkbox value and there particular row. How to do it.? Anybody can suggest me that... Thanks In advance... i am doing..

    private void GetData()
    {
        grdlistWord.
        ArrayList arr;
        if (ViewState["TotalRecords"] != null)
        {
            arr = (ArrayList)ViewState["TotalRecords"];
        }
        else
        {
            arr = new ArrayList();
        }
        for (int i = 0; i < grdlistWord.Rows.Count; i++)
        {

            CheckBox chk = (CheckBox)grdlistWord.Rows[i].Cells[0].FindControl("chkWord");
            if (chk.Checked)
            {
                if (!arr.Contains(grdlistWord.DataKeys[i].Value))
                {
                    arr.Add(grdlistWord.DataKeys[i].Value);
                }
            }
            else
            {
                if (arr.Contains(grdlistWord.DataKeys[i].Value))
                {
                    arr.Remove(grdlistWord.DataKeys[i].Value);
                }
            }
            //}
        }
        ViewState["TotalRecords"] = arr;
    }

    protected void lnkbtnDelete_Click(object sender, EventArgs e)
    {
        {
            try
            {
                int count = 0;
                //SetData();
                //gvAll.AllowPaging = false;
                //gvAll.DataBind();
                ArrayList arr = (ArrayList)ViewState["TotalRecords"];
                count = arr.Count;
                for (int i = 0; i < grdlistWord.Rows.Count; i++)
                {
                    //string a = grdlistWord.Rows.Count.ToString();
                    //Response.Write(a.ToString());
                    if (arr.Contains(grdlistWord.DataKeys[i].Value))
                    {

                        DeleteRecord(grdlistWord.DataKeys[i].Value.ToString());
                        arr.Remove(grdlistWord.DataKeys[i].Value);
                    }
                }
                ViewState["TotalRecords"] = arr;
                //hfCount.Value = "0";
                //gvAll.AllowPaging = true;
                GridBind();
                //ShowMessage(count);
            }
            catch (SqlException ex)
            {
    开发者_高级运维            ex.ToString();
            }
        }
    }


    private void DeleteRecord(string word)
    {

        string query = "delete from searchword where word=@word";

        SqlCommand cmd = new SqlCommand(query, con);
        cmd.Parameters.AddWithValue("@word", word);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }

HTML Code:

Word:
    <fieldset>
        <legend>List</legend>
        <asp:GridView ID="grdlistWord" runat="server" DataKeyNames="word" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" OnRowDataBound="grdlistWord_RowDataBound" OnRowDeleting="grdlistWord_RowDeleting" Width="456px" >
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkWord" runat="server" onclick="check_click(this);" OnCheckedChanged="chkWord_CheckedChanged" />

                    </ItemTemplate>
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkAll" runat="server" onclick="checkAll(this);" />
                        <asp:LinkButton ID="lnkbtnDelete" runat="server" Text="Delete" OnClick="lnkbtnDelete_Click" ForeColor="white"></asp:LinkButton>
                    </HeaderTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Word" HeaderText="Word" />
                <asp:HyperLinkField HeaderText="Edit" Text="edit" DataNavigateUrlFields="Word" DataNavigateUrlFormatString="SearchWord.aspx?words={0}&amp;mode=Edit" />
                <asp:CommandField ShowDeleteButton="True" HeaderText="Delete" />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EmptyDataTemplate>Records not exist!</EmptyDataTemplate>
        </asp:GridView>
        <asp:HiddenField ID="hfCount" runat="server" Value = "0" />

    </fieldset>


This page looks like it has an answer for you - difficult to say though with the limited amount of info you've give in the question.


You will need to handle the CheckedChanged event of the CheckBox and retrieve the corresponding title value in that row. Then write SQL code to delete all rows in the table whose title equals this title value. Use ExecuteNonQuery with the SqlCommand object to run this statement and perform the deletion.

0

精彩评论

暂无评论...
验证码 换一张
取 消