ASP.. NET服务器控件-基于下拉选择显示隐藏文本框

ASP.NET Server Controls - Show Hide Textbox based on Dropdown selection

本文关键字:显示 隐藏 文本 选择 于下拉 NET 服务器控件 ASP      更新时间:2023-09-26

根据用户从下拉菜单中选择的内容显示/隐藏文本框或整个div部分的最佳方法是什么?我不相信这可能与服务器控件,所以我将不得不使用常规的客户端HTML控件,对吗?谢谢你的建议。jQuery是最好的选择吗?

基于下拉选择,我希望能够显示以下Div,并在默认情况下隐藏Div。:

 <div id="divLimitPrice">Limit Price<br />
 <asp:TextBox ID="txtLimitPrice" runat="server" ValidationGroup="ValidationGroupOrder">    </asp:TextBox>

您可以像使用简单的html控件一样使用服务器控件。您只需要正确设置控件的呈现客户端id。下面是一个例子:(请参阅我所做的代码注释)

function TriggerChange(me)
{
    // get the drop down control
    var cTheDropDown = jQuery("#<%=ddlControl.ClientID%>");
    // find the value of the selected one
    var SelValue = jQuery('option:selected', cTheDropDown).attr('value');
    // now do what you like with it
    if(SelValue == "open")
      jQuery("#divLimitPrice").show();
    else
      jQuery("#divLimitPrice").hide();
}

的缩略版
function TriggerChange(me)
{
    // get the selected value from the drop down list
    //  and base on it, show or hide your div
    if(jQuery("#<%=ddlControl.ClientID%>").val() == "open")
      jQuery("#divLimitPrice").show();
    else
      jQuery("#divLimitPrice").hide();
}

在control上添加触发器as

<asp:DropDownList ID="ddlControl" runat="server" onchange="TriggerChange(this);">