views:

27

answers:

2

Evening all

I have the following scenarion. I have a range of drop down menus where a clietn can select. The code below is wrapped in an update panel but without this, the button click fires a method to retrieve the number of products. So for example, an item is selected from ddlCategory, the btnValidate is clicked and the label returns the number of products within that category.

I have the following code for an update panel - I'm just not sure how to implement if effectively.

<asp:UpdatePanel ID="UpdatePanel1" runat="Server">
            <ContentTemplate>

        <asp:Label ID="lblSearchResultsStatus" runat="server" Text="Number of results found: "></asp:Label>
        <asp:Label ID="lblSearchResults1" runat="server" Text=""></asp:Label>
            <br />
        <br />
        <asp:Button ID="btnValidate" runat="server" Text="Validate Search" 
                OnClick="btnValidate_Click" Width="120px" />
                 </ContentTemplate>
            </asp:UpdatePanel>

How do I go about wiring the update panel so that when a drop down list item is selected, the buttong is effectively clicked?

Do I have to implement something on each of the ddlSelectedIndexChanged event or is there a property within the update panel that does?

Apologies for the noob question.

A: 

You can call your btnValidate_Click event from codebehind at any point, i.e. Page_Load

protected void Page_Load(object sender, EventArgs e)
{
    btnValidate_Click(btnValidate, new EventArgs());
}
Marko
Cheers Marko - I'm sure I can clean this up, buit for a bodge to demo this, that's grand.Thank you.
Ricardo Deano
You're welcome :) Please accept the answer if it helped you. Thanks!!
Marko
Sorry Marko - although this was the way I did it (to force it thorugh in my demo), I will be using the method Nekno suggested.However, +1 for help and calling the button click action.
Ricardo Deano
No probs - nekno's answer is the way to go, but you now know how to invoke an event from code-behind :)
Marko
+1  A: 

The point of the UpdatePanel is to update a portion of the page with an AsyncPostBack instead of reloading the whole page, but in order for the drop-down lists to trigger an AsyncPostBack automatically, they must be on an UpdatePanel. In order to update the labels, they must be on the same UpdatePanel with the labels.

A common pattern to implement what you want to accomplish:

  • Put the DDLs on the UpdatePanel, and set AutoPostBack="true" on each DDL, so they trigger AsyncPostBacks.
  • Add an event handler for SelectedIndexChanged on each DDL (can be the same event handler).
  • Move whatever you do in btnValidate_Click to another method.
  • Call the new method from btnValidate_Click and the SelectedIndexChanged event handler(s) so they all perform the same function.
nekno