无法修改Controls集合,因为控件包含代码块(即) [英] The Controls collection cannot be modified because the control contains code blocks (i.e. )

查看:69
本文介绍了无法修改Controls集合,因为控件包含代码块(即)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的appcode中使用printclass



来自printimgbtn点击事件后面的代码我正在调用静态类方法,其参数为contorl和string





这是类代码:

  public   static   void  PrintWebControl(Control ctrl, string 脚本)
{
// if(System.Web.HttpContext。当前。(字符串)会话[isgrid] ==网格)
// {
// GridView grid =(GridView)ctrl;
// grid.AllowPaging = false;
// grid.DataBind();
// }


StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
if (ctrl WebControl)
{
单位w = new 单位( 100 ,UnitType.Percentage); ((WebControl)ctrl).Width = w;
}
Page pg = new Page();
pg.EnableEventValidation = false ;
if (Script!= string .Empty)
{
pg.ClientScript.RegisterStartupScript(pg.GetType(), PrintJavaScript,Script);
}
HtmlForm frm = new HtmlForm();
pg.Controls.Add(frm);
frm.Attributes.Add( runat server);
frm.Controls.Add(ctrl);
pg.DesignerInitialize();
pg.RenderControl(htmlWrite);
string strHTML = stringWrite.ToString();
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(strHTML);
HttpContext.Current.Response.Write( < script> window.print();< /脚本>中);
HttpContext.Current.Response.End();
}

代码背后代码:

受保护 void btnPrint_Click( object sender,ImageClickEventArgs e)
{

PrintHelper.PrintWebControl(PnlExportButtons0, .Empty);
}

显示错误:

无法修改Controls集合,因为控件包含代码块(即<%... %>

解决方案

查看您的HTML设计



将代码块替换为<%#而不是<%=



首选

aspnet-controls-collection-can-be.html [ ^ ]

-controls-collection-can-be-modified-because-the-control-contains-code-bl [ ^ ]


你解决了我更换

< pre lang =   cs> document.getElementById(ContentPlaceHolder1_txtFromDate)。 value  =( new  Date())。format('  dd / MMM / yyyy'); 
document.getElementById(ContentPlaceHolder1_txtToDate)。 value =( new Date())。format( ' dd / MMM / yyyy');


document.getElementById(< =%txtfromdate = %> )。 value =( new Date())。format(' dd / MMM / yyyy);
document.getElementById(< =%txtTodate =%>)。value =(new Date())。format('
dd / MMM / yyyy);


i am using printclass in my appcode

from code behind printimgbtn click event i am calling static class method which had arguments as contorl and string


this is class code :

public static void PrintWebControl(Control ctrl, string Script)
    {
        //if (System.Web.HttpContext.Current.(string)Session["isgrid"] == "grid")
        //{
        //    GridView grid = (GridView)ctrl;
        //    grid.AllowPaging = false;
        //    grid.DataBind();
        //}


        StringWriter stringWrite = new StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
        if (ctrl is WebControl)
        {
            Unit w = new Unit(100, UnitType.Percentage); ((WebControl)ctrl).Width = w;
        }
        Page pg = new Page();
        pg.EnableEventValidation = false;
        if (Script != string.Empty)
        {
            pg.ClientScript.RegisterStartupScript(pg.GetType(), "PrintJavaScript", Script);
        }
        HtmlForm frm = new HtmlForm();
        pg.Controls.Add(frm);
        frm.Attributes.Add("runat", "server");
        frm.Controls.Add(ctrl);
        pg.DesignerInitialize();
        pg.RenderControl(htmlWrite);
        string strHTML = stringWrite.ToString();
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Write(strHTML);
        HttpContext.Current.Response.Write("<script>window.print();</script>");
        HttpContext.Current.Response.End();
    }

code behind code : 

protected void btnPrint_Click(object sender, ImageClickEventArgs e)
{
    
    PrintHelper.PrintWebControl(PnlExportButtons0,string.Empty);
}

but it is showing the error  :

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

解决方案

See Your HTML design

Replace the code block with <%# instead of <%=

Prefer
aspnet-controls-collection-cannot-be.html[^]
the-controls-collection-cannot-be-modified-because-the-control-contains-code-bl[^]


ya it solved i replace

<pre lang="cs">document.getElementById(ContentPlaceHolder1_txtFromDate).value = (new Date()).format('dd/MMM/yyyy');
        document.getElementById(ContentPlaceHolder1_txtToDate).value = (new Date()).format('dd/MMM/yyyy');

by
document.getElementById(<=%txtfromdate=%>).value = (new Date()).format('dd/MMM/yyyy);
        document.getElementById(<=%txtTodate=%>).value = (new Date()).format('dd/MMM/yyyy);


这篇关于无法修改Controls集合,因为控件包含代码块(即)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆