C# 动态创建的ListView控件

C# 动态创建的ListView控件,c#,asp.net,C#,Asp.net,我已经在自定义Web控件中创建了列表视图控件 以下是我的所有模板: private class LayoutHeaderTemplate : ITemplate { public void InstantiateIn(Control container) { var div = new HtmlGenericControl("div") { ID = "itemPlaceholder" }; var

我已经在自定义Web控件中创建了列表视图控件

以下是我的所有模板:

    private class LayoutHeaderTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            var div = new HtmlGenericControl("div") { ID = "itemPlaceholder" };

            var headerTable = new HtmlGenericControl("table");

            headerTable.Attributes.Add("border", "1px");
            var headerTr = new HtmlGenericControl("tr");
            var headerTd1 = new HtmlGenericControl("td");
            headerTd1.Attributes.Add("width", "200");
            headerTd1.Controls.Add(new CheckBox() { Text = "None", ID = "CheckNoneId", CssClass="CheckNone" });
            var headerTd2 = new HtmlGenericControl("td");
            headerTd2.Attributes.Add("width", "100");
            var headerTd3 = new HtmlGenericControl("td");
            headerTd3.Attributes.Add("width", "100");
            headerTr.Controls.Add(headerTd1);
            headerTr.Controls.Add(headerTd2);
            headerTr.Controls.Add(headerTd3);
            headerTable.Controls.Add(headerTr);
            container.Controls.Add(headerTable);

            container.Controls.Add(div);

            var footerTable = new HtmlGenericControl("table");
            footerTable.Attributes.Add("class", "Footer");
            footerTable.Attributes.Add("border", "1px");
            var footerTr = new HtmlGenericControl("tr");
            var footerTd1 = new HtmlGenericControl("td");
            footerTd1.Attributes.Add("width", "200");
            CheckBox CheckOther = new CheckBox() { Text = "Other", ID = "CheckOtherId", CssClass = "CheckOther" };
            CheckOther.Attributes.Add("onChange", "return otherCheckBoxSelect(this);");
            footerTd1.Controls.Add(CheckOther);                
            var footerTd2 = new HtmlGenericControl("td");
            footerTd2.Attributes.Add("colspan", "2");
            footerTd2.Attributes.Add("width", "205");
            footerTd2.Controls.Add(new CheckBox() { Text = "Show Additional Info", ID = "CheckAdditionalInfoId",CssClass="AdditionalInfo" });
            footerTr.Controls.Add(footerTd1);
            footerTr.Controls.Add(footerTd2);                
            footerTable.Controls.Add(footerTr);
            container.Controls.Add(footerTable);

        }

    }


    /// <summary>
    /// Created Item Template for List View.
    /// </summary>
    private class ItemTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            var tbl1 = new HtmlGenericControl("table");
            tbl1.Attributes.Add("border", "1px");
            var tr1 = new HtmlGenericControl("tr");
            var td1 = new HtmlGenericControl("td");
            td1.Attributes.Add("width", "200");
            td1.DataBinding += DataBinding;
            var td2 = new HtmlGenericControl("td");
            td2.Attributes.Add("width", "100");
            td2.Controls.Add(new Button() { Text = "Edit", ID = "EditButtonId", CausesValidation = false, CommandName = "Edit" });
            var td3 = new HtmlGenericControl("td");
            td3.Attributes.Add("width", "100");
            td3.Controls.Add(new Button() { Text = "Delete", CausesValidation = false, CommandName = "Delete", OnClientClick = "return deleteConfirm();" });
            tr1.Controls.Add(td1);
            tr1.Controls.Add(td2);
            tr1.Controls.Add(td3);
            tbl1.Controls.Add(tr1);
            container.Controls.Add(tbl1);
        }

        public void DataBinding(object sender, EventArgs e)
        {
            var container = (HtmlGenericControl)sender;
            var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem;
            container.Controls.Add(new Label() { Text = dataItem.ToString(), ID = "ItemLabelId" });

        }
    }

    /// <summary>
    /// Created EditItemTemplate for List View.
    /// </summary>
    private class EditItemTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            var tbl1 = new HtmlGenericControl("table");
            tbl1.Attributes.Add("border", "1px");
            var tr1 = new HtmlGenericControl("tr");
            var td1 = new HtmlGenericControl("td");
            td1.Attributes.Add("width", "200");
            td1.DataBinding += DataBinding;
            var td2 = new HtmlGenericControl("td");
            td2.Attributes.Add("width", "100");
            td2.Controls.Add(new Button() { Text = "Update", ID = "UpdateButtonId", CausesValidation = false, CommandName = "Update" });
            var td3 = new HtmlGenericControl("td");
            td3.Attributes.Add("width", "100");
            td3.Controls.Add(new Button() { Text = "Cancel", ID = "CancelButtonId", CausesValidation = false, CommandName = "Cancel" });
            tr1.Controls.Add(td1);
            tr1.Controls.Add(td2);
            tr1.Controls.Add(td3);
            tbl1.Controls.Add(tr1);
            container.Controls.Add(tbl1);
        }

        public void DataBinding(object sender, EventArgs e)
        {
            var container = (HtmlGenericControl)sender;
            var dataItem = ((ListViewDataItem)container.NamingContainer).DataItem;
            container.Controls.Add(new TextBox() { Text = dataItem.ToString(), ID = "ItemTextBoxID" });

        }
    }`
当我单击ListView中的“更新、删除”按钮时,它给了我一个错误:

Server Error in '/' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +144
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +122
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446

请帮助我,我无法找到确切的问题所在。

关闭事件验证以使其正常工作。但这并不是建议,因为事件验证是为了防止黑客攻击。它检查回发不是由不可见控件或不存在的控件完成的

您应该在页面级别关闭事件验证,然后为页面上的所有控件调用RegisterForEventValidation,以验证它们的事件和回发值

Page.ClientScript.RegisterForEventValidationbutton1.UniqueID

编辑:


您还可以动态添加控件并将其注册以进行事件验证,而无需关闭enableEventValidation

如果你能解决这个问题,并在问题容易重现的地方张贴简短的例子,可能会有所帮助。@Moo Juice uhhh。。。不,不,这个错误消息写得不好。这里的解决方案基本相同。您还可以看到,它是通过堆栈跟踪中的call System.Web.UI.ClientScriptManager.ValidateEvent启用的,而不是我使用的按钮Link Button及其工作状态。
Server Error in '/' Application.

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[ArgumentException: Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.]
   System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +144
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +122
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18446