How to fill an asp:DropDown client side?

后端 未结 5 905
梦如初夏
梦如初夏 2020-12-20 15:40

i have a web-page with two controls:

\"enter

相关标签:
5条回答
  • 2020-12-20 16:04

    Related to Attempt#1 to solve the problem of the page position you can try using this in the aspx page header

    <%@ Page MaintainScrollPositionOnPostback="true" %>
    

    and register a javascript in the cbCountry_SelectedIndexChanged method to set focus at the state combobox.

    0 讨论(0)
  • 2020-12-20 16:10

    Go to the setting of the dropdownList

    Set AutoPostBack=True ,

    0 讨论(0)
  • 2020-12-20 16:14

    Use the AJAX Control Toolkit... it has a CascadingDropDown which would be perfect! :)

    http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

    0 讨论(0)
  • 2020-12-20 16:16

    I have a trick and it works. It is using a hidden button inside the same update pane the the State dropdown.

    <script type="text/javascript">
        function ddlCountry_changed() {
            document.getElementById('<%= btnLoad.ClientID %>').click();
        }
    </script>
    
    Country:
    <asp:DropDownList ID="ddlCountry" runat="server" onchange="ddlCountry_changed();">
        <asp:ListItem Text="Vietnam" Value="1"></asp:ListItem>
        <asp:ListItem Text="United State" Value="2"></asp:ListItem>
    </asp:DropDownList>
    
    <asp:UpdatePanel ID="updatePanel" runat="server">
        <ContentTemplate>
            <asp:Button ID="btnLoad" style="display: none" runat="server" OnClick="btnLoad_OnClick" />
            State:
            <asp:DropDownList ID="ddlState" runat="server">
            </asp:DropDownList>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    Code behind:

        protected void btnLoad_OnClick(object sender, EventArgs e)
        {
            ddlState.Items.Clear();
    
            if (ddlCountry.SelectedValue == "2")
            {
                ddlState.Items.Add(new ListItem() { Text = "California", Value = "1" });
            }
            else
            {
                ddlState.Items.Add(new ListItem() { Text = "Saigon", Value = "2" });
            }
        }
    
    0 讨论(0)
  • 2020-12-20 16:17

    Updating the DropDownList controls using javascript won't alter the viewstate, which means that in each post the changes will be overridden.

    I have done something similar serializing to JSON the content of the lists in hidden fields and then in server code deserializing it on each post

    This is an answer that shows a full working example to show something similar (check the accepted answer).

    ASP.NET jQuery double listbox edit and save

    Edit 1

    BTW In order to get rid of:

    Invalid postback or callback argument.

    You can disable the viewstate validation, however this is not recommended, you should only do it if you are in an intranet where you can trust most of the time in your users

    <%@ Page EnableEventValidation="false"
    
    0 讨论(0)
提交回复
热议问题