开发者

AJAX Update Panel - Action button click on drop down selection

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"&开发者_JAVA百科gt;
            <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.


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.


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());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜