javascriptasp.netasp.net-webpages

Get Response object from asp.net Button Onclick Event


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.


Solution

  • 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%>')