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?
2 Answers
Reset to default 4That 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);
}
}