如何在ASP.NET GridView中添加复选框列,以便可以检查行并单击按钮将其删除? [英] how can I add a checkbox column in an ASP.NET GridView so that I can check a row and hit a button to delete it?

查看:36
本文介绍了如何在ASP.NET GridView中添加复选框列,以便可以检查行并单击按钮将其删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在ASP.NET GridView中添加复选框列,以便可以检查行并单击按钮将其删除?

how can I add a checkbox column in an ASP.NET GridView so that I can check a row and hit a button to delete it?

推荐答案

您可以使用javascript这样的gridview中的所有复选框


< pre lang ="cs">

函数SelectAllCheckboxes(spanChk)
{
var oItem = spanChk.children;
var theBox =(spanChk.type ==& quot; checkbox& quot;)?
spanChk:spanChk.children.item [0];
xState = theBox.checked;
elm = theBox.form.elements;

for(i = 0; i& lt; elm.length; i ++)
if(elm [i] .type ==&"checkbox&"&& amp;
elm [i] .id!= theBox.id)
{
if(elm [i] .checked!= xState)
elm [i] .click();
}
}


</pre>



这是带有复选框列的gridview ::-

for selecting all the check boxes in gridview you can use javascript like this


<pre lang="cs">

function SelectAllCheckboxes(spanChk)
{
var oItem = spanChk.children;
var theBox= (spanChk.type==&quot;checkbox&quot;) ?
spanChk : spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;

for(i=0;i&lt;elm.length;i++)
if(elm[i].type==&quot;checkbox&quot; &amp;&amp;
elm[i].id!=theBox.id)
{
if(elm[i].checked!=xState)
elm[i].click();
}
}


</pre>



this is the gridview with checkbox column ::-

<asp:GridView ID="gvCity" runat="server" AutoGenerateColumns="False" Width="99%"

                                    OnRowCommand="gvCity_RowCommand" OnRowEditing="gvCity_RowEditing" OnRowDeleting="gvCity_RowDeleting"

                                    AllowPaging="True" OnPageIndexChanging="gvCity_PageIndexChanging" CssClass="allgrids"

                                    HeaderStyle-BackColor="#ededec">
                                    <PagerSettings FirstPageText="First" NextPageText="Next" PreviousPageText="Previous"

                                        LastPageText="Last" />
                                    <PagerStyle HorizontalAlign="Right" />
                                    <Columns>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server"

                                                    type="checkbox" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chkSelect" runat="server" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="City">
                                            <ItemTemplate>
                                                <asp:Label ID="lblcity" runat="server" Text='<%#Eval("CITY_NAME") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="State">
                                            <ItemTemplate>
                                                <asp:Label ID="lblState" runat="server" Text="Andhra Pradesh" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Country">
                                            <ItemTemplate>
                                                <asp:Label ID="lblCountry" runat="server" Text="India" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Date Of Creation">
                                            <ItemTemplate>
                                                <asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Actions">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" ForeColor="Blue" CommandName="Edit"

                                                    CommandArgument='<%#Eval("CITY_ID") %>' />
                                                <asp:LinkButton ID="lbtnDelete" runat="server" Text="Delete" ForeColor="Blue" CommandName="Delete"

                                                    CommandArgument='<%#Eval("CITY_ID") %>' OnClientClick="return confirm('Are you sure you want to delete ?');" />

                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>


我获得了约233,000个结果(0.11秒).
删除多行记录Gridview复选框确认
在GridView中检查所有复选框
I got About 233,000 results (0.11 seconds).
Enjoy google

Delete Multiple Rows Records Gridview CheckBox Confirmation
Checking All CheckBoxes in a GridView


您好,

我已为您提供了一些示例代码.

一次检查一次

Hi,

I ''ve some sample code for your requirement.

check this once

 <asp:datalist id="DataList1" runat="server" width="400" xmlns:asp="#unknown">
 <HeaderTemplate >
   <table width="100%" align="center">
      <tr>
        <td>ID</td>
        <td>Name</td>
        <td>Operation</td>
      </tr>

 </HeaderTemplate>
 <itemtemplate>
      <tr>
        <td>
            <asp:label id="Label1" runat="server" text="<%#Eval("id") %>"></asp:label></td>
        <td>
            <asp:label id="Label2" runat="server" text="<%#Eval("name") %>"></asp:label></td>
        <td>
            <asp:checkbox id="CheckBox1" runat="server" /></td>
      </tr>
 </itemtemplate>
 <footertemplate>
  </footertemplate></table>

</asp:datalist>



并在按钮单击事件中编写以下代码



And write following code in button click event

strin cnt=String.Empty;

foreach (DataListItem dti in DataList1.Items)
{
    if (((CheckBox)dti.FindControl("CheckBox1")).Checked)
    {
         if(cnt.Length>0)
         {
           cnt=cnt+","+((Label)dti.FindControl("Label1")).Text ;
          }
          else
          {
          cnt=((Label)dti.FindControl("Label1")).Text ;
          }
    }
}



在上面的代码中,您将获得在数据控件中检查的所有ID.

通过使用这些,您可以编写SQL查询来删除它们

您的查询就像...



in the above code you''ll get all ids which are checked in data control.

By using those you can write your sql query for delete them

your query just like...

string qury="delete from <tablename> where id in ("+cnt+")";
</tablename>



这样您就可以满足您的要求.

最好的



then you can achieve your requirement.

All the Best


这篇关于如何在ASP.NET GridView中添加复选框列,以便可以检查行并单击按钮将其删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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