我已經創建列表視圖控件在自定義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
請幫幫我我無法得到什麼是確切的問題。
如果您確定問題併發布問題很容易重現的較短示例,可能會有所幫助。 – Stilgar 2014-10-16 15:08:24