Can you use findcontrol with an Ajax Accordion in

2019-08-03 18:17发布

问题:

This goes along with another question that I have asked, I have a drop down list on my master page that causes postback when changed and in that index changed method I am trying to find my ajax accordion to look at the selected index of it

protected void ddlSelectedCustomer_SelectedIndexChanged(object sender, EventArgs e)
{
    CustomerSelected();

    Response.AppendHeader("Refresh", "0;URL=storefront.aspx");

    ViewState["SelectedAccordionIndex"] = (AjaxControlToolkit.Accordion)FindControl("MyAccordion").SelectedIndex;
}

Error that I get

Object reference not set to an instance of an object.

回答1:

the problem is FindControl returns an Object. Cast it to an accordion and try again: ViewState["SelectedAccordionIndex"] = ((Accordion)FindControl("MyAccordion")).SelectedIndex;

If this doesn't why you may need to drill deeper, e.g. ((Accordion)this.FindControl(<the id of your content placeholder your accordion is in>).FindControl("MyAccordion")).SelectedIndex;



回答2:

You should be able to use it the same as with any other ASP control. As mentioned above, it returns as an Object so you will have to cast it as any control you are using, whether it be a dropdown, a listbox, or in this case an ajax accordion