ASP.NET CheckBox AutoPostBack没有触发

时间:2016-08-17 15:10:33

标签: c# asp.net .net custom-server-controls

我有一个复选框,我将其添加到服务器控件中。这个复选框没有得到通常的onclick ...__ doPostBack()调用。这导致它不会像我希望的那样引起回发。

private void CreateGrid()
    {
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter writer = new HtmlTextWriter(sWriter);

        GridItem.ID = "gridItem";

        GridHeader.ID = "gridHeader";
        GridHeader.Attributes["class"] += " no-select";

        GridCount.ID = "GridCount";

        GridDescription.ID = "GridDescription";

        if (cBoxID == null) tBox.Visible = false;
        else
        {
            tBox.ID = cBoxID;
            tBox.AutoPostBack = true;
            tBox.EnableViewState = true;
            ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(tBox);
            //tBox.CheckedChanged += new EventHandler(Force_Post_Back);
        }

        QuickFilter.Attributes["class"] = "quick-filter";
        QuickFilter.Attributes["title"] = "Quick Filter";

        IconMagnifier.Attributes["class"] = "icon-magnifier";

        GridResults.ID = "ltlGridResults";
        GridResults.ClientIDMode = ClientIDMode.Static;
        //GridResults.EnableViewState = false;

        QuickFilter.Controls.Add(IconMagnifier);

        GridHeader.Controls.AddAt(0, tBox);
        GridHeader.Controls.Add(QuickFilter);
        GridHeader.Controls.Add(GridCount);
        GridHeader.Controls.Add(GridDescription);

        GridItem.Controls.Add(GridHeader);
        GridItem.Controls.Add(GridResults);
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        CreateGrid();
    }

    protected override void Render(HtmlTextWriter writer)

    {
        GridItem.RenderControl(writer);
    }

Image to generated html

1 个答案:

答案 0 :(得分:0)

我在CreateGrid方法的底部添加了以下代码行:

this.Controls.Add(GridItem);

这允许我将GridItem控件添加到当前控件,从而允许回发。即使你渲染Checkbox也没有这个调用,它完全是愚蠢的。