0

I have two User Controls namely:

  1. ucGILocation.ascx (UC1)
  2. UcUpdateReportDescriptionSection.ascx (UC2)

which both are loaded in an .aspx page named NewReport.aspx

I want to hide some controls from UC2 when a dropDownList from UC1 selected index == 3.

However, it gives me a NullException error when I hide the controls in UC2 whenever the dropdownlist's index selected == 3.

I believe it is caused by the page_load because whenever the dropdown list selectedIndexChanged occurs, it will post-back the whole NewReport.aspx and since the page_load is not finish, it is already trying to hide the controls from UC2 .

Here is my code:

  1. UC1 (code behind)

    protected void ddlGIUpdatePillar_SelectedIndexChanged(object sender, System.EventArgs e) //akmal adds to hide div_locationItems when "Cyber Security" is selected
    {
        if (ddlGIUpdatePillar.SelectedIndex == 3)//checking if "Cyber Security" is selected
        {
            div_locationItems.Visible = false;
            UcUpdateReportDescriptionSection pnl_toHideDescItems2 = (UcUpdateReportDescriptionSection)Page.FindControl("panel_hideDescItems2");
            pnl_toHideDescItems2.Visible = false;
         }
    }
    

the third line

    pnl_toHideDescItems1.Visible = false;

is not executed since it throws NullReference exception. Is there any other alternatives that I can do to make it work.

Chrisantics
  • 165
  • 2
  • 16
  • Can you use a client side logic to perform this? With javascript / jquery you can hide controls after the page is fully load, via jquery you can bind to the onchanged event of your dropdown and hide there the elements of the second user control. – Filippo Agostini Oct 27 '17 at 07:57
  • What you are looking for are called `Delegates`: https://stackoverflow.com/questions/2920145/how-to-work-with-delegates-and-event-handler-for-user-control – VDWWD Oct 27 '17 at 10:04

0 Answers0