<asp:datagrid id="DataGrid_BanZu" runat="server" Width="648px" Height="96px" BorderStyle="Solid"
ToolTip="双击某条记录可以进行修改" ForeColor="White" CellPadding="4" BackColor="White" BorderWidth="1px"
BorderColor="Gray" AllowSorting="True" PageSize="5" AllowPaging="True" AllowCustomPaging="True"
AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="Aqua" BackColor="Khaki"></SelectedItemStyle>
<EditItemStyle BackColor="Blue"></EditItemStyle>
<AlternatingItemStyle CssClass="DataGrid_AlternatingItem"></AlternatingItemStyle>
<ItemStyle CssClass="DataGrid_Item"></ItemStyle>
<HeaderStyle Font-Size="X-Small" Font-Bold="True" BorderWidth="5px" ForeColor="Black" BackColor="Orange"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="选中">
<HeaderStyle HorizontalAlign="Center" Width="50px"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
<ItemTemplate>
<asp:CheckBox id="chkExport" AutoPostBack="False" Runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ID" HeaderText="编号">
<HeaderStyle Width="10%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Name" HeaderText="班组名称">
<HeaderStyle Width="40%"></HeaderStyle>
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="Description" HeaderText="描述信息">
<HeaderStyle Width="40%"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle Visible="False" Mode="NumericPages"></PagerStyle>
</asp:datagrid>
代码如下:
System.Web.UI.WebControls.CheckBox chkExport;
string strDel = "DELETE FROM tbl_BASE_TEAM WHERE ID = ";
int iCount = 0;
foreach(DataGridItem oDataGridItem in DataGrid_BanZu.Items)
{
chkExport = (CheckBox)oDataGridItem.FindControl("chkExport");
//Response.Write(Convert.ToString(chkExport.Checked));
TextBox_Description.Text += Convert.ToString(chkExport.Checked); if(chkExport.Checked)
{
if(iCount == 0)
{
strDel += oDataGridItem.Cells[1].Text.ToString();
}
else
{
strDel += " OR ID = ";
strDel += oDataGridItem.Cells[1].Text.ToString();
}
iCount++;
//((HtmlInputHidden)oDataGridItem.FindControl("SelectedID")).Value;
}
}我发现即便在datagrid中选中整个checkbox,
但是在if(chkExport.Checked) 语句中也总是返回false。 
请问这是为什么? 
谢谢。

解决方案 »

  1.   

    Page_Load事件, 对DataGrid绑定语句前面加上一个:if(!Page.IsPostBack)
    {
       ....;// 绑定DataGrid
    }
      

  2.   

    兄弟, 多谢!
    请问这个if(!Page.IsPostBack)到底是干什么的?
    谢谢。
      

  3.   

    byval e as datagridcommandeventargs
    有这个变量..
    如果checkbox在datagrid中的第一列的话
    dim ck as new checkbox
    ck=ctype(c.item.cell(0).findcontrol,checkbox)
    if (ck.checked)
    ///处理
    end if
    这样就行了
      

  4.   

    if(!page.ispostback){}就是看看是不是第一次加载该页面
      

  5.   

    呵呵,来晚了,
    解释一下if(!page.ispostback){}:
    ifpostback:回发(相当于第一次加载页面)
      

  6.   


        protected void btnDel_Click(object sender, EventArgs e)
        {
            try
            {
                for (int i = 0; i < this.gvSelfHelpPublicShare.Rows.Count; i++)
                {
                    if (((CheckBox)this.gvSelfHelpPublicShare.Rows[i].Cells[0].Controls[1]).Checked == true)
                    {
                        SelfHelpPublicShareID = UtilTool.ToSafeInt32(this.gvSelfHelpPublicShare.DataKeys[i].Value);                    new SelfHelpPublicShareManager().DeleteSelfHelpPublicShareByID(SelfHelpPublicShareID);
                    }
                }
                this.BindPageIndex(1);
            }        catch (Exception ex)
            {
                this.lblMessage.Text = ex.Message;
            }
        }
    if (((CheckBox)this.gvSelfHelpPublicShare.Rows[i].Cells[0].Controls[1]).Checked == true)
    判断是否选中!
      

  7.   

    我用这个
    if(!Page.IsPostBack)
    {
       ....;// 绑定DataGrid
    }搞定了