繁体   English   中英

ASP.NET Gridview-复选框-选择多行并获取记录

[英]ASP.NET Gridview - Checkbox - Select multiple rows and get records

我创建了一个带有某些列前面的复选框的gridview。 我需要获取用户喜欢的数据并构建一个xml文件。

我不知道。 有人可以用C#帮助我吗?

到目前为止,这是我的代码。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource1"  AllowPaging="True" BackColor="#DEBA84" BorderColor="#DEBA84" 
BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" >
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

<Columns>       

<asp:TemplateField>
<HeaderStyle HorizontalAlign="left" />
<HeaderTemplate>
<asp:CheckBox ID="chkSelectAll" ToolTip="Click here to select/deselect all rows"
runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Service Point">
<ItemTemplate>                  
<%# Eval("SERVICEPOINTID")%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Start Date">
<ItemTemplate>
<%# Eval("STARTTIME")%>
</ItemTemplate>                 
</asp:TemplateField>

谢谢,

史蒂夫

您可以使用下面的代码来为已检查的行逐一获取值。

foreach (GridViewRow rowItem in GridView1.Rows)
  {
     var chk = (CheckBox)(rowItem.Cells[0].FindControl("chkSelectAll"));

     // chk.checked will access the checkbox state on button click event
     if (chk.Checked)
     {
         //get required values here
     }
  }
ForEach(GridViewRow row in MyGridView.Rows)
{
  if (row.RowType == DataControlRowType.DataRow) //avoid header/footer rows.
  {
    var myCheckBox = (CheckBox)row.FindControl("chkSelect");
    //myCheckBox.Checked tells you if it's checked or not, yay!
    var myPrimaryKey = (GuidOrIntOrSomething)MyGridView.DataKeys[row.RowIndex].Value;
    //now you have your Key and the checkbox for whether the user has checked it 
    //and you can do your update/insert/delete/whatever against the DB.
  }
}

并且您真的应该使用直接检查所有复选框来处理所有复选框所需要的令人讨厌的javascript。 用户单击某条回发是非常违反直觉和沮丧的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM