不清楚怎样开才能好使,下面是提示错误问题:
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentException: 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
下面是源码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class sale_gedit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] role = Roles.GetRolesForUser();
if (role[0].ToString().Trim() == "经理")
{
GridView1.DataSourceID = "SqlDataSource1";
}
else
{
Label4.Text = role[0].ToString();
GridView1.DataSourceID = "SqlDataSource3";
DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DataBind();
}
}
回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentException: 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
下面是源码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class sale_gedit : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string[] role = Roles.GetRolesForUser();
if (role[0].ToString().Trim() == "经理")
{
GridView1.DataSourceID = "SqlDataSource1";
}
else
{
Label4.Text = role[0].ToString();
GridView1.DataSourceID = "SqlDataSource3";
DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
DataBind();
}
}
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("buydays", "{0:D}") %>'></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("buydays") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("buydays", "{0:D}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="cars" HeaderText="车型" SortExpression="cars" />
<asp:BoundField DataField="IDCart" HeaderText="身份证号" SortExpression="IDCart" />
<asp:TemplateField HeaderText="备注" SortExpression="CutRes">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Height="110px" Text='<%# Bind("CutRes") %>'
TextMode="MultiLine" Width="232px"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Height="103px" Text='<%# Bind("CutRes") %>'
TextMode="MultiLine" Width="242px"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("CutRes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button" ShowDeleteButton="True" ShowEditButton="True"
ShowInsertButton="True" />
</Fields>
<FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:DetailsView>
<asp:Label ID="Label4" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [id], [name], [sex], [phone], [cars] FROM [CutOrder]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [id], [name], [sex], [phone], [cars] FROM [CutOrder] WHERE ([cars] = @cars)">
<SelectParameters>
<asp:ControlParameter ControlID="Label4" Name="cars" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [CutOrder] WHERE [id] = @id" InsertCommand="INSERT INTO [CutOrder] ([name], [sex], [phone], [homeaddress], [workaddress], [birthday], [buydays], [cars], [IDCart], [CutRes]) VALUES (@name, @sex, @phone, @homeaddress, @workaddress, @birthday, @buydays, @cars, @IDCart, @CutRes)"
SelectCommand="SELECT * FROM [CutOrder] WHERE ([id] = @id)" UpdateCommand="UPDATE [CutOrder] SET [name] = @name, [sex] = @sex, [phone] = @phone, [homeaddress] = @homeaddress, [workaddress] = @workaddress, [birthday] = @birthday, [buydays] = @buydays, [cars] = @cars, [IDCart] = @IDCart, [CutRes] = @CutRes WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="phone" Type="String" />
<asp:Parameter Name="homeaddress" Type="String" />
<asp:Parameter Name="workaddress" Type="String" />
<asp:Parameter Name="birthday" Type="DateTime" />
<asp:Parameter Name="buydays" Type="DateTime" />
<asp:Parameter Name="cars" Type="String" />
<asp:Parameter Name="IDCart" Type="String" />
<asp:Parameter Name="CutRes" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="name" Type="String" />
<asp:Parameter Name="sex" Type="String" />
<asp:Parameter Name="phone" Type="String" />
<asp:Parameter Name="homeaddress" Type="String" />
<asp:Parameter Name="workaddress" Type="String" />
<asp:Parameter Name="birthday" Type="DateTime" />
<asp:Parameter Name="buydays" Type="DateTime" />
<asp:Parameter Name="cars" Type="String" />
<asp:Parameter Name="IDCart" Type="String" />
<asp:Parameter Name="CutRes" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
</asp:Content>
符用于限定类型以及类型成员的申明,c#中有13种修饰符,按功能可分为三部分:存取修饰符,类修饰符和成员修饰符.存取修饰符:public:存取不受限制.private:只有包含该成员的类可以存取.internal:只有当前工程可以存取.protected:只有包含该成员的类以及继承的类可以存取.类修饰符:abstract:可以被指示一个类只能作为其它类的基类.sealed:指示一个类不能被继承.成员修饰符:abstract:指示该方法或属性没有实现.const:指定域或局部变量的值不能被改动.event:声明一个事件.extern:指示方法在外部实现.override:对由基类继承成员的新实现.readonly:指示一个域只能在声明时以及相同类的内部被赋值.static:指示一个成员属于类型本身,而不是属于特定的对象.virtual:指示一个方法或存取器的实现可以在继承类中被覆盖.
作者:金桥网络 文档来源:互联网 点击数:1026 更新时间:2006-6-4
在程序运行中,产生事件的主体有很多,其中尤其以键盘和鼠标为最多。本文就来探讨一下在C#中和这二个主体相关的事件的处理过程。 一.本文介绍的程序设计和运行的软件环境: (1).微软公司视窗2000服务器版 (2)..Net FrameWork SDK Beta 2 二.C#中处理鼠标相关的事件: 鼠标相关的事件大致有六种,分别是 : "MouseHover"、"MouseLeave"、"MouseEnter"、"MouseMove"、"MouseDown"和"MouseUp"。 (1).如何在C#程序中定义这些事件: 在C#中是通过不同的Delegate来描述上述事件,其中描述"MouseHover"、"MouseLeave"、"MouseEnter"事件的Delegate是"EventHandler",而描述后面的三个事件的Delegate是"MouseEventHandler"来描述。这二个Delegate分别被封装在不同的命名空间,其中"EventHandler"被封装在"System"命名空间;"MouseEventHandler"被封装在"Syetem.Windows.Froms"命名空间中的。在为"MouseHover"、"MouseLeave"、"MouseEnter"事件通过数据的类是"EventArgs",他也被封装在"System"命名空间中;而为后面的三个事件提供数据的类是"MouseEventArgs",他却被封装在"Syetem.Windows.Froms"命名空间。以上这些就决定了在C#中定义这些事件和响应这些事件有着不同的处理办法。下面就来介绍这些不同点。
对于上述的前三个事件,是用以下语法来定义的: "组件名称"."事件名称"+= new System.EventHandler("事件名称"); 下面是程序中具体实现代码: button1.MouseLeave += new Syetem.EvenHandler(button1_MLeave); 在完成了事件的定义以后,就要在程序中加入响应此事件的代码,否则程序编译的时候会报错。下面是响应上面事件的基本结构。private void button1_MLeave ( object sender , System.EventArgs e )
{
此处加入响应此事件的代码
} 定义"MouseMove"、"MouseDown"和"MouseUp"事件的语法和前面介绍的三个事件大致相同,具体如下: "组件名称"."事件名称"+= new System.Windows.Forms. MouseEventHandler("事件名称"); 下面是程序中具体实现代码:button1.MouseMove += new System.Windows.Forms.MouseEventHandler(button1_MMove); 下面是响应上面事件的基本结构:private void button1_MMove ( object sender , System.Windows.Forms. MouseEventArgs e )
{
此处加入响应此事件的代码
} 注释:在上述程序中的"button1"是定义的一个按钮组件。
2).鼠标相关事件中的典型问题处理办法: 在掌握了C#中定义和鼠标相关的事件,我们就来探讨一下和鼠标相关事件的典型问题。其一是读取鼠标的当前位置;其二是判定到底是那个鼠标按键按动。 判定鼠标的位置可以通过事件"MouseMove"来处理,在"MouseEventArgs"类中提供了二个属性"X"和"Y",来判定当前鼠标纵坐标和横坐标。而判定鼠标按键的按动情况,可以通过事件"MouseDown"来处理,并且在"MouseEventArgs"类中也提供了一个属性"Button"来判定鼠标按键情况。根据这些知识,可以得到用C#编写的读取鼠标当前位置和判定鼠标按键情况的程序代码。下面就是此代码(mouse.cs)和此代码编译后运行界面:
图01:用C#读取鼠标位置和鼠标按键的程序运行界面
mouse.cs的源程序代码如下:using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
public class Form1 : Form
{
private System.ComponentModel.Container components = null ;
public Form1 ( )
{
file://初始化窗体中的各个组件
InitializeComponent ( ) ;
}
file://清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if (components != null)
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
this.Name = "Form1" ;
this.Text = "C#处理鼠标按动事件!" ;
file://为鼠标按动定义一个事件处理过程"Form1_MouseDown"
this.MouseDown += new MouseEventHandler ( Form1_MouseDown ) ;
file://为鼠标移动定义一个事件处理过程"Form1_MouseMove"
this.MouseMove += new MouseEventHandler ( Form1_OnMouseMove ) ;}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
private void Form1_OnMouseMove ( object sender , MouseEventArgs e )
{
this.Text = "当前鼠标的位置为:( " + e.X + " , " + e.Y + ")" ;
}private void Form1_MouseDown ( object sender , MouseEventArgs e )
{
file://响应鼠标的不同按键
if ( e.Button == MouseButtons.Left )
{
MessageBox.Show ( "按动鼠标左键!" ) ;
}
if ( e.Button == MouseButtons.Middle )
{
MessageBox.Show ( "按动鼠标中键!") ;
}
if ( e.Button == MouseButtons.Right )
{
MessageBox.Show ( "按动鼠标右键!") ;
}
}
}
三.C#中处理和键盘相关的事件: 在C#中和键盘相关的事件相对比较少,大致就三种:"KeyDown"、"KeyUp"和"KeyPress"。 (1).如何在C#程序中定义这些事件:
C#中描述"KeyDown"、"KeyUp"的事件的Delegate是"KeyEventHandler"。而描述"KeyPress"所用的Delegate是"KeyPressEventHandler"。这二个Delegate都被封装在命名空间"Syetem.Windows.Froms"中。为"KeyDown"、"KeyUp"的事件提供数据的类是"KeyEventArgs"。而为"KeyPress"事件提供数据的类是"KeyPressEventArgs"。同样这二者也被封装在命名空间"Syetem.Windows.Froms"中。 在C#程序定义"KeyDown"、"KeyUp"事件的语法如下: "组件名称"."事件名称"+= new Syetem.Windows.Froms. KeyEventHandler("事件名称"); 下面是程序中具体实现代码:
button1. KeyUp += new Syetem.Windows.Froms. KeyEventHandler(button1_KUp); 下面是响应上面事件的基本结构。
private void button1_KUp ( object sender , Syetem.Windows.Froms. KeyEventArgs e )
{
此处加入响应此事件的代码
} 在C#程序定义"KeyPress"事件的语法如下: "组件名称"."事件名称"+= new Syetem.Windows.Froms. KeyPressEventHandler("事件名称"); 下面是程序中具体实现代码:
button1. KeyPress += new Syetem.Windows.Froms. KeyPressEventArgs(button1_KPress); 在完成了事件的定义以后,就要在程序中加入响应此事件的代码,否则程序编译的时候会报错。下面是响应上面事件的基本结构。
private void button1_KPress ( object sender , Syetem.Windows.Froms. KeyPressEventArgs e )
{
此处加入响应此事件的代码
} 注释:程序中出现的"button1"是定义的一个按钮组件。(2).和键盘相关事件中的典型问题处理办法: 和键盘相关的典型问题无非就是判定到底是哪个按键被按动。通过上面的三个事件都可以完成。并且在"KeyEventArgs"类中通过了一个属性"KeyCode",可以用他来读取当前按键。所以就在"KeyUp"或者"KeyDown"事件中处理这个问题。根据上面这些知识,可以得到用C#编写读取读取按键的程序代码,下面就是此代码(key.cs)和此代码运行后的界面:
图02:用C#读取键盘按键的程序运行界面 key.cs的代码如下:
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
public class Form1 : Form
{
private System.ComponentModel.Container components = null ;public Form1 ( )
{
file://初始化窗体中的各个组件
InitializeComponent ( ) ;
}
protected override void Dispose ( bool disposing )
{
file://清除程序中使用过的资源
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
private void InitializeComponent ( )
{
this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 292 , 273 ) ;
this.Name = "Form1" ;
this.Text = "C#处理键盘事件!" ;
file://为按键的按动定义一个事件处理过程"Form1_KeyUp"
this.KeyUp += new KeyEventHandler ( this.Form1_KeyUp ) ;}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
file://显示你所按动的按键名称
private void Form1_KeyUp ( object sender , KeyEventArgs e )
{
MessageBox.Show ( e.KeyCode.ToString ( ) , "您所按动的健为:" ) ;}
}
{
if(!this.ispostback)
{
string[] role = Roles.GetRolesForUser();
if (role[0].ToString().Trim() == "经理")
{
GridView1.DataSourceID = "SqlDataSource1";
}
else
{
Label4.Text = role[0].ToString();
GridView1.DataSourceID = "SqlDataSource3";
DataBind();
}
}
}
//传送参数
response.write("<script>window.open(’*.aspx?id="+this.DropDownList1.SelectIndex+"&id1="+ +"’)</script>")
//接收参数
string a = Request.QueryString("id");
string b = Request.QueryString("id1");2、为按钮添加对话框
Button1.Attributes.Add("onclick","return confirm(’确认?’)");
button.attributes.add("onclick","if(confirm(’are you sure ?’)){return true;}else{return false;} ")3、删除表格选定记录
int intEmpID = (int)MyDataGrid.DataKeys[e.Item.ItemIndex];
string deleteCmd = "DELETE from Employee where emp_id = " + intEmpID.ToString()4、删除表格记录警告
private void DataGrid_ItemCreated(Object sender,DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
case ListItemType.Item :
case ListItemType.AlternatingItem :
case ListItemType.EditItem:
TableCell myTableCell;
myTableCell = e.Item.Cells[14];
LinkButton myDeleteButton ;
myDeleteButton = (LinkButton)myTableCell.Controls[ 0];
myDeleteButton.Attributes.Add("onclick","return confirm(’您是否确定要删除这条信息’); ");
break;
default;
break;
}
}5、点击表格行链接另一页
private void grdCustomer_ItemDataBound( object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//点击表格打开
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
e.Item.Attributes.Add("onclick","window.open(’Default.aspx?id=" + e.Item.Cells[0].Text + "’);");
}
//双击表格连接到另一页
//在itemDataBind事件中
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string OrderItemID =e.item.cells[1].Text;
e.item.Attributes.Add("ondblclick", "location.href=’../ShippedGrid.aspx?id= " + OrderItemID + "’");
}
//双击表格打开新一页
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string OrderItemID =e.item.cells[1].Text;
e.item.Attributes.Add("ondblclick", "open(’../ShippedGrid.aspx?id=" + OrderItemID + "’)");
}
★特别注意:【?id=】 处不能为 【?id =】6、表格超链接列传递参数
<asp:HyperLinkColumn Target="_blank" headertext="ID号" DataTextField="id" NavigateUrl="aaa.aspx?id=’
<%# DataBinder.Eval(Container.DataItem, "数据字段1")%>’ & name=’<%# DataBinder.Eval(Container.DataItem, "数据字段2")%>’ />7、表格点击改变颜色
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onclick","this.style.backgroundColor=’#99cc00’;
this.style.color=’buttontext’;this.style.cursor=’default’;");
}
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","this.style.backgroundColor=’#99cc00’;
this.style.color=’buttontext’;this.style.cursor=’default’;");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=’’;this.style.color=’’; ");
}8、关于日期格式
DataFormatString="{0:yyyy-MM-dd}"
e.items.cell["你的列"].text=DateTime.Parse(e.items.cell["你的列"].text.ToString("yyyy-MM-dd"))
9、获取错误信息并到指定页面
//不要使用Response.Redirect,而应该使用Server.Transfer
// in global.asax
protected void Application_Error(Object sender, EventArgs e)
{
if (Server.GetLastError() is HttpUnhandledException)
Server.Transfer("MyErrorPage.aspx");
//其余的非HttpUnhandledException异常交给ASP.NET自己处理就okay了
}
//Redirect会导致post-back的产生从而丢失了错误信息,所以页面导向应该直接在服务器端执行,这样就可以在错误处理页面得到出错信息并进行相应的处理 10、清空Cookie
Cookie.Expires=[DateTime];
Response.Cookies("UserName").Expires = 0
可以在页面中,将它设置为false或者在WEBCONFIG中将它设置为false.