Click here to Skip to main content
15,910,877 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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
ASP.NET
<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
C#
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 want show these field against LEAVE,SHORTLEAVE,LONGLEAVE,and hide upon Absent ,SpecialDuty etc
Posted
Comments
BillWoodruff 3-Jan-15 19:44pm    
It looks like your code as shown would work for the two values of ddlEmpStatus shown.

Is your question how do you handle implementing several other complex conditions, or is there some problem with the code you have now ?
Sajid227 3-Jan-15 20:04pm    
both of your options are valid,i have checked this code in vs but its not works and also i need to implement it for multiple value of same drop down as explain above
BillWoodruff 3-Jan-15 21:01pm    
Okay, if it's not working now, please describe the error message(s) you get, and where they occur; or, if there are no error messages, but the result is not what you expect, then tell us what you see that is not "right."

Do set a break-point at the start of your 'SelectedIndexChanged EventHandler, and use F11 (in Visual Studio) to single-step through the code, and examine the values of any variables and what does/does not get executed.

A question that comes up in a situation like this: do I create something that works asap and that may be difficult to extend/maintain/modify later, or do spend more time and energy making something more general purpose that will be easy to extend and maintain, and which, hopefully, I can re-use in the future.

My choice is always, unless impossible because of deadline$, etc., to go for making something general-purpose, and I have already made a tool for handling complex UI changes that I do re-use, or re-write as needed. For me, it is always easier to scale-down a general purpose tool/library to fit some special purpose than it is to create a new tool for each new situation that is similar ... but, I accept that's not true, or simply not a possible option, for many other people.

1 solution

change the condition as below
C#
string[] visibleItems = new []{"LEAVE","SHORTLEAVE","LONGLEAVE"};

var selectedvalue =ddlEmpStatus.SelectedValue.ToUpper();
if(visibleItems.Contains(selectedvalue))
{
  //"show"
}else
{
   //"hide"
}


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.
 
Share this answer
 
v2
Comments
DamithSL 3-Jan-15 21:11pm    
I may have done something wrong, please leave a comment when you down vote and it will definitely help me to improve.
Sajid227 4-Jan-15 13:53pm    
I HAVE ANOTHER problem,there are three type of status type not two,like for some status type i have to show a,b,c,d fields,for other a,b,c and for last one just a,b ,then how to set them,your solution is working for two level.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900