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
LVL 29
QPRAsked:
Who is Participating?
 
SammyConnect With a Mentor Commented:
actually you dont need to post your code,
all you need is to run your page and view the source. you will see your checkbox1 id has changed to your wizarID_checkbox1

example
<asp:Wizard ID="Wizard1" runat="server" OnFinishButtonClick="Wizard1_FinishButtonClick">
            <WizardSteps>
                <asp:WizardStep runat="server" Title="Step 1">
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </asp:WizardStep>
                <asp:WizardStep runat="server" Title="Step 2">
                </asp:WizardStep>
            </WizardSteps>
        </asp:Wizard>
        <asp:Label ID="lbl" runat="server"></asp:Label>
if I do response.write here for the CheckBox1 clientID i will get Wizard1_CheckBox1 thats why you are getting object expected error
View the source of the page, see what the clientID is for checkBox and change it in your javascript

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

What actually you want to do ?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
QPRAuthor Commented:
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?
0
 
QPRAuthor Commented:
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)
0
 
dynamicrevolutionsCommented:
in your control, add autopostback=true
not adding that will not execute Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As....
0
 
SammyCommented:
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
0
 
sandip132Commented:
///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;
                  }
            }
0
 
QPRAuthor Commented:
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.
0
 
dynamicrevolutionsCommented:
the point should be given to sammy.
he mentioned about autopostback initially
0
 
QPRAuthor Commented:
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
0
 
SammyCommented:
Can you post your code so we can see why the postback is not responding??
0
 
QPRAuthor Commented:
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>
0
 
SammyCommented:
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
0
 
QPRAuthor Commented:
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?
0
 
SammyCommented:
>>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?
0
 
QPRAuthor Commented:
"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.
0
 
SammyCommented:
>>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
0
 
SammyCommented:
0
 
QPRAuthor Commented:
>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?
0
 
SammyCommented:
they are controlled by the framework and dont need the user interaction
post back if theres anything you dont understand about the custom validator.

0
 
QPRAuthor Commented:
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>
0
 
SammyCommented:
Object expected means the script can not see the the object "Control"
can you post your aspx code
0
 
QPRAuthor Commented:
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?
0
 
QPRAuthor Commented:
spot on - thanks
0
 
QPRAuthor Commented:
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?
0
 
SammyCommented:
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
0
 
QPRAuthor Commented:
Thanks, I'll do that.
0
All Courses

From novice to tech pro — start learning today.