【问题标题】:selecting data from dropdown without refreshing the page从下拉列表中选择数据而不刷新页面
【发布时间】:2015-07-07 11:15:18
【问题描述】:

在我的应用程序中,当我从下拉列表(ddlcategory)中选择类别时,数据将绑定在纯度下拉列表(ddlpurity)中。但是页面正在刷新。所以我使用更新面板来解决这个问题(页面刷新) problem.Now the data is not binding to the ddlpurity ,when data from ddlcategory is selected.How can i bind the data to ddlpurity without refreshing the page.

asp 设计页面

<asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager>
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
 <Triggers>
     <asp:Asyncpostbacktrigger controlid="ddlcategory" eventname="SelectedIndexChanged" />
   </Triggers>
       <ContentTemplate>
             <asp:DropDownList ID="ddlcategory" class="form-control txtboxmargin validate[required]" runat="server" 
               AutoPostBack="True" onselectedindexchanged="ddlcategory_SelectedIndexChanged" AppendDataBoundItems="True">
             <asp:ListItem Value="">--select category--</asp:ListItem>
           </asp:DropDownList>
       </ContentTemplate>

   </asp:UpdatePanel>

<asp:DropDownList ID="ddlpurity" 
               class="form-control txtboxmargin" AutoPostBack="True" runat="server"
               onselectedindexchanged="ddlpurity_SelectedIndexChanged">
           </asp:DropDownList>

C# 代码:

protected void ddlcategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlcategory.SelectedItem.Text == "Gold")
        {
            BindDDLGoldPurity();
            lblheadpurity.Text = "ADD GOLD PURITY";
            txtsalesrate.ReadOnly = true;

        }
        if (ddlcategory.SelectedItem.Text == "Silver")
        {
            BindDDLSilverPurity();
            lblheadpurity.Text = "ADD SILVER PURITY";
            txtsalesrate.ReadOnly = true;

        }
        if (ddlcategory.SelectedItem.Text == "Gemstones")
        {
            txtsalesrate.ReadOnly = false;
            txtsalesrate.Text = "";

            ddlpurity.Items.Clear();

        }

    }

【问题讨论】:

  • 将它们放在同一个更新面板中
  • 正如@Jeremy 所说,您需要在更新面板中添加dropdownlist。并且在下拉列表的其他部分设置 AutoPostBack=false
  • 但如果我添加标签(lblcategory,lblpurity),它会显示“扩展器不能位于与其扩展的控件不同的 UpdatePanel 中。”

标签: c# asp.net


【解决方案1】:

设置 AutoPostBack="False" 以摆脱刷新行为。

【讨论】:

    【解决方案2】:

    您应该包装 UpdatePanel 中的两个控件。

    在第二部分。删除

    AutoPostBack="true" 
    

    来自您的DropDownList

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel runat="server" ID="UpdatePanel" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddlcategory" EventName="SelectedIndexChanged" />
        </Triggers>
        <ContentTemplate>
            <asp:DropDownList ID="ddlcategory" class="form-control txtboxmargin validate[required]"
                runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlcategory_SelectedIndexChanged"
                AppendDataBoundItems="True">
                <asp:ListItem Value="">--select category--</asp:ListItem>
            </asp:DropDownList>
            <asp:DropDownList ID="ddlpurity" class="form-control txtboxmargin" AutoPostBack="True"
                runat="server" OnSelectedIndexChanged="ddlpurity_SelectedIndexChanged">
            </asp:DropDownList>
        </ContentTemplate>
    </asp:UpdatePanel>
    

    您也可以使用javascript/jquery 来执行数据库请求。

    reference

    【讨论】:

    • 但它显示错误..我们不能在其中包含 div??
    • 错误:“扩展程序不能位于与其扩展的控件不同的 UpdatePanel 中。”
    • 你能展示你在 js fiddle 中尝试过的代码吗
    • 您的标签是在面板内部还是外部?见this
    猜你喜欢
    • 2017-11-19
    • 2012-02-06
    • 1970-01-01
    • 2023-03-11
    • 2019-12-23
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    相关资源
    最近更新 更多