2014-10-16 150 views
1

我已经创建列表视图控件在自定义Web控件动态创建ListView控件

以下是我所有的模板:

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" }); 

     } 
    }` 

以下是我的列表查看活动:

protected void itemValueListView_ItemEditing(object sender, ListViewEditEventArgs e) 

     { 
      itemValueListView.EditIndex = e.NewEditIndex; 

      this.itemValueListView.DataSource = this.ItemValueList; 
      this.itemValueListView.DataBind(); 
     } 

     protected void itemValueListView_ItemUpdating(object sender, ListViewUpdateEventArgs e) 
     { 
      ListViewItem item = itemValueListView.Items[e.ItemIndex]; 
      TextBox itemTextBox = (TextBox)item.FindControl("ItemTextBoxID"); 

      this.itemValueList.RemoveAt(e.ItemIndex); 
      this.itemValueList.Insert(e.ItemIndex, itemTextBox.Text); 

      itemValueListView.EditIndex = -1; 
      this.itemValueListView.DataSource = this.ItemValueList; 
      this.itemValueListView.DataBind(); 
     } 

     protected void itemValueListView_ItemCanceling(object sender, ListViewCancelEventArgs e) 
     { 
      itemValueListView.EditIndex = -1; 

      this.itemValueListView.DataSource = this.ItemValueList; 
      this.itemValueListView.DataBind(); 
     } 

     protected void itemValueListView_ItemDeleting(object sender, ListViewDeleteEventArgs e) 
     { 
      ListViewItem item = itemValueListView.Items[e.ItemIndex]; 

      Label itemLabel = (Label)item.FindControl("ItemLabelId"); 
      this.itemValueList.Remove(itemLabel.Text); 

      this.itemValueListView.DataSource = this.ItemValueList; 
      this.itemValueListView.DataBind(); 
     }` 

时我正在点击更新,在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 

请帮帮我我无法得到什么是确切的问题。

+1

如果您确定问题并发布问题很容易重现的较短示例,可能会有所帮助。 – Stilgar 2014-10-16 15:08:24

回答

-1

关闭事件验证为它工作。但这不是建议事件验证是为了防止黑客入侵。它会检查回发是否由不可见控件或不存在的控件完成。

您应该关闭页面级别的事件验证,然后在页面上调用RegisterForEventValidation获取所有控件,以使其事件和回发值得到验证。

<pages enableEventValidation="false"/>

Page.ClientScript.RegisterForEventValidation(button1.UniqueID);

编辑:

您还可以动态地添加控件并注册了事件验证,而不必关闭enableEventValidation。

+0

@ Moo-Juice uhhh ...不,它不会写错误信息。相同的解决方案几乎在这里http://stackoverflow.com/questions/228969/invalid-postback-or-callback-argument-event-validation-is-enabled-using-page。你也可以看到它启用与调用System.Web.UI.ClientScriptManager.ValidateEvent堆栈跟踪 – Hillboy 2014-10-16 15:20:32

+1

而不是按钮我已经使用链接按钮,现在它的工作。 – 2014-10-17 03:17:26