如何隐藏和显示表单的字段 [英] how to hide and show fields of the form
问题描述
我有一个asp表单,其中我有几个控件,有一个下拉值,我想显示和隐藏以下字段基于从下拉列表中选择的值,问题是没有一个值对我想显示或隐藏字段,有两个以上的值asp代码是
i have a asp form ,in which i have few controls,there is a drop down value ,i want to show and hide followings fields base on the value selected from the drop down,problem is there is not one value against i want to show or hide the fields ,there are more then two values asp code is
<label class="col-sm-2 control-label">
EmployeeStatus: <span class="symbol required"></span>
</label>
<div class="col-sm-3">
<asp:DropDownList CssClass="form-control" ID="ddlEmpStatus" runat="server" OnSelectedIndexChanged="ddlEmpStatus_SelectedIndexChanged" AutoPostBack="true" />
<asp:RegularExpressionValidator runat="server" ID="revddlEmpStatus" ControlToValidate="ddlEmpStatus" Required="required" Display="Dynamic"></asp:RegularExpressionValidator>
</div>
后面的代码是
code behind is
protected void ddlEmpStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddlEmpStatus.SelectedValue == "Absent")
{
ddlDutyType.Visible = false;
ddlEmployeeShift.Visible = false;
txtEndDate.Visible = false;
txtHasWirles.Visible = false;
}
else
{
txtStartDate.Visible = true;
txtEndDate.Visible = true;
ddlDutyType.Visible = true;
ddlEmployeeShift.Visible = true;
}
}
i希望将这些字段展示给LEAVE,SHORTLEAVE,LONGLEAVE,并隐藏在Absent,SpecialDuty etc
i want show these field against LEAVE,SHORTLEAVE,LONGLEAVE,and hide upon Absent ,SpecialDuty etc
推荐答案
更改条件如下
change the condition as below
string[] visibleItems = new []{"LEAVE","SHORTLEAVE","LONGLEAVE"};
var selectedvalue =ddlEmpStatus.SelectedValue.ToUpper();
if(visibleItems.Contains(selectedvalue))
{
//"show"
}else
{
//"hide"
}
但是,除了硬编码隐藏和可见项目之外,您还可以在员工状态表中使用另一列来保留此隐藏/可见值。然后你可以将它的value属性和显示属性绑定到你想要显示的文本列。
But rather than hard code those hide and visible items you can have another column in employee status table to keep this hide/visible value. then you can bind the value property for that and display property to text column you want to display.
这篇关于如何隐藏和显示表单的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!