最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Programmatically set Request["__EVENTTARGET"] from javascript - Stack Overflow

programmeradmin2浏览0评论

I'm writing ASP.NET application, and I should check on server-side value of Request["__EVENTTARGET"] in Page_Load. I need to set it value from client-side programmically. I try to set it by this:

document.getElementById('__EVENTTARGET').value = "my_value";

but it doesn't work. Can you help me, why? Thank you!

UPD:

function ShowUploadDialog(obj) {
            document.getElementById('<%= uplReportLogo.ClientID %>').click();
            document.getElementById('<%= hdnInvokeFileUpload.ClientID %>').value = $('input[type=file]').val();
            __doPostBack(obj.id, 'Click');
}

Server controls:

<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();
    }
}

I have "Access denied" exception on row theForm.submit(); Why?

I'm writing ASP.NET application, and I should check on server-side value of Request["__EVENTTARGET"] in Page_Load. I need to set it value from client-side programmically. I try to set it by this:

document.getElementById('__EVENTTARGET').value = "my_value";

but it doesn't work. Can you help me, why? Thank you!

UPD:

function ShowUploadDialog(obj) {
            document.getElementById('<%= uplReportLogo.ClientID %>').click();
            document.getElementById('<%= hdnInvokeFileUpload.ClientID %>').value = $('input[type=file]').val();
            __doPostBack(obj.id, 'Click');
}

Server controls:

<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();
    }
}

I have "Access denied" exception on row theForm.submit(); Why?

Share Improve this question edited Dec 3, 2013 at 17:50 Max Zhukov asked Dec 3, 2013 at 17:22 Max ZhukovMax Zhukov 8971 gold badge8 silver badges34 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

That variable is used by ASP.NET to store the sender of any postback, so I think your value is getting overwritten each time. You may try to invoke

__doPostBack('my_value','any_other_argument')

by JS, then checking such value in Page_Load event.

For this to work, be sure to remove EventValidation attribute in the first line of your ASPX file.

This is how you can handle it on code-behind:

    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;
            }
        }
    }

EDIT:

Edited to follow your example:

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
}

it's due to the access denied error the form is unable to submit, You just need to override the function generated by framework and handle the exception untill the form is not submitted just keep calling this function. I got the same problem and it is resolved now. Add this function on your page. 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);
        }

    }
发布评论

评论列表(0)

  1. 暂无评论