调用RowCreated gridview处理程序中的代码隐藏方法(函数)

本文关键字:方法 隐藏 函数 代码 gridview RowCreated 处理 程序 调用 | 更新日期: 2023-09-27 18:06:09

我想在Gridview中调用Code Behind方法。

这是Code Behind方法,循环遍历所有web控件的形式:

private void SetupJSdataChange(Control parentControl)
{
    foreach (Control control in parentControl.Controls)
    {
        //Response.Write(control.ID + "<br>");
        if (control is WebControl)
        {
            WebControl webControl = control as WebControl;
            webControl.Attributes.Add("onchange", "InputChanged(this)");
        }
    }
}

我想让它循环通过每个webcontrol是在一个RowCreated gridview的处理程序代替。这是我的尝试:

protected void GVTrabajadores_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        foreach (Control control in parentControl.Controls)
        {
            //Response.Write(control.ID + "<br>");
            if (control is WebControl)
            {
                WebControl webControl = control as WebControl;
                webControl.Attributes.Add("onchange", "InputChanged(this)");
            }
        }
    }
}

但是我得到一个错误,说"名称'parentControl'不存在于当前上下文中",我不知道如何处理它。

这是Gridview:

        <asp:GridView ID="GVTrabajadores" runat="server"
            CssClass="table table-hover table-striped"
            ShowHeaderWhenEmpty = "true"
            GridLines="None"
            AutoGenerateColumns="False"
            ShowFooter="true"
            OnRowCreated="GVTrabajadores_RowCreated"
            OnRowCommand="GVTrabajadores_RowCommand">
            <Columns>
                <asp:TemplateField HeaderText="Nombre">
                    <ItemTemplate>
                        <asp:TextBox ID="TxNombre" runat="server" Text='<%#Eval("nombre") %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:TextBox ID="TxNombref" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Cerrada">
                    <ItemTemplate>
                        <asp:CheckBox ID="CBCerrada" runat="server" Checked='<%# (Eval("cerrada").ToString() == "1" ? true : false) %>' />
                    </ItemTemplate>
                    <FooterTemplate>
                        <asp:CheckBox ID="CBCerradaf" runat="server" />
                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

调用RowCreated gridview处理程序中的代码隐藏方法(函数)

这应该适合您。注意,我使用了通用的事件参数——如果你需要它们,你需要修改它们。

private void GVTrabajadores_RowCreated(object sender, GridViewRowEventArgs e)
    {
        GridView gV = sender as GridView;
        for(int i = 0; i < gV.Columns.Count; i++)
        {
          Control c = e.Row.Cells[i].Controls[0];
            string controlType = c.GetType().ToString().Replace("System.Web.UI.WebControls.", "");
            switch (controlType)
            {
                case "TextBox":
                    TextBox t = c as TextBox;
                    t.TextChanged += GVTrabajadores_TextBox_TextChanged;
                    t.AutoPostBack = true;
                    break;
                case "CheckBox":
                    CheckBox cB = c as CheckBox;
                    cB.CheckedChanged += GVTrabajadores_CheckBox_Changed;
                    cB.AutoPostBack = true;
                    break;
            }
        }
    }
    private void GVTrabajadores_CheckBox_Changed(object sender, EventArgs e)
    {
        //process change
    }
    private void GVTrabajadores_TextBox_TextChanged(object sender, EventArgs e)
    {
        //process changed text
    }