Wednesday, March 23, 2011

Hide/Show controls with AJAX

I am using an ASP.NET AJAX-Enabled Web application (ASP.NET 2.0 and AJAX Toolkit 1.0) that contains one button and 2 UpdatePanels (UpdatePanel_1 and UpdatePanel_2)

The button is registered with RegisterAsyncPostBackControl in the ScriptManager object UpdatePanel_1 is in "Conditional" update mode and contains a TextBox.

UpdatePanel_2 is in "Always" update mode and contains another TextBox

When the button is pressed its handler calls UpdatePanel_1.Update() that updates the value of the TextBox based on a randomly selected value in a list; Also the UpdatePanel_2's TextBox is being updated automatically , also without page refresh

Based on the value of a boolean ViewState variable I would also like to hide/show the UpdatePanels alternatively but I get the error :

"Sys.InvalidOperationException: COuld not find UpdatePanel with ID 'UpdatePanel_2' (or UpdatePanel_1).

If it is  being updated dinamically then it must be inside another UpdatePanel"

How can it be done without adding extra wrapping UpdatePanels?

Thanks,

arunganu

protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager1.RegisterAsyncPostBackControl(Button1); 


    if (!IsPostBack)   
    {

        Visibility = true;
    }

    UpdatePanel_1.Visible = !Visibility;
    UpdatePanel_2.Visible = Visibility;

    Visibility = !Visibility;        
}


protected void Button1_Click(object sender, EventArgs e)
{
        if (Panel1.Visible)
                 UpdatePanel_1.Update();    
}

protected bool Visibility
{
    get
    {
        return (bool)(ViewState["Visibility"] ?? true);
    }
    set
    {
        ViewState["Visibility"] = value;
    }
}
From stackoverflow
  • The problem is that invisible controls aren't rendered to the client. So then trying to make them visible isn't going to work because as far as the client is concerned, they don't exist.

    Try using style="display:none", or use different CSS classes and styles for visible and invisible panels, rather than setting visible=false;

  • You can invisible, or visible controls is child of updatepanel, not invisible, visible updatepanel, I try use updatemode = conditional but error, and then I visible controls add to updatepanel. Hopy help you Thanks everybody post

0 comments:

Post a Comment