Configure Update Panel to run a particular method only?

J

John Kotuby

Hi all,
I have an update panel that holds only 2 listBoxes in a page surrounded by
40 other controls.
The purpose of the panel is -- select an Item in ListBox1 and update the
contents in ListBox2.
I have Autopostback="true" on ListBox1.
Even though ListBox2 is actually the only thing refreshing on the page, as
expected, I am pretty sure that a lot of the startup code to the page is
also running unneccesarily, causing a long delay.

Can someone give me a quick pointer on how to tell the page to run just the
SelectedIndexChanged code when the postback is the result of the ListBox1
within the Updatepanel?

A link to an article that targets this question would be fine.

Thanks for any help...
 
M

Mike Placentra II

Can someone give me a quick pointer on how to tell the page to run just the
SelectedIndexChanged code when the postback is the result of the ListBox1
within the Updatepanel?

"Startup code" should usually be run in a conditional block. You can
check the value of Page.IsPostBack.

-Mike Placentra II
 
M

Milosz Skalecki [MCAD]

Hi John,

Several posibilities- you could read IsInPartialRendering property of the
UpdatePanel to determine if a particular panel is being refreshed ie:
if (myPanel1.IsinparyialRendering)
{
}
or even better, use client-side ajax scripting to populate the lisbox

-- begin asp code --

<asp:ScriptManager runat="server" ID="manager" EnablePageMethods="true" />
Manufacturer:
<asp:ListBox runat="server" ID="list1" OnChange="Populate(this)">
<asp:ListItem Text="Audi" Value="Audi" />
<asp:ListItem Text="Porsche" Value="Porsche" />
<asp:ListItem Text="Ford" Value="Ford" />
<asp:ListItem Text="Honda" Value="Honda" />
</asp:ListBox>
Car:
<asp:ListBox runat="server" ID="list2" />
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />

<script type="text/javascript">

function Populate(list)
{
PageMethods.GetCars(list.options[list.selectedIndex].value,
OnSuccess, OnError, OnTimeOut);
}

function OnSuccess(cars)
{
var list2 = $get('<%= list2.ClientID %>');
list2.options.length = 0;

for (var i = 0; i < cars.length; i++)
{
list2.options = new Option(cars, cars);
}
}

function OnError(result)
{
alert('Error : ' + result.get_message());
}

function OnTimeOut(result)
{
alert('Sorry, timeout!');
}

</script>
-- end aspx code --

-- begin c# code beside --
[System.Web.Services.WebMethod(false)]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string[] GetCars(string manufacturer)
{
string[] cars = new string[10];

for (int i = 0; i < cars.Length; i++)
{
cars = manufacturer + " car " + i.ToString();
}

return cars;
}
-- end c'# code beside --


or just to check eventtarget hidden field value (of course when using
UpdatePanels and ListBox1.AutoPostBack set to true):

bool list1SelectedIndexChanged =
Request[postEventSourceID] == list1.UniqueID;

hope this helps
 
J

jkotuby

Thanks Milosz,
That tuorial was very helpful. Sorry I didn't respond sooner...got
busy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top