我正在编写ASP.NET应用程序,我应该检查Request["__EVENTTARGET"]
中Page_Load
的服务器端值。我需要在程序上从客户端设置它的值。
我试着通过这个来设置它:
document.getElementById('__EVENTTARGET').value = "my_value";
但它不起作用。你能帮我吗,为什么?谢谢!
UPD:
function ShowUploadDialog(obj) {
document.getElementById('<%= uplReportLogo.ClientID %>').click();
document.getElementById('<%= hdnInvokeFileUpload.ClientID %>').value = $('input[type=file]').val();
__doPostBack(obj.id, 'Click');
}
服务器控件:
<asp:FileUpload runat="server" ID="uplReportLogo" CssClass="file upload" />
<asp:Button runat="server" ID="btnReportImage" Text="Load Image..." CssClass="button action load"
OnClientClick="javascript: return ShowUploadDialog(event, this);" OnClick="btnReportImage_Click" />
UPD2:
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
我在行theForm.submit();
上有“拒绝访问”例外原因?
答案 0 :(得分:2)
ASP.NET使用该变量来存储任何回发的sender
,因此我认为您的值每次都会被覆盖。
您可以尝试调用
__doPostBack('my_value','any_other_argument')
由JS,然后在Page_Load
事件中检查这样的值。
要使其正常工作,请务必删除ASPX文件第一行中的EventValidation
属性。
这是你在代码隐藏方面处理它的方法:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string _evt = this.Request["__EVENTTARGET"]; // 1st parameter
string _eva = this.Request["__EVENTARGUMENT"]; // 2nd parameter
switch (_evt)
{
case "my_value":
//do anything here
break;
default:
break;
}
}
}
编辑:
编辑跟随你的例子:
JS:
function ShowUploadDialog() {
document.getElementById('<%= uplReportLogo.ClientID %>').click();
document.getElementById('<%= hdnInvokeFileUpload.ClientID %>').value = $('input[type=file]').val();
__doPostBack('fileUpload', '');
}
ASPX:
<asp:Button runat="server" ID="btnReportImage" Text="Load Image..." CssClass="button action load"
OnClientClick="ShowUploadDialog();" />
CS:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string _evt = this.Request["__EVENTTARGET"]; // 1st parameter
string _eva = this.Request["__EVENTARGUMENT"]; // 2nd parameter
switch (_evt)
{
case "fileUpload":
btnReportImage_Click();
break;
default:
break;
}
}
}
protected void btnReportImage_Click() // remove sender & event arguments here, you do not need them
{
//your code
}
答案 1 :(得分:0)
由于访问被拒绝错误,表单无法提交,您只需要覆盖框架生成的函数并处理异常,直到表单未提交为止继续调用此函数。我遇到了同样的问题,现在解决了。 在您的页面上添加此功能。 B.O.Luck
function __doPostBack(eventTarget, eventArgument) {
try {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
catch (e) {
__doPostBack(eventTarget, eventArgument);
}
}