简体   繁体   English

如何使用asp.net中的另一个下拉列表更改下拉列表选择的索引和可见性?

[英]How to change drop down list selected index and visibility using another drop down list in asp.net?

I have a drop down list like this in my asp.net application, 我在asp.net应用程序中有一个这样的下拉列表,

<asp:DropDownList runat="server" ID="ddlChanges" CssClass="selectstyle" onchange="javascript:ddlChangesChanged();"> 
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem> 
<asp:ListItem Text="Status Change" Value="1"></asp:ListItem>
<asp:ListItem Text="Product Name Change" Value="2"></asp:ListItem>       
<asp:ListItem Text="Category Change" Value="3"></asp:ListItem>
</asp:DropDownList>

So, when user selects "Status Change" from the list, another dropdown list with following values appears, 因此,当用户从列表中选择“状态更改”时,会出现另一个具有以下值的下拉列表,

<asp:DropDownList runat="server" ID="ddlStatus" CssClass="selectstyle"  Style="visibility: hidden;"> 
<asp:ListItem Text="-- Select -- " Value="-1"></asp:ListItem> 
<asp:ListItem Text="Pending" Value="1"></asp:ListItem>
<asp:ListItem Text="Fixed" Value="2"></asp:ListItem>       
<asp:ListItem Text="Cancelled" Value="3"></asp:ListItem>
</asp:DropDownList>

<asp:Button ID="btnUpdate" CssClass="selectstyle" runat="server" Text="Update" OnClick="btnUpdate_Click"></asp:Button>

If user does not select any value from the drop down list (ddlStatus), I am displaying an alert message ("You must select a status.") using JavaScript in the btnUpdate click event, 如果用户未从下拉列表(ddlStatus)中选择任何值,则我将在btnUpdate单击事件中使用JavaScript显示警告消息(“您必须选择状态。”),

if (this.ddlStatus.SelectedItem.Value.Trim() == "-1")
{
    string script = "<script type=\"text/javascript\">alert('You must select a status.');</script>";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}

But after this, my page refreshes and drop down list ddlChanges has "Status Change" (previously selected item) as selected item and drop down list (ddlStatus) gets hidden. 但是在此之后,我的页面将刷新,并且下拉列表ddlChanges具有“状态更改”(以前选择的项目),因为选中的项目和下拉列表(ddlStatus)被隐藏了。 I want ddlChanges drop down list to have -- Select -- as the selected item and ddlStatus should be visible. 我希望ddlChanges下拉列表具有-选择-作为所选项目和ddlStatus应该是可见的。

How can I do that? 我怎样才能做到这一点?

You are doing a full postback (all changes on client get reset, except the posted back input values) - so either do a client (javascript) validation (best option), or switch to ajax partial postback (faster option), or simply just change this (slowest option): 您正在执行完整的回发(客户端上的所有更改都将被重置,但回发的输入值除外)-因此,请进行客户端(javascript)验证(最佳选项),或切换到Ajax部分回发(更快的选项),或者只是更改此选项(最慢的选项):

if (this.ddlStatus.SelectedItem.Value.Trim() == "-1")
{
    ddlStatus.Attributes["style"] = null;
    string script = "<script type=\"text/javascript\">alert('You must select a status.');</script>";
    ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", script);
}

btnUpdate click event raises the server side code, and the server does not know about what the user has selected on client. btnUpdate click事件引发服务器端代码,服务器不知道用户在客户端上选择了什么。 one option could be use client side validation to avoid Post back and preserve the selections at client 一种选择是使用客户端验证,以避免回发并保留客户端的选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用另一个下拉列表 asp.net core 3.0 过滤下拉列表的选项 - How to filter the options of a drop down list using another drop down list asp.net core 3.0 asp.net:设置为下拉列表选择的默认值 - asp.net : set default value selected for a drop down list 通过另一个下拉列表ID ASP.Net筛选下拉列表 - Filter Drop down list by another drop down list Id ASP.Net 替代ASP.NET下拉列表 - Alternative to ASP.NET drop down list 从jsp.net中的列表使用级联下拉列表 - Cascading drop down using jquery from list in asp.net 如何在ASP.NET Web应用程序中使用JavaScript根据下拉列表中的所选选项显示不同的输入类型 - How to display different input type depending on selected option from a drop down list using javascript in asp.net web application 根据另一个下拉列表更改下拉列表值 - Change drop down list values depending on another drop down list 根据另一个下拉列表中的所选选项更改下拉列表中的项目 - Change items in a drop-down list depending on the selected option in another drop-down list 根据另一个选定的下拉列表更改下拉列表中的选项 - Change options in a drop-down list depending on another selected drop-down list 根据另一个下拉列表中的所选项目更改下拉所选项目 - Change drop down selected item based on selected item of another drop down list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM