从gridview获取已检查值的列表 [英] Get a list of cheked values form the gridview

查看:72
本文介绍了从gridview获取已检查值的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带复选框的gridview,现在我想在选中特定复选框时获取有关gridview中特定行的信息.当我想要单行时它可以工作,但是当我想要一个数据列表时,它可以一一读取.选中标头复选框后,该按钮可以返回插槽ID的列表.请检查下面的代码

我的gridview

I have a gridview with checkboxes, Now i want to get information about a specific row in the gridview when i check specific checkbox. It works when i want a single row but it reads one by one when i want a list of data. The button supose to return a list of Slot ID''s when a header checkbox is checked.Check the code below

My gridview

<asp:GridView ID="grvSlots" runat="server" CellPadding="4" ForeColor="#333333"

           GridLines="None" Height="162px" Width="188px" DataKeyNames="ID"

           onrowdatabound="grvSlots_RowDataBound" onrowcreated="grvSlots_RowCreated">
           <AlternatingRowStyle BackColor="White" />

           <EditRowStyle BackColor="#2461BF" />
           <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
           <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
           <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
           <RowStyle BackColor="#EFF3FB" />
           <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
           <SortedAscendingCellStyle BackColor="#F5F7FB" />
           <SortedAscendingHeaderStyle BackColor="#6D95E1" />
           <SortedDescendingCellStyle BackColor="#E9EBEF" />
           <SortedDescendingHeaderStyle BackColor="#4870BE" />

           <Columns>
           <asp:TemplateField HeaderText="select">
           <ItemTemplate>
               <asp:CheckBox ID="CheckBox1" runat="server" Width="50px" />

           </ItemTemplate>
          <HeaderTemplate>
          <asp:CheckBox ID="chkAll" runat="server" Text="Select"

           oncheckedchanged="chkAll_CheckedChanged" /></td>
          </HeaderTemplate>
           </asp:TemplateField>

           </Columns>
       </asp:GridView>





按钮后面的代码





Code behind button

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow rowItem in grvSlots.Rows)
        {

            CheckBox chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox1"));




            if (chk.Checked)
            {
              System.Windows.Forms.MessageBox.Show( grvSlots.DataKeys[rowItem.RowIndex]["ID"].ToString());
            }
        }

    }



我的客户端中用于取消选中和选中框的代码



Code in my client side for unchecking and checking the boxes

<script type="text/javascript">
        function SelectAll(id) {
            var frm = document.forms[0];
            for (i = 0; i < frm.elements.length; i++) {

                if (frm.elements[i].type == "checkbox") {

                    frm.elements[i].checked = document.getElementById(id).checked;

                }

            }

        }

</script>




代码以选中或取消选中复选框

受保护的无效grvSlots_RowDataBound(对象发送者,System.Web.UI.WebControls.GridViewRowEventArgs e)
{
如果(e.Row.RowType == DataControlRowType.Header)
{
//在标题的复选框中为onclick事件添加属性
//并传递全选"复选框的ClientID
((CheckBox)e.Row.FindControl("chkAll")).Attributes.Add("onclick","javascript:SelectAll(""+(((CheckBox)e.Row.FindControl(" chkAll)).ClientID +'')");
}
}
受保护的void chkAll_CheckedChanged(对象发送者,EventArgs e)
{
StringCollection sc =新的StringCollection();
字符串ID = string.Empty;
如果(!Page.IsPostBack)
{
为(int i = 0; i< grvSlots.Rows.Count; i ++)
{
CheckBox chk =(CheckBox)grvSlots.Rows [0] .Cells [0] .FindControl("chkAll");
如果(chk!= null)
{
如果(chk.Checked)
{
id = grvSlots.Rows [0] .Cells [0] .Text;
sc.Add(id);
}
}
}
}
}


以下是我如何填充gridview




Code to check or uncheck the checkboxes

protected void grvSlots_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
//adding an attribute for onclick event on the check box in the header
//and passing the ClientID of the Select All checkbox
((CheckBox)e.Row.FindControl("chkAll")).Attributes.Add("onclick", "javascript:SelectAll(''" + ((CheckBox)e.Row.FindControl("chkAll")).ClientID + "'')");
}
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
if (!Page.IsPostBack)
{
for (int i = 0; i < grvSlots.Rows.Count; i++)
{
CheckBox chk = (CheckBox)grvSlots.Rows[0].Cells[0].FindControl("chkAll");
if (chk != null)
{
if (chk.Checked)
{
id = grvSlots.Rows[0].Cells[0].Text;
sc.Add(id);
}
}
}
}
}


Below is how I populate my gridview

public void loadGrvSlots()
    {
        DataTable dt = new DataTable();
        systemBusinessLayer = new BusinessLayer();

        dt = systemBusinessLayer.loadGrvSlots();
        grvSlots.DataSource = dt;
        grvSlots.DataBind();
    }




方法在我的课上加载gridview

公共DataTable loadGrvSlots()
{
使用(SqlConnection con = new SqlConnection(ConnString))
{
SqlCommand cmd =新的SqlCommand("procAllGetTimeSlots",con);
cmd.CommandType = CommandType.StoredProcedure;

DataTable dTable = new DataTable("TimeSlots");
SqlDataAdapter适配器=新的SqlDataAdapter(cmd);
adapter.Fill(dTable);
返回dTable;

}
}

最后是我的存储过程




Method In my class to load gridview

public DataTable loadGrvSlots()
{
using (SqlConnection con = new SqlConnection(ConnString))
{
SqlCommand cmd = new SqlCommand("procAllGetTimeSlots", con);
cmd.CommandType = CommandType.StoredProcedure;

DataTable dTable = new DataTable("TimeSlots");
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dTable);
return dTable;

}
}

Lastly my stored procedure

ALTER PROCEDURE [dbo].[procAllGetTimeSlots]
AS
SELECT     SlotName, ID
FROM         TimeSlots





希望有足够的信息家伙





Hope thats enough info guys

推荐答案

用于标头复选框,您可以将"auotpostback"属性设置为true.
并最终从网格视图中获取所有数据.
for header Check box you can set the "auotpostback" property to true.
and at event get all data from grid view.


这篇关于从gridview获取已检查值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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