[c#]不开火的 ASP.NET 复选框 AutoPostBack

标签: Asp.net .Net C#
发布时间: 2017/2/25 3:26:54
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有一个我要加入到服务器控件的复选框。此复选框没有得到通常的 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);
    }

对生成的 html 图像

解决方法 1:

我到我的 CreateGrid 方法的底部添加下面的代码行︰

this.Controls.Add(GridItem);

这样我就可以将 GridItem 控件添加到当前控件,从而允许为回发。如果不调用此即使你呈现该复选框,它是完全哑。

官方微信
官方QQ群
31647020