Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

Disable radio button list

I have 2 radio lists, if somebody chooses 'Yes' for the first question then the 2nd question does not apply.
I thought it would be a nice touch to disable this 2nd control to give a visual clue but can't seem to get it to happen.
Here is the code, any idea why not working and/or how to make it happen?

  Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdNZresident.SelectedIndexChanged
        If RdNZresident.SelectedItem.Value = "Yes" Then
            RdNZpermit.Enabled = False
        End If
    End Sub
Avatar of Sammy
Sammy
Flag of Canada image

did you have the autopostback set to true for the first radio list?
then the 2nd question does not apply ???

What actually you want to do ?
Avatar of QPR

ASKER

I don't have any autopostback set to true but given that it is a server side control that sounds logical... can you expand please?
Avatar of QPR

ASKER

Sandip132....
It's pretty simple

Q1 yes or no.
Q2 if you said yes to Q1 then yes or no to question 2

If you said no to Q1 then disable/grey out the Q2 list.
Saves the user from given an illogical reply No to Q1 and Yes to Q2 makes no sense.
Greying out Q2 just gives a nicer user experience (to use that cliche)
Avatar of dynamicrevolutions
dynamicrevolutions

in your control, add autopostback=true
not adding that will not execute Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As....
like dynamicrevolutions said if you dont set the autopostback to true the SelectedIndexChanged will never be reached
the other option is to use a clientside to disable and enable the radiobuttons based on the user's response.
the advantage of using a clientscript is saving app from making a round trip to the server.
http://www.houseofscripts.com/scripts/javascripts/disablerb.htm
if you decide on using a clientscript then all you need is to add an onclick event
this.myRadioButton.Attributes.Add("onclick",javascript:your_function_here();");

HTH
///Set  AutoPostback=True for RadioButtonList1

private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if(RadioButtonList1.Items[0].Selected==true)
                  {
                        RadioButtonList2.Enabled=false;
                  }
                  else
                  {
                        RadioButtonList2.Enabled=true;
                  }
            }
Avatar of QPR

ASKER

thanks Dynamic - I'll give it a go when I'm back at work tomorrow.
Maybe the roundtrip overhead will counter act the small benefit of doing the disabling thing.
the point should be given to sammy.
he mentioned about autopostback initially
Avatar of QPR

ASKER

autopostback didn't work unless I'm doing something wrong?
I highlighted the first control and set enablepostback to true in the properties.
Hit debug and tested - regardless of which option I choose in RList1 RList is still enabled
Can you post your code so we can see why the postback is not responding??
Avatar of QPR

ASKER

Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdNZresident.SelectedIndexChanged
   If RdNZresident.SelectedItem.Value = "Yes" Then
      RdNZpermit.Enabled = False
   End If
End Sub


<asp:RadioButtonList ID="RdNZresident" runat="server" AutoPostBack="True">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>

<asp:RadioButtonList ID="RdNZpermit" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
Your code runs fine
I havent changed a single line and it worked fine for me.
<asp:ListItem>Yes</asp:ListItem> ---> when click on this list item this
<asp:RadioButtonList ID="RdNZpermit" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
will be disabled

if you want to toggle it then you should add an else statement
If RdNZresident.SelectedItem.Value = "Yes" Then
            RdNZpermit.Enabled = False
        Else
            RdNZpermit.Enabled = True
        End If

HTH
Avatar of QPR

ASKER

I have found the problem but have no idea how to fix it.
I'm working with a wizard control and the radio lists appear on page/section 4.
On page 5 I have a checkbox that must be ticked before the user can finish. (I have a client side javascript function to ensure this)
When I nav to page 4 and select the radio button RdNZresident I get a javascript error saying that document.form1.ChkAgree.checked is null or not an object. If I hit the "previous" button and then "next" to come back to the same page there is no error and RdNZpermit is diasbled!

So how come the postback is causing the JS function in default.aspx to error on postback but not when initially loaded?
The function is handled via <form id="form1" runat="server" onsubmit="return AgreeCheck()">
Perhaps the "next" button on the wizard is being treated as some kind of submit?
>>Perhaps the "next" button on the wizard is being treated as some kind of submit?
wizard buttons are submit buttons
>>The function is handled via <form id="form1" runat="server" onsubmit="return AgreeCheck()">
whats the reason for this?
cant you use any other event instead of the form's submit?
Avatar of QPR

ASKER

"cant you use any other event instead of the form's submit?"
I'm open to suggestions.
I need to check to see if a checkbox has been checked prior to submitting the form (clicking "finish" on the wizards final step).
There is no validator for checkboxes and examples I've seen of making home made ones look long and complicated.
>>There is no validator for checkboxes and examples I've seen of making home made ones look long and complicated.
would this be complicated?
<script type="text/javascript">
function ValCheck(source, args)
    {
    args.IsValid=document.getElementById("CheckBox1").checked;
    }
    </script>
<asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Check Box Not Checked" Display="dynamic" ClientValidationFunction="ValCheck"
            ></asp:CustomValidator>

I am using a CustomValidator here and if you want you can do more validation on the server as well.

HTH
Sammy
Avatar of QPR

ASKER

>would this be complicated?
For a novice to .NET like myself yes... that said, I am a master of the copy and paste so thanks :)
So does this need an event to trigger it or do these server side customvalidators auto run?
they are controlled by the framework and dont need the user interaction
post back if theres anything you dont understand about the custom validator.

Avatar of QPR

ASKER

Getting errors.
If I tick the checkbox and submit I get the error
If I don't tick the checkbox I get the validation text but still the error.
Error = Line 12, char 5 object expected.
I have double checked and it is called CheckBox1 in both the function and the form
This is the first block of code in the page including blank linkes, 12 seems to be the actual statement


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
      Vacancy Application
</title><link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
function ValCheck(source, args)
    {
    args.IsValid=document.getElementById("CheckBox1").checked;
    }
    </script>

</head>
Object expected means the script can not see the the object "Control"
can you post your aspx code
Avatar of QPR

ASKER

Will post tomorrow when back at work, given it's a 5 page wizard control it's pretty long!
Are there any relevant bits you want to see or the whole thing?
ASKER CERTIFIED SOLUTION
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QPR

ASKER

spot on - thanks
Avatar of QPR

ASKER

Meant to add..... when I run the site the output window has lots of warnings like this...
c:\websites\VApplications\Default.aspx.vb(56,0): warning BC42020: Variable declaration without an 'As' clause; type of Object assumed.

This line points to Dim strQuals6 = txtQuals6.Text
All the warnings point to similar dims.
Anything to worry about?
should be Dim strQuals6 as String=txtQuals6.Text
the warning is basically telling you are declaring variables without a specific type and the framework will assume all of these variables are meant to be objects.

correct the variable declaration to reflect the expected data they will hold.

HTH
Avatar of QPR

ASKER

Thanks, I'll do that.