I have an ASP.NET Webform, Which includes a server side Button. I have registered an Onclick button event on it.
<asp:Button ID="UploadButton" CssClass="btn add btn-primary" runat="server" Text="Upload File" OnClick="UploadButton_Click" />
what i am trying to do is,
On the Server Side, after Processing the required things, i have filled a response object, which i want to return to client Side (like assign it to a javascript variable).
How can i get the object returned from button click event on client side.
protected ResponseModel UploadButton_Click(object sender, EventArgs e)
{
ResponseModel responseModel = new ResponseModel();
//Processing File
//filling Response Object
return responseModel
}
Initially return type of the UploadButton_Click() method was void.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
I’m not sure how complex or simple is your response object but have you tried using a <asp:HiddenField />? Something like:
<asp:HiddenField ID="hiddenField" runat="server" value="" />
Then in your code-behind:
protected void UploadButton_Click(object sender, EventArgs e)
{
//Processing File
//filling Response Object
hiddenField.Value = responseObject.customProperty;
}
Javascript should be able to find it via:
document.getElementById('<%=hiddenField.ClientID%>')
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0