2010-12-09 92 views
0

我创建了一个GridView,并在某些列前面有一个复选框。我需要获取用户正在忽略的数据并构建一个xml文件。ASP.NET Gridview - 复选框 - 选择多行并获取记录

我找不出来。有人可以帮助我在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> 

谢谢

史蒂夫

+0

你可以发布代码 - 你到目前为止尝试过吗? – 2010-12-09 00:45:22

回答

1

您可以使用下面的代码通过一个用于检查行获取值之一。

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 
    } 
    } 
0
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。当用户点击某个回传时,这非常违反直觉,令人沮丧。

相关问题