2014-10-16 151 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