下拉列表和更新面板

DropDownList and Update Panel(下拉列表和更新面板)
本文介绍了下拉列表和更新面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了地址控件,其中包含 2 个 DropDownList(用于城市和国家/地区)和几个 TextBox.第二个 DropDownList 数据源依赖于第一个 DropDownList 数据源.

<legend><%=Title%></legend><asp:UpdatePanel ID="UpdatePanel1" runat="server"><内容模板><div><label for="<%=ddlCountry.ClientID %>">国家</label><asp:DropDownList runat="server" ID="ddlCountry"DataTextField="名称" DataValueField="Id"数据源="<%#Facade.Addresses.GetCountries() %>"自动回传=真"OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"/>

<div><label for="<%=ddlCity.ClientID %>">城市</label><asp:DropDownList runat="server" ID="ddlCity"DataTextField="名称" DataValueField="名称"/>

</内容模板><触发器><asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged"/></触发器></asp:UpdatePanel><div><label for="<%=txtStreet.ClientID %>">Street</label><uc:TextBox ID="txtStreet" Text="<%#Address.Street %>"runat="服务器"/>

<div><label for="<%=txtBlock.ClientID %>">Block</label><uc:TextBox ID="txtBlock" Text="<%#Address.Block %>"runat="服务器"/>

<div></fieldset>

背后的代码

protected void Page_Init(object sender, EventArgs e){ddlCountry.DataBind();如果 (!IsPostBack){ddlCity.DataSource = Facade.Addresses.GetCities(countryId);ddlCity.DataBind();}}protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e){ddlCity.DataSource = Facade.Addresses.GetCities(countryId);ddlCity.DataBind();}

效果很好.但是如果页面上的其他控件导致回发,当 ddlCity 中的 SelectedValue 设置为第一个(默认)值时.

我该如何避免?

解决方案

Page_Init 上的代码移动到 Page_Load 并放入 !IsPostBack>

protected void Page_Load(object sender, EventArgs e){如果 (!IsPostBack){ddlCountry.DataBind();ddlCity.DataSource = Facade.Addresses.GetCities(countryId);ddlCity.DataBind();}}

I develop address control, which contains 2 DropDownLists (for cities and countries) and several TextBoxes. The second DropDownList DataSource depends on the first DropDownList DataSource.

<fieldset>
 <legend><%=Title%></legend>
  <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div>
            <label for="<%=ddlCountry.ClientID %>">Country</label>
            <asp:DropDownList runat="server" ID="ddlCountry" 
              DataTextField="Name" DataValueField="Id" 
              DataSource="<%#Facade.Addresses.GetCountries() %>"
              AutoPostBack="true" 
              OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged"
            />
        </div>
        <div>
            <label for="<%=ddlCity.ClientID %>">City</label>
            <asp:DropDownList runat="server" ID="ddlCity" 
                DataTextField="Name"   DataValueField="Name" />
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="ddlCountry" EventName="SelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>
<div>
    <label for="<%=txtStreet.ClientID %>">Street</label>
    <uc:TextBox ID="txtStreet" Text="<%#Address.Street %>" runat="server" />
</div>
<div>
    <label for="<%=txtBlock.ClientID %>">Block</label>
    <uc:TextBox ID="txtBlock" Text="<%#Address.Block %>" runat="server" />
</div>
<div>
</fieldset> 

Code Behind

protected void Page_Init(object sender, EventArgs e)
  {

    ddlCountry.DataBind();
     if (!IsPostBack)
       {
         ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
         ddlCity.DataBind();
      }

}

protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
  {
     ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
     ddlCity.DataBind();
 }

It works good. But if other control on the page causes PostBack, when the SelectedValue in ddlCity sets to the first (default) value.

How do I avoid it?

解决方案

Move the code on Page_Init to Page_Load and put it inside !IsPostBack

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ddlCountry.DataBind();
        ddlCity.DataSource = Facade.Addresses.GetCities(countryId);
        ddlCity.DataBind();
    }
}

这篇关于下拉列表和更新面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)