Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have one dropdownlist where i have items called male and female.
and i have another textbox labled pregnancy count .
once i select female that pregnanacy count should enable.if male it should disable using c#.
help me out.
thanks in Advance.:)

XML
Here is my source code

<div id="R3C2" style="width:23%;height:7%;float:left;border:Solid 1px Black">
<asp:Label ID="Gendr" runat="server" Text="Gender">
<asp:DropDownList ID="DropDwnList1" runat="server"
onselectedindexchanged="DropDwnList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem >Male
<asp:ListItem Selected="True">Female

</div>
<div id="R3C3" style="width:26%;height:7%;float:left;border:Solid 1px Black">
<asp:Label ID="PrgncyCunt" runat="server" Text="PregnancyCount">
<asp:TextBox ID="TextBox5" runat="server" Width="15.8%">
</div>
Posted
Updated 25-May-15 0:39am
v3
Comments
Raje_ 25-May-15 6:17am    
Is it a Windows App or Web Application?
Member 11686116 25-May-15 6:21am    
Web Application
Member 11686116 25-May-15 7:05am    
yes if it is male i want to disable it

You can do this by using jquery also :
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

 <script type="text/javascript">
  $(document).ready(function(){
  
    $('#gender').change(function(){
	   var value = $(this).val();
	   if(value == 1 || value == 0)
	   {
	     $('.pregnencyDiv').hide();
	   }
	   else
	   {
	     $('.pregnencyDiv').show();
	   }
	});
  });
  
</script>
<style>
  .pregnencyDiv
  {
    display : none;
  }
</style>
<div>
 <select id="gender">
	 <option value='0'>--Select--</option>
	 <option value='1'>Male</option>
	 <option value='2'>Female</option>
 </select>
 <br>
 <div class="pregnencyDiv"> 
  Pregnancy count <input type='text' id='txtCount' name='txtCount' />
 <div>
</div>


[Edit]

It is always good to avoid unnecessary server round trip. but if you need it in from server side here you go :

ASP.NET
<div>
        <asp:DropDownList ID="dropDownGender" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropDownGender_SelectedIndexChanged">
            <asp:ListItem Value="0" Text="--Select--"></asp:ListItem>
            <asp:ListItem Value="1" Text="Male"></asp:ListItem>
            <asp:ListItem Value="2" Text="Female"></asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="txtCount" runat="server" Enabled="false"></asp:TextBox>
    </div>


then create OnSelectedIndexChanged event in your code behind page :

C#
protected void dropDownGender_SelectedIndexChanged(Object sender, EventArgs e)
   {
       var value = Convert.ToInt32(dropDownGender.SelectedValue);

       if (value == 0 || value == 1)
       {
           txtCount.Enabled= false;
       }
       else
       {
           txtCount.Enabled= true;
       }
   }


Hope it helps you. :)
 
Share this answer
 
v4
Comments
Member 11686116 25-May-15 6:29am    
Thanku .But i want using C#:(
Raje_ 25-May-15 6:49am    
:)
Member 11686116 25-May-15 6:55am    
i hve used but it is enabled if am selecting male it is going to pregnanacyCount :(
Raje_ 25-May-15 7:01am    
Do you want to hide PrgncyCunt label? can you post your code behind code?
protected void ddlSex_SelectedIndexChanged(object sender, EventArgs e)
{
    
//write your code here for the change event. i.e. check the selected value is "Female" and then disable the male textbox
if(ddlSex.SelectedValue=="Female"
{
txtMale.Enabled= false;

}
else
{txtMale.Enabled = true;
}
}


Assuming you are using ASP.NET Web form not mvc
 
Share this answer
 
v4
Comments
Member 11686116 25-May-15 6:25am    
If am using this code got error like "cannot implicitly convert type srting to bool"
Mostafa Asaduzzaman 25-May-15 6:27am    
can you try ddlSex.SelectedValue.ToString()=="Female"
Member 11686116 25-May-15 6:32am    
Operator == Cannot be applied operands of type methodgroup and string.this error message.
Something like this maybe works for you

if(dropdownlist.Text == "Male")
{
textbox.Enabled = false;
}
else
{
textbox.Enabled = true;
}
 
Share this answer
 
Comments
Member 11686116 25-May-15 6:34am    
I have tried this but that text box is enabled.
Member 11686116 25-May-15 6:34am    
Here is my source code

<div id="R3C2" style="width:23%;height:7%;float:left;border:Solid 1px Black">
<asp:Label ID="Gendr" runat="server" Text="Gender">
<asp:DropDownList ID="DropDwnList1" runat="server"
onselectedindexchanged="DropDwnList1_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem >Male
<asp:ListItem Selected="True">Female

</div>
<div id="R3C3" style="width:26%;height:7%;float:left;border:Solid 1px Black">
<asp:Label ID="PrgncyCunt" runat="server" Text="PregnancyCount">
<asp:TextBox ID="TextBox5" runat="server" Width="15.8%">
</div>
Raje_ 25-May-15 6:44am    
Please see my edited answer.

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