Friday, May 10, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
83
rated 0 times [  87] [ 4]  / answers: 1 / hits: 9608  / 11 Years ago, tue, december 3, 2013, 12:00:00

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?


More From » c#

 Answers
16

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
}

[#49888] Monday, December 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
adriannemyiag

Total Points: 504
Total Questions: 105
Total Answers: 99

Location: Ecuador
Member since Thu, Apr 22, 2021
3 Years ago
;