在ASP.NET2.0中我使用Callback技术从服务器端取得数据,并用JS添加到DropDownList控件中
但提交时出现 invaild Postback 错误 ,因为DropDownList被JS改动过,出于安全性问题,肯定会错
系统提示要使用ClientScript.RegisterForEventValidation,但我不知如何使用,还请高手赐教,先谢了,全代码如下
aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Server Time</title>
<script language="javascript"> function GetServerTime()
{
var message = '';
var context = '';
<%=sCallBackFunctionInvocation%>
}
function ShowServerTime(timeMessage, context)
{
var oOption = document.createElement("OPTION");
oOption.text=timeMessage;
oOption.value=timeMessage;
document.all("DropDownList1").add(oOption); }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="得到服务器端时间" onclick="GetServerTime();" id="Button1" />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>
</form>
</body>
</html>
但提交时出现 invaild Postback 错误 ,因为DropDownList被JS改动过,出于安全性问题,肯定会错
系统提示要使用ClientScript.RegisterForEventValidation,但我不知如何使用,还请高手赐教,先谢了,全代码如下
aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" EnableEventValidation="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Server Time</title>
<script language="javascript"> function GetServerTime()
{
var message = '';
var context = '';
<%=sCallBackFunctionInvocation%>
}
function ShowServerTime(timeMessage, context)
{
var oOption = document.createElement("OPTION");
oOption.text=timeMessage;
oOption.value=timeMessage;
document.all("DropDownList1").add(oOption); }
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="得到服务器端时间" onclick="GetServerTime();" id="Button1" />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" /></div>
</form>
</body>
</html>
解决方案 »
- <li>标签的使用
- Gridview分页后SelectedIndexChanging怎么写
- repeater 修改,有谁知道怎么做?
- 我在网页中放置了多个文本框,想要获取其ID的值,请问该如何操作!谢谢
- 怎样在表格table中动态加入内容?(在线=)
- 求一个asp.net新闻发布系统原代码
- 想做一个网站,需要些什么知识
- 大家帮帮忙吧,datagrid中的e.Item.Cells[9].Controls[0]总是不正确!
- 有人知道電子報是怎樣發送的嗎?請教......
- 请问客户端脚本可以访问服务器端的数据库么?若能应该怎么操作?
- 指定的参数已超出有效值的范围。参数名: index
- 如何在网页中不用事件刷新dataGrid,用timer行吗?
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page, ICallbackEventHandler
{
public string sCallBackFunctionInvocation;
public string sTime; protected void Page_Load(object sender, EventArgs e)
{
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");
} #region ICallbackEventHandler Members public string GetCallbackResult()
{
return sTime;
} void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
sTime = DateTime.Now.ToString();
} #endregion
protected void Button2_Click(object sender, EventArgs e)
{
/*
点此按钮时就出错,无法提交,出现如下错误
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.
*/
if (DropDownList1.Items.Count > 0)
{
Response.Write(DropDownList1.SelectedItem.ToString());
}
}
}