[英]Enable second combo box if the the first combo box value is scheduled in C#
Greetings for the Day!! 当天的问候!
I am a newbie to DOT NET. 我是DOT NET的新手。 Developing a tool, building UI. 开发工具,构建UI。
I am stuck in generating combo box values on selection of other combo box. 我被困在选择其他组合框时生成组合框值。
Question is : There are 2 Combo box : DropDownList1 and DropDownList2 DropDownList1 values: 0-All 1-Not Scheduled 2-Rejected 3-Selected DropDownList2 values: 0-Select ALL 1-Poor Communication 3-Failed in test 4-Poor Typing skill 问题是:有2个组合框:DropDownList1和DropDownList2 DropDownList1值:0-全部1-未计划2-拒绝3-选择的DropDownList2值:0-选择全部1-差的沟通3-测试失败4键入技巧差
Values are populating in the combobox. 值正在组合框中填充。
// for DropDownList1 //对于DropDownList1
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Test_DB;User ID=sa");
con.Open();
//Interview Status
string Sql = "select Id,Status from dbo.InterviewStatus";
SqlCommand cmd = new SqlCommand(Sql, con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "Status";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
DropDownList1.Items.Insert(0, new ListItem("All", "0"));
}
Now, DropDownList2 should be enabled only when DropDownList1 is Rejected. 现在,仅当拒绝DropDownList1时,才应启用DropDownList2。 for other DropDownList1 values, DropDownList2 should be disabled. 对于其他DropDownList1值,应该禁用DropDownList2。
Kindly help me in resolving this. 请帮助我解决这个问题。
Thanks in advance :) 提前致谢 :)
If I understood you correctly, you can use the SelectedIndexChanged
event and check the values in your cboStatus
如果我对您的理解正确,则可以使用SelectedIndexChanged
事件并检查cboStatus
的值
private void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.ToString() == "2")
{
DropDownList2.Enabled = true;
}
}
EDIT: 编辑:
Since the index seems to coincide with your Status
values you actually need only the index, unless it is an important information for the user. 由于索引似乎与您的Status
值一致,因此您实际上只需要索引,除非它对用户是重要信息。 To populate you ComboBox
just get the values from you database as a List
and use the AddRange
method 要填充ComboBox
只需从数据库中以List
获取值,然后使用AddRange
方法
DropDownList1.Items.AddRange(listOfValues.ToArray());
You can do the cascading style drop down lists by following the below given approach, also you can populate the cboComments DropDownList
during the intialization process itself as the data is small and as it is an aspx control you will typically do it in the Page_Load
event you can see this thread for more information. 您可以按照以下给定的方法进行级联样式下拉列表,也可以在初始化过程中填充cboComments DropDownList
,因为数据很小,并且它是aspx控件,因此通常在Page_Load
事件中执行此操作可以查看该线程以获取更多信息。
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CascadingDDL._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="jumbotron">
<h1>Cascading Drop Down List</h1>
</div>
<div class="row">
<div class="col-md-12">
<asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboStatus_SelectedIndexChanged">
<asp:ListItem Value="0" Text="All"></asp:ListItem>
<asp:ListItem Value="1" Text="Not Scheduled"></asp:ListItem>
<asp:ListItem Value="2" Text="Rejected"></asp:ListItem>
<asp:ListItem Value="3" Text="Selected"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="cboComments" runat="server">
<asp:ListItem Value="0" Text="Select All"></asp:ListItem>
<asp:ListItem Value="1" Text="Poor Communication"></asp:ListItem>
<asp:ListItem Value="3" Text="Failed in Test"></asp:ListItem>
<asp:ListItem Value="4" Text="Poor Typing skill"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</asp:Content>
The Code-Behind 背后的代码
namespace CascadingDDL
{
using System;
using System.Web.UI;
public partial class _Default : Page
{
public const int StatusRejected = 2;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.cboComments.Enabled = false;
}
}
protected void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
{
var status = int.Parse(this.cboStatus.SelectedValue);
if (status == StatusRejected)
{
this.cboComments.Enabled = true;
}
else
{
this.cboComments.Enabled = false;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.