Search code examples
c#asp.netuser-controlspostbackurl

User control PostBack URL Issues


In my project, I have create user control and dynamically load it in the place holder. But now I am having problem with PostBack URL issues.

I need to pass value to other page from the user control to other page.

Please see my code at below

Here is my front-end user control

<div class="rightColumnModule2TitleContainer">
<table class="rightColumnModule2Table">
    <tr>
        <td>
            <div class="rightColumnModule2Title">Contact Person</div>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="Button1" runat="server" Text="Button"  PostBackUrl="~/Module/Sales/Customer/CreateContactListing.aspx"/>

        </td>
    </tr>
</table>

In my other page

 protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!Page.IsPostBack)
            {
                if (PreviousPage.IsCrossPagePostBack)
                {

                }
             }
        }
        catch (Exception ex)
        {
            logger.Error(ex.Message);
            throw;
        }
    }

I am getting error on (PreviousPage.IsCrossPagePostBack)

Here is my error

Unable to cast object of type 'ASP.module_sales_customer_createsalescustomer_aspx' to type 'LewreERP_MixsolProject.Module.Sales.Customer.SalesCustomerListing'.

Please guide me. Thanks in advance.


Solution

  • First you need to check for null for PreviousPage.

     if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostBack)
     {
     }
    

    Take a look at this article. It explains PreviousPage.IsCrossPagePostBack effectively with user controls.