开发者

Get selected dropdown option in code-behind when ajax population

I had a huge problem and spend hours trying to make it work, but no luck.

My issue was, I had 2 dropdowns, once the first one is selected, the second is ajax populated. But when I want to capture it using C# codebehind, the selected value won't reflec开发者_如何学Ct.

Is there any proper way by doing this and capturing the second dropdown, using code behind only, without using the isPostBack Request method?

If you check out my new running website, below the navigation, you will see my scenario.

http://www.mabinx.com/

I had to capture it on this page : http://www.mabinx.com/AddYourWebsite

Any help will be appreciated! :)


Put the second Drop Down in an Update Panel, then assign a code behind method to the attribute OnSelectedIndexChange, like so:

<asp:UpdatePanel runat="server">
<ContentPanel>
    <asp:DropDownList ID="MYDDL" runat="server" 
    OnSelectedIndexChanged="MethodThatRunsWhenChangeIsMade" AutoPostBack="true">
    </asp:DropDownList>
</ContentPanel>
</asp:UpdatePanel

And then in code behind:

protected void MethodThatRunsWhenChangeIsMade(object sender, EventArgs e)

{
// Do something, like populate a third dropdown
// If you need to populate another drop down, or something similar, put that in the same update panel
}


Solution :

  • If you populate a .Net dropdown with ajax, the codebehind won't get the dynamically added list items on postback.

  • All you have to do is to create a .Net hidden input tag (runat server), and bind an onChange event, and populate the .Net hidden field with the selected dropdown selection value.

  • To reference the selected ajax'ed dropdown list item in the codebehind, just reference the .Net hidden field value.


Your click event redirects the browser to perform the search:

$('.btnSearch').click(function (e) {
    e.preventDefault();
    window.location = "/Search/" + $('.ddlCountry option:selected').val() + "/" + $('.ddlState option:selected').val() + "/" + $('.ddlCategory option:selected').val();
});

As the page is redirecting and not posting back the selected value from the ddlState DropDownList will not be available when the page loads the subsequent time.

Instead of changing the location using JavaScript, why not use an ImageButton control which posts back, enabling you to retrieve whatever values you need from the page and then redirect the response to perform your search.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜