如何使用复选框从gridview删除记录 [英] How to delete the records from gridview using checkboxes

查看:75
本文介绍了如何使用复选框从gridview删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

正在使用C#,asp.net和SqlServer2005.

我有一个网格视图,其中填充了数据.

我正在使用标题"和逐行"复选框删除单个和多个记录.

这是我的设计页面.
======================

Dear Friends,

Am working on C# , asp.net , SqlServer 2005.

I have a gridview with data filled in it.

am using Header and row by row checkboxes to delete single and multiple records.

this is my design page.
=======================

<Columns>

            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="Checkall" runat="server"  onclick="javascript:SelectAll(this);" />
                                    </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="Chkselect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="UserName" HeaderText="UserName"

                    SortExpression="UserName" />
                <asp:BoundField DataField="Login_Time" HeaderText="Login_Time"

                    SortExpression="Login_Time" />
                <asp:BoundField DataField="Logout_Time" HeaderText="Logout_Time"

                    SortExpression="Logout_Time" />
            </Columns>




请有人帮我,如何通过选中复选框删除记录

我需要将所有记录都删除,然后将其删除,然后将其删除.

请帮帮我

谢谢.




Please can anybody help me, how to delete the records by selecting checkboxes

I need All records to be deleted and multiple deletion, and single deletion.

Please help me

Thanks.

推荐答案

请参阅已解决的答案:使用复选框删除GridView中的多行 [删除多行记录Gridview复选框确认 [删除GridView中的多行 [
Please refer solved answer: delete records in gridview using checkbox[^]
Also have look on:
Deleting multiple Rows in GridView using Checkbox[^]
Delete Multiple Rows Records Gridview CheckBox Confirmation[^]
Deleting Multiple Rows in a GridView[^]


http://technico.qnownow.com/2012/04/06/add-checkboxes-to- asp-net-gridview-control/ [ ^ ]
Steps to delete multiple records from gridview
http://www.dotnetfunda.com/articles/article1646-how-to-delete-multiple-selected-records-from-the-gridview.aspx[^]
http://technico.qnownow.com/2012/04/06/add-checkboxes-to-asp-net-gridview-control/[^]


像这样添加网格视图

Add Grid View Like this

<asp:GridView ID="GVID">
 <Columns>
<asp:TemplateField HeaderText="Select">
                                                                                                   <ItemTemplate>
                                                                                                       <asp:CheckBox ID="QchkBxSelect" runat="server" />
                                                                                                   </ItemTemplate>
                                                                                                   <HeaderTemplate>
                                                                                                       <asp:CheckBox ID="QchkBxHeader" onclick="javascript:QHeaderClick(this);" runat="server" />
                                                                                                   </HeaderTemplate>
                                                                                               </asp:TemplateField>
<asp:TemplateField HeaderText="Risk ID" meta:resourcekey="asptempriskid">
                                                                                                   <ItemTemplate>
                                                                                                       <asp:Label ID="lbluniqueId" Text='<%# Eval("uniqueID") %>' runat="server"></asp:Label>
                                                                                                   </ItemTemplate>
                                                                                               </asp:TemplateField>

</Columns>
                                                                                                                   </asp:GridView>





调用Java脚本以选择取消选择"复选框






Call The Java Script to Select DeSelect Checkbox


<script type="text/javascript">
var QTotalChkBx;
        var QCounter;


        window.onload = function () {
            //Get total no. of CheckBoxes in side the GridView.

            QTotalChkBx = parseInt('<%= this.GVID.Rows.Count %>');

            //Get total no. of checked CheckBoxes in side the GridView.

            QCounter = 0;
        }
function QHeaderClick(QCheckBox) {
            //Get target base & child control.
            var QTargetBaseControl =
       document.getElementById('<%= this.GVID.ClientID %>');
            var QTargetChildControl = "QchkBxSelect";

            //Get all the control of the type INPUT in the base control.
            var QInputs = QTargetBaseControl.getElementsByTagName("input");

            //Checked/Unchecked all the checkBoxes in side the GridView.
            for (var n = 0; n < QInputs.length; ++n)
                if (QInputs[n].type == 'checkbox' &&
                QInputs[n].id.indexOf(QTargetChildControl, 0) >= 0)
                    QInputs[n].checked = QCheckBox.checked;

            //Reset Counter
            QCounter = QCheckBox.checked ? QTotalChkBx : 0;
        }

        function QChildClick(QCheckBox, QHCheckBox) {
            //get target control.
            var QHeaderCheckBox = document.getElementById(QHCheckBox);

            //Modifiy Counter;
            if (QCheckBox.checked && QCounter < QTotalChkBx)
                QCounter++;
            else if (QCounter > 0)
                QCounter--;

            //Change state of the header CheckBox.
            if (QCounter < QTotalChkBx)
                QHeaderCheckBox.checked = false;
            else if (QCounter == QTotalChkBx)
                QHeaderCheckBox.checked = true;
        }


    </script>


像这样在网格视图外部添加Asp按钮


add Asp Button Outside Grid View Like this

<asp:Button ID="Delete" runat="server" Text="Delete "
                                                                                            OnClick="Delete_Click" />



在删除按钮onClick事件中的代码"中尝试以下操作

字符串flagchecked ="0";
ArrayList arrrid = new ArrayList();
//从GridView控件中选择复选框
for(int i = 0; i< GVID.Rows.Count; i ++)
{
GridViewRow行= GVID.Rows [i];
bool isChecked =((CheckBox)row.FindControl("chkBxSelect")).Checked;

如果(已检查)
{
flagchecked ="1";
字符串uniqueid =((Label)row.FindControl("lblitemuniqueId")).Text;
arrrid.Add(uniqueid);
}
}
如果(flagchecked =="1")
{
for(int jR = 0; jR< arrrid.Count; jR ++)
{
//删除操作

}
}



Try this below Code In Delete Button onClick Event

string flagchecked = "0";
ArrayList arrrid = new ArrayList();
// Select the checkboxes from the GridView control
for (int i = 0; i < GVID.Rows.Count; i++)
{
GridViewRow row = GVID.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkBxSelect")).Checked;

if (isChecked)
{
flagchecked = "1";
string uniqueid= ((Label)row.FindControl("lblitemuniqueId")).Text;
arrrid.Add(uniqueid);
}
}
if (flagchecked == "1")
{
for (int jR = 0; jR < arrrid.Count; jR++)
{
//Delete Operation

}
}


这篇关于如何使用复选框从gridview删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆