【问题标题】:TemplateControl Checkbox Event in Custom GridView Control自定义 GridView 控件中的 TemplateControl 复选框事件
【发布时间】:2012-07-05 10:07:46
【问题描述】:

我有一个自定义的 GridView 控件,我从数据库中获取数据以填充控件。在页面上我还创建了一个 HeaderTemplate 复选框控件和一个 ItemTemplate 复选框控件:

<nm:ContactGridViewControl runat="server" ID="grdContacts">
        <Columns>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox runat="server" AutoPostBack="true" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </nm:ContactGridViewControl>

我在 OnInit 事件中按如下方式填充 GridView。我选择不在每次回发时重新填充,因为它会减慢应用程序的速度。

protected override void OnInit(EventArgs e)
    {
        this.RowDataBound += new GridViewRowEventHandler(ContactGridViewControl_RowDataBound);
        this.RowCreated += new GridViewRowEventHandler(ContactGridViewControl_RowCreated);

        if (!Page.IsPostBack)
        {
            List<EnquiryItem> contactList = new List<EnquiryItem>();
            DataTable list = new DataTable();

            if (SessionManager.LoginState != null)
            {
                contactList = SiteDataLayerHandler.GetContactList(SessionManager.LoginState.UserID);
            }

            if (contactList != null)
            {
                list.Columns.Add("LeadID");
                list.Columns.Add("Name");
                list.Columns.Add("Email Address");

                foreach (EnquiryItem item in contactList)
                {
                    DataRow row = list.NewRow();

                    row["LeadID"] = item.LeadID;
                    row["Name"] = string.Format("{0} {1}", item.FirstName.ToCapitalize(), item.LastName.ToCapitalize());
                    row["Email Address"] = item.EmailAddress;

                    list.Rows.Add(row);

                }

                this.DataSource = list;
                this.DataBind();
            }
        }

        base.OnInit(e);
    }

为了将与控件关联的所有代码保存在一个地方,我在“OnRowDataBound”上动态添加了一个“CheckedChanged”事件,这仅适用于 HeaderTemplate 中的复选框。原因是我可以将此复选框用作“选择/取消选择所有行”:

protected void ContactGridViewControl_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == -1) // Check if row is Header row
        {
            CheckBox chk = e.Row.GetAllControls().OfType<CheckBox>().FirstOrDefault();

            if (chk != null)
            {
                chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
            }
        }
    }

然后我将事件代码放在同一页面上,如下所示:

protected void chk_CheckedChanged(object sender, EventArgs e)
    {
        bool isChecked = ((CheckBox)sender).Checked;

        foreach (GridViewRow row in this.Rows)
        {
            CheckBox chkBox = row.Cells[0].Controls[0] as CheckBox;

            if (chkBox != null)
            {
                chkBox.Checked = isChecked;
            }
        }
    }

这就是问题的开始。我的活动永远不会受到打击!但是,该复选框会回发。

【问题讨论】:

    标签: c# asp.net events gridview custom-controls


    【解决方案1】:

    好的,答案是这样的。我需要在“OnRowCreated”事件而不是“OnRowDataBound”上分配 CheckedChanged 事件

    protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex == -1)
            {
                CheckBox chk = e.Row.GetAllControls().OfType<CheckBox>().FirstOrDefault();
    
                if (chk != null)
                {
                    chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
                }
            }
            base.OnRowCreated(e);
        }
    

    这样事件每次都会触发方法

    【讨论】:

    • GetAllControls 扩展方法从何而来?
    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    • 2014-11-03
    • 2012-06-07
    • 1970-01-01
    • 2011-04-25
    相关资源
    最近更新 更多