Jul 29, 2007

UpdatePanel tricks

Trigger update from outside of the panel



Nesting UpdatePanel Controls

Nested panels are useful when you want to be able to refresh specific regions of the page separately and refresh multiple regions at the same time. You can accomplish this by setting the UpdateMode property of both the outer and nested controls to Conditional. This causes the outer panel not to refresh its page region if only the inner panel is refreshing. However, if the outer panel is refreshed, the nested panels are refreshed also. By default, the UpdateMode property of an UpdatePanel control is set to Always. This means that whenever a partial-page refresh is triggered, the UpdatePanel control will refresh the page region regardless of whether it was the UpdatePanel control that was triggered. To make sure that the UpdatePanel control will refresh only when it has been triggered, you can set the UpdateMode property of the UpdatePanel control to Conditional.



Sibling UpdatePanel

For sibling updatepanels, if their updatemode is "conditional", then by default they only update their own area. But you can use trigger in case when click one button in side of one panel to update the content in other panel.

Disabling Automatic Triggers

If you don't want your the control trigger update in update panel, (some time this is desireable when you want to trigger update by certain control, instead of all control in update panel), you need to set ChildrenAsTriggers to false

            <%@ Page Language="C#" %>



            Untitled Page

Managing Triggers and Refreshing an UpdatePanel Programmatically

Sometimes, if you want more granular control how to update the update panel, you can do that programatically. In this case, you don't need to specify statically trigger in aspx page, you do that in you .net code.

            protected void Page_Load()

            protected void ChoicesRadioButtonList_SelectedIndexChanged(object sender, EventArgs e)
            SortedList answers = this.AnsweredQuestions;
            RadioButtonList r = (RadioButtonList)sender;
            answers[r.ToolTip] = r.SelectedValue;
            this.AnsweredQuestions = answers;

            ResultsList.DataSource = this.AnsweredQuestions;

            if (this.AnsweredQuestions.Count == SurveyDataList.Items.Count)
            SubmitButton.Visible = true;


Adding script to client using ScriptManager

You can add script to page either statically or dynamically, for example

            void Page_Load(object sender, EventArgs e)
            ScriptManager smgr = ScriptManager.GetCurrent(Page);
            if (smgr == null)
            throw new Exception("ScriptManager not found");

            ScriptReference sref = new ScriptReference();
            sref.Path = "~/scripts/test.js";