在带有两个复选框的asp.net中冒泡 [英] bubbling in asp.net with two checkboxes

查看:72
本文介绍了在带有两个复选框的asp.net中冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Gridview中实现一个简单的示例,其中在Gridview中有两个checkboxes(headertemplate("chkall")itemtemplate("chksingle")).我在SQL Server 2008中使用C#4.0.

在这个简单的程序中,我遇到两个问题:
a)每当我尝试检查/取消选中chkall(其autopostback属性设置为"true")时,它都不会工作.

b)选中某些复选框,然后单击Web窗体上某处的删除"按钮,它不起作用...
请帮助我纠正以下代码....

Hi, i am trying to implement a simple example of bubbling in Gridview WITH two checkboxes(headertemplate("chkall") and itemtemplate("chksingle")) in Gridview. I m using c# 4.0 with sql server 2008.

I have two issues in this simple program:
a) whenever i am trying to check/uncheck the chkall(whose autopostback proerty is set to ''true''), it does nt work.

b) WHen i chk on some checkboxes, and click on a ''delete'' button somewhere on the web-form, it doesnot work...
please help me rectifying the following code....

protected void chkall_CheckedChanged(object sender, EventArgs e)
        {
            foreach (GridViewRow row in GridView1.Rows)
            {
        CheckBox chksingle = (CheckBox)row.Cells[0].FindControl("chksingle");
         chksingle.Checked =  ((CheckBox)sender).Checked;
         }
      }


      void DBind()
        {
     con = new SqlConnection("server=.;database=shikhar;integrated security=true");
     adap = new SqlDataAdapter("select * from emp", con);
     ds = new DataSet();
     adap.Fill(ds, "emp");
     GridView2.DataSource = ds.Tables[0];
     GridView2.DataBind();
     con.Close();
      }


        protected void Button2_Click(object sender, EventArgs e)
        {
 SqlConnection con = new SqlConnection("server=.;database=shikhar;integrated security=true");
      con.Open();
      foreach (GridViewRow row in GridView2.Rows)
            {
   CheckBox chksingle = row.Cells[0].FindControl("chksingle") as CheckBox;
             if (chksingle.Checked)
                {
  SqlCommand cmd = new SqlCommand("delete employee where id=''"+row.Cells[1].Text+"''",con);
              
                    cmd.ExecuteNonQuery();
                }
            }
            con.Close();
            con.Dispose();
            DBind();
        }
    }



[Edited]代码被包装在"pre"标签中[/Edited]
[edit]紧急情况已删除:这可能对您来说很紧急,但对我们而言并非如此.您所强调的紧迫性只是使我们认为您离开得太晚了,并希望我们为您做这件事.这使某些人烦恼,并且可能减慢响应速度. -OriginalGriff [/edit]



Code is wrapped in "pre" tags[/Edited]
[edit]Urgency deleted: It may be urgent to you, but it isn''t to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response. - OriginalGriff[/edit]

推荐答案

HI shikhar在建议您进行一些更改之前,我需要您提供一些信息,这些内容如下.

1]您的gridview控件是否在UpdatePanel中.如果是,请检查是否为gridcontrol设置了AsyncPostBackTrigger.如果不是,请先进行设置,然后它应该可以工作.

2]您是否为复选框控件设置了onCheckedchange事件,因为我们无法通过双击直接设置它.我们需要显式复制该事件名称,并将其粘贴到控件的源代码中.

3]如果给定的概率已经存在,请在注释复选框事件后尝试运行代码,看看是否抛出任何异常.

4]如果您将事件分配给控件,请检查如何分配它.它应类似于下面的示例.OncheckedChanged="chkAll_CheckedChanged"

尝试那些检查可能对您有帮助.请尝试编程.
HI shikhar before suggest you some changes i need some information from your end Those are as follows.

1]Is your gridview control is in UpdatePanel.if yes then check that have you set the AsyncPostBackTrigger for gridcontrol or not.if no then please set it first then it should work.

2]Did you set the onCheckedchange event for the checkbox control or not because we won''t be able to set it directly by double clicking it.we need to explicitly copy that event name and past it in source code.for the control.

3]if given probability are already in place then try to run your code after commenting the event of checkbox and see is there any exception are thrown or not.

4] if you assign an event to the control then check how you assign it.It should like the given below example.OncheckedChanged="chkAll_CheckedChanged"

Try those checks might be those will be helpffull for you.Enjoy programming.


我认为问题出在Page_Load事件中,并且每次刷新页面时您都在绑定网格.因此,将绑定登录名放入!isPostBack并检查
I think the problem is in the Page_Load event and You are binding the grid everytime whenevr the page is refreshed. So put the binding login in the !isPostBack and check


这篇关于在带有两个复选框的asp.net中冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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