我在自定义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" });
}
}`
以下是我的所有列表视图事件:
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
请帮助我,我无法得到确切的问题。
答案 0 :(得分:-1)
关闭事件验证以使其生效。但这不建议事件验证是为了防止黑客入侵。它检查回发是不是由不可见的控件或不存在的控件完成的。
您应该在页面级别关闭事件验证,然后在页面上调用所有控件的RegisterForEventValidation,以验证其事件和回发值。
<pages enableEventValidation="false"/>
Page.ClientScript.RegisterForEventValidation(button1.UniqueID);
修改强>
您还可以动态添加控件并将其注册以进行事件验证,而不必关闭enableEventValidation。