删除按钮是与DataGrid分开的下面是删除按钮事件:private void QKdelete_Click(object sender, System.EventArgs e)
{
    for(int i=1;i<=WGQKnum();i++)//WGQKnum()是DataGrid的记录个数
    {
if(((CheckBox)WGQK.Items[i].Cells[1].FindControl("BHQK")).Checked==true)
    {
        QKdelete.Attributes["onClick"]="alert(1)";
    }
    }
}
但是在运行到:
if(((CheckBox)WGQK.Items[i].Cells[1].FindControl("BHQK")).Checked==true)
的时候就无法运行,提示的错误为:未将对象引用设置到对象的实例。 我就是想判断被选中的复选框是否被选中,选中之后得到那个复选框的值,然后连接数据库删除...前台的DataGrid中的复选框我是用<input value='<%#DataBinder.Eval(...)%>'/>
代码:<asp:datagrid id="WGQK" style="Z-INDEX: 157; LEFT: 128px; POSITION: absolute; TOP: 80px" runat="server"
Width="1000px" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="选择">
<HeaderStyle HorizontalAlign="Center" Width="40px" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<span style="TEXT-ALIGN: center"><input type=checkbox id="BHQK" value='<%#DataBinder.Eval(Container.DataItem,"编号")%>'>
</span>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="检查时间">
<HeaderStyle HorizontalAlign="Center" Width="80px" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"检查时间")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="检查内容">
<HeaderStyle HorizontalAlign="Center" Width="100px" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"检查内容")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="检查情况">
<HeaderStyle HorizontalAlign="Center" Width="100px" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"检查情况")%>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="处理过程及结果">
<HeaderStyle HorizontalAlign="Center" Width="680px" VerticalAlign="Middle" Font-Bold="True"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle"></ItemStyle>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"处理过程及结果")%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>



}
判断复选框是否被选中应该怎样书写
如果实在不行我用javascript将值GET的方式传入下一个页面,在载入的时候处理

解决方案 »

  1.   

    //是否选中
    bool flag=false;
    for(int n=0;n<WGQK.Items.Count;n++)
    {
    CheckBox cb=(CheckBox)WGQK.Items[n].Cells[0].FindControl("BHQK");
    if(cb.Checked)
    {
    flag=true;
    }
    }
    //选中时,
    if(flag)
    {
    for(int n=0;n<WGQK.Items.Count;n++)
    {
    CheckBox cb=(CheckBox)WGQK.Items[n].Cells[0].FindControl("BHQK");
    if(cb.Checked)
    {
    int id=int.Parse(WGQK.DataKeys[WGQK.Items[n].ItemIndex].ToString());
    hy.id=id;
    hy.DeleteOne();
    }
    }
    }
    //未选中时
    else
    {
    this.Page.RegisterStartupScript("","<script>alert('您没有选择任何一项!')</script>");
    }
      

  2.   

    hy.id=id;
    hy.DeleteOne();
    是什么意思,hy是什么??
      

  3.   

    if(ch.Checked)
    这个地方会报错....
      

  4.   

    前台就是一个Datagrid与显示的数据,为什么判断是否选中的时候会报错??
      

  5.   

    但是在运行到:
    if(((CheckBox)WGQK.Items[i].Cells[1].FindControl("BHQK")).Checked==true)
    的时候就无法运行,提示的错误为:未将对象引用设置到对象的实例。 、、、、、、、、、、
    有可能是因为就没有找到这个CheckBox
      

  6.   

    CheckBox cb=(CheckBox)WGQK.Items[n].Cells[0].FindControl("BHQK");
    if(cb.Checked)
    {
       ... ...
    }
    我换成这样也无法找到
    应该怎样写
    DataGrid的行与列也是从0开始嘛
      

  7.   

    会不会是因为你的"<input type=checkbox id="BHQK" value='<%#DataBinder.Eval(Container.DataItem,"编号")%>'> 是input的?
      

  8.   

    就是因为不能使用input的原因,input后面加了runat=server也没用
    我使用了asp:checkbox之后,输出了数据,选中了复选框还是不能进入if(cb.Checked),添加了断点查看到我选中的那个复选框的状态为false,为什么???
      

  9.   

    怎样查询判断前台为input的复选框?
      

  10.   

    访问其中的datagrid控件的子控件你可以参考一下访问gridview(2005中替换datagrid)
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                for (int i = 0; i < e.Row.Cells.Count; i++)
                {
                    if (e.Row.Cells[i].Controls.Count > 0)
                    {
                        try
                        {
                            TextBox t = (TextBox)e.Row.Cells[i].Controls[0];
                            t.BackColor = System.Drawing.Color.Red;
                            if (i == 1)
                                t.Attributes.Add("onfocus", "setday(this)");
                            t.Width = 150;
                        }
                        catch (Exception ex)
                        {
                            //Response.Write(ex.ToString());
                        }                }
                }        }
    }
      

  11.   

    patriot074() ( ) 信誉:100  
    好像是不能得到input的值呢