C# Don';在下拉列表中选择“索引更改”后,是否不刷新页面?

C# Don';在下拉列表中选择“索引更改”后,是否不刷新页面?,c#,asp.net,C#,Asp.net,我正在使用两个下拉框,在用户根据他们选择的内容选择第一个必需的下拉框之后,另一个下拉框将加载最终列表。我有工作,但我不希望页面重新加载后,第一个下拉选项被选中,有没有办法解决这个问题下面是我的测试代码 隐藏代码 受保护的无效页面加载(对象发送方、事件参数e) { 如果(!IsPostBack) { DropDownList1.Items.Add(新列表项(“3天”、“3天”)); DropDownList1.Items.Add(新列表项(“4天”、“4天”)); DropDownList1.I

我正在使用两个下拉框,在用户根据他们选择的内容选择第一个必需的下拉框之后,另一个下拉框将加载最终列表。我有工作,但我不希望页面重新加载后,第一个下拉选项被选中,有没有办法解决这个问题下面是我的测试代码

隐藏代码
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
DropDownList1.Items.Add(新列表项(“3天”、“3天”));
DropDownList1.Items.Add(新列表项(“4天”、“4天”));
DropDownList1.Items.Add(新列表项(“5天”、“5天”));
DropDownList1.Items.Add(新列表项(“7天”、“7天”));
}
}
受保护的void DropDownList1\u SelectedIndexChanged(对象发送方,事件参数e)
{
DropDownList2.Items.Clear();
DropDownList3.Items.AddRange(GetListItems(DropDownList1.SelectedValue));
}
私有ListItem[]GetListItems(字符串值)
{
var items=新列表
();
如果(值=“3天”)
{
DropDownList2.Items.Add(新列表项(“1”、“1”));
}
如果(值=“4天”)
{
DropDownList2.Items.Add(新列表项(“2”、“2”));
}
如果(值=“5天”)
{
DropDownList2.Items.Add(新列表项(“3”、“3”));
}
如果(值=“7天”)
{
DropDownList2.Items.Add(新列表项(“4”、“4”));
}
返回项目。ToArray();
}

您需要在通话中使用,以避免回发

首先,您需要从下拉列表中删除自动回发和事件处理程序:

<div class="1">
<asp:DropDownList ID="DropDownList1" runat="server">
  <asp:ListItem Text="Select..." Value="No selection made"></asp:ListItem>
</asp:DropDownList>
</div>
<div class="1">
  <asp:DropDownList ID="DropDownList2" runat="server">
    <asp:ListItem Text="Select..." Value="No selection made"></asp:ListItem>
  </asp:DropDownList>
</div>

然后,使用JQuery进行调用,该调用发送所选选项并返回第二个下拉列表的选项

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $('#<%=DropDownList1.ClientID%>').change(function() {
            var selectedOption = $(this).val();
            console.log(selectedOption); //verify you have the value
            $.ajax({
                  type: "POST",
                  url: "codebehind.aspx/GetListItems",
                  data: JSON.stringify({ value: selectionOption }),
                  contentType: "application/json; charset=utf-8",
                  dataType: "json"
                }).success(function (data) {
                    console.log(data);  //verify the format of the return data
                    obj = JSON.parse(data.d);
                    console.log(obj); //verify obj structure
                    //adding options to drop down list will look something like...
                    $.each(data, function (index, optiondata) {
                        $("#DropDownList2").append("<option value='" + optiondata.Value + "'>" + optiondata.Text + "</option>");
                    });

                });
    });
});
</script>

$(文档).ready(函数(){
$('#')。更改(函数(){
var selectedOption=$(this.val();
console.log(selectedOption);//验证您是否具有该值
$.ajax({
类型:“POST”,
url:“codebehind.aspx/GetListItems”,
数据:JSON.stringify({value:selectionOption}),
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”
}).成功(功能(数据){
console.log(data);//验证返回数据的格式
obj=JSON.parse(data.d);
console.log(obj);//验证obj结构
//将选项添加到下拉列表将类似于。。。
$.each(数据、函数(索引、选项数据){
$(“#DropDownList2”).append(“+optiondata.Text+”);
});
});
});
});
您需要将代码隐藏中的函数更改为:

[WebMethod]
public static List<ListItem> GetListItems(string value)
{

   //..do stuff

   return JsonConvert.SerializeObject(items);
}
[WebMethod]
公共静态列表GetListItems(字符串值)
{
//…做事
返回JsonConvert.SerializeObject(项);
}
() ScriptManager控件和UpdatePanel控件。这些控件消除了每次回发时刷新整个页面的要求,从而改善了用户体验。默认情况下,UpdatePanel控件内的回发控件(如按钮)会导致部分页面更新。默认情况下,UpdatePanel控件之外的按钮或其他控件会刷新整个页面

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <fieldset>
            <div class="1">
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Text="Select..." Value="No selection made"></asp:ListItem>
                </asp:DropDownList>
            </div>
            <div class="1">
                <asp:DropDownList ID="DropDownList2" runat="server">
                    <asp:ListItem Text="Select..." Value="No selection made"></asp:ListItem>
                </asp:DropDownList>
            </div>
        </fieldset>
    </ContentTemplate>
</asp:UpdatePanel>


不重新加载页面的唯一方法是使用AJAX,而不是发布。你愿意使用JQuery吗?我对这一点很陌生,你能详细解释一下吗?将你的
DropDownList
包装在一个更新面板中,只有下拉列表下的部分会异步重新加载,页面的其余部分不会。一旦我删除了Autopost和事件处理程序,第二个下拉列表就不再加载选项。其次,使用代码隐藏,现在我得到了一个错误,C#这个成员不能从静态上下文中引用下拉列表应该是空的,直到AJAX工作。我的答案中有一些关于AJAX和JQuery的参考资料,但这些并不是详尽无遗的。您可能需要了解如何在JQuery中链接(包括document.ready和script标记)。我会用几个额外的助手来更新我的答案,为你指明正确的方向。另外,整个要点是在不回发的情况下填充下拉列表,这样页面就不会刷新。您需要解决静态引用错误,因为AJAX/Webmethod要求GetListItems是一个静态函数。