从下拉菜单中选择数据而不刷新页面

本文关键字:刷新 数据 下拉菜单 选择 | 更新日期: 2023-09-27 18:01:33

在我的应用程序中,当我从下拉列表(ddlcategory)中选择类别时,数据将绑定在纯度下拉列表(ddlpurity)中。但这个页面令人耳目一新。所以我使用更新面板来解决这个(页面刷新)问题。现在,当选择来自ddlcategory的数据时,数据不会绑定到ddlpurity。如何在不刷新页面的情况下将数据绑定到ddlpurity ?

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();
        }
    }

从下拉菜单中选择数据而不刷新页面

设置AutoPostBack="False"来取消刷新行为

您应该两个控件都包装在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来执行数据库请求。

参见参考