我在gridview中使用复选框控件,我想在checkboxchanged事件之前显示确认。

确认框询问问题,但不回发,什么也不做...

// CS

        TemplateField field = new TemplateField(){
            HeaderText = "AKTIF",
            ItemTemplate = new AddTemplateToGridView("AKTIF")
        };
        field.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
        field.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
        field.ItemStyle.Width = Unit.Pixel(40);
        newXGrid.Columns.Add(field);

// ITEMPLATE
        public class AddTemplateToGridView : ITemplate
        {
            String columnName;

            public AddTemplateToGridView(String colname)
            {
                columnName = colname;
            }

            void ITemplate.InstantiateIn(System.Web.UI.Control container)
            {
                if (columnName == "AKTIF")
                {
                    CheckBox chk = new CheckBox() { ID="chkAktif", AutoPostBack=true };
                    chk.Attributes.Add("onclick","return confirm('Are you sure ?')");
                    chk.CheckedChanged += new EventHandler(l_CheckedChanged);
                    container.Controls.Add(chk);
                }
            }


            void l_CheckedChanged(object sender, EventArgs e)
            {
               ...
            }



        }

//页面上的我的复选框
<input id="ctl00_MainContent_ctl11_ctl02_chkAktif" type="checkbox" name="ctl00$MainContent$ctl11$ctl02$chkAktif" onclick="return confirm('Are you sure ?');setTimeout('__doPostBack(\'ctl00$MainContent$ctl11$ctl02$chkAktif\',\'\')', 0)">

最佳答案

更改此:

chk.Attributes.Add("onclick","return confirm('Are you sure ?')");

对此:
chk.Attributes.Add("onclick","if (!confirm('Are you sure ?')) return false;");

将Checkbox设置为AutoPostBack="true"以触发点击后的回发。

关于c# - 如何在asp.net中更改复选框前使用确认?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9704068/

10-15 15:53