Click here to Skip to main content
12,241,270 members (58,417 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
I have radio buttons in my project which should get disabled if one of the 2 options from a list of 4 attributes is selected. This is my code:

<asp:DropDownList ID="drpLType" runat="server" CssClass="drp">
 <asp:ListItem Value="0" Selected="True">Professional</asp:ListItem>
 <asp:ListItem Value="1">Enterprise</asp:ListItem>
 <asp:ListItem Value="2">Maintanence</asp:ListItem>
 <asp:ListItem Value="3">Reporting</asp:ListItem>
</asp:DropDownList>

I used javascript for this purpose:

function funMeapSupportValidate(val)
{
  switch(val)
    {
        case "0" :
            document.getElementById('<%=this.rdoMeapSupport.ClientID%>').disabled = false;
            break;
        case "1" :
            document.getElementById('<%=this.rdoMeapSupport.ClientID%>').disabled = false;
            break;
        case  "2" :
            document.getElementById('<%=this.rdoMeapSupport.ClientID%>').check = false;
            document.getElementById('<%=this.rdoMeapSupport.ClientID%>').disabled = true;
            break;
        case  "3" :
            document.getElementById('<%=this.rdoMeapSupport.ClientID%>').check = false;
             document.getElementById('<%=this.rdoMeapSupport.ClientID%>').disabled = true;
            break;
        default:
            break;
    }
}

At my backend code:

protected void drpLType_SelectedIndexChanged(object sender, EventArgs e)
{
    if (drpLType.SelectedValue == "Professional")
    {
        rdoMeapSupport.Enabled = true;
    }
 
    if (drpLType.SelectedValue == "Enterprise")
    {
        rdoMeapSupport.SelectedValue = null;
        rdoMeapSupport.Enabled = true;
    }
 
    if ((drpLType.SelectedValue == "Maintanence") || (drpLType.SelectedValue == "Reporting"))
    {
        rdoMeapSupport.Enabled = false;
        rdoMeapSupport.ClearSelection();
    }
 
}

Now the problem is there are two buttons on my website. Whenever I click those buttons, the radio buttons get activated even when I have selected Maintenance or Reporting . How do I disable that?

This is on my page load event:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Tab1.CssClass = "Clicked";
        MainView.ActiveViewIndex = 0;
 
        if (drpLType.SelectedValue == "Professional")
        {
            rdoMeapSupport.Enabled = true;
            rdoMeapSupport.SelectedValue = "Yes";
        }
 
        drpLType_SelectedIndexChanged();
    }
 
    drpLType.Attributes.Add("onchange", "javascript:funMeapSupportValidate(this.value);");
}
Posted 18-Dec-12 17:53pm
Edited 18-Dec-12 18:08pm
v4
Comments
Dee_Bee 18-Dec-12 23:02pm
   
Check whether you have written rdoMeapSupport.Enabled = true; on page load.
BlehBlahBah 18-Dec-12 23:08pm
   
hey. i've updated my question with the page load event
Dee_Bee 18-Dec-12 23:18pm
   
remove if (drpLType.SelectedValue == "Professional")
{
rdoMeapSupport.Enabled = true;
rdoMeapSupport.SelectedValue = "Yes";
}
from your on page load and run the solution. I guess problem is with that only.
Dee_Bee 18-Dec-12 23:20pm
   
Why did you place if (drpLType.SelectedValue == "Professional") { rdoMeapSupport.Enabled = true; rdoMeapSupport.SelectedValue = "Yes"; }
on page load again, as you have separate event in your code ?
Zaf Khan 18-Dec-12 23:17pm
   
With ref to my solution (solution 1) the same applies to your code in your FORM_Load event code.... use the numbers instead of the text professional, enterprise etc etc.....

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

in your code where you have the following..........


protected void drpLType_SelectedIndexChanged(object sender, EventArgs e)
{
    if (drpLType.SelectedValue == "Professional")
    {
        rdoMeapSupport.Enabled = true;
    }
 
    if (drpLType.SelectedValue == "Enterprise")
    {
        rdoMeapSupport.SelectedValue = null;
        rdoMeapSupport.Enabled = true;
    }
 
    if ((drpLType.SelectedValue == "Maintanence") || (drpLType.SelectedValue == "Reporting"))
    {
        rdoMeapSupport.Enabled = false;
        rdoMeapSupport.ClearSelection();
    }
 
}


Shouldnt that be.............

protected void drpLType_SelectedIndexChanged(object sender, EventArgs e)
{
    if (drpLType.SelectedValue == "1")
    {
        rdoMeapSupport.Enabled = true;
    }
 
    if (drpLType.SelectedValue == "2")
    {
        rdoMeapSupport.SelectedValue = null;
        rdoMeapSupport.Enabled = true;
    }
 
    if ((drpLType.SelectedValue == "3") || (drpLType.SelectedValue == "4"))
    {
        rdoMeapSupport.Enabled = false;
        rdoMeapSupport.ClearSelection();
    }
 
}


The reason for this is you have CLEARLY defined VALUE="1" value="2" etc
  Permalink  
v2
Comments
BlehBlahBah 18-Dec-12 23:16pm
   
what exactly is the difference?
BlehBlahBah 18-Dec-12 23:25pm
   
@zaf khan: Thanks a lot!
Zaf Khan 19-Dec-12 9:01am
   
The difference is that the TEXTual value that your using like PROFESSINAL,ENTERPRISE Etc makefora more USER FRIENDLY experience so that humans can easily understand the item they are selecting, and you have chosen to define the values as numbers as opposed to string values, no one option is better than the other strings or numbers, you use whatever method suits your needs best.

So as you have defined the values for the option items in the list/dropdown in a numeric format so you should use NMERIC format when testing for the results....

If you had used values with a text format like value="cat" or value="dog" so then you would test for a textual result such as .... If Something="cat"...

Hope that explains a little better. I find the MSDN website is a good source of information, though some of the deeper stuff takes some reading over and over.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100